I managed to do it according to @ddulaney suggestion: create my own codec my_true
and write my own decode
function.
I have 3 files:
- register.py:
import codecs, io, encodings
from encodings import utf_8
def my_true_decode(input, errors="strict"):
raw = bytes(input).decode("utf-8")
code = raw.replace('1+1==3','1+1==2')
return code, len(input)
def search_function(encoding):
if encoding != "my_true":
return None
utf8 = encodings.search_function("utf8")
return codecs.CodecInfo(
name="my_true",
encode=utf8.encode,
decode=my_true_decode,
)
codecs.register(search_function)
- script.py:
# coding: my_true
if 1+1==3:
print("This is True...")
- run.py:
import register
import script
Now run python3 run.py
Output is This is True...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…