You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
io: TextIOWrapper silently corrupts output when encoder is a no-op #161518
When an encoder returns the input str itself, TextIOWrapper.write treats it as ASCII and writes the str's internal storage to the file. For non-ASCII text the file is silently corrupted.
Expected: TypeError: encoder should return a bytes object, not 'str'.
Reproducer
We use a 44-character string that ends in an emoji, so CPython internally stores it as four bytes per character.
importcodecsclassEnc(codecs.IncrementalEncoder):
defencode(self, text, final=False):
returntextinfo=codecs.CodecInfo(None, None, incrementalencoder=Enc,
incrementaldecoder=codecs.getincrementaldecoder("utf-8"),
name="passthrough")
codecs.register(lambdan: infoifn=="passthrough"elseNone)
withopen("out.txt", "w", encoding="passthrough") asf:
f.write("The quick brown fox jumps over the lazy dog🐍")
print(open("out.txt").read())
The quick b
Only the first 44 bytes (out of 44*4 = 176) were written to the file.
Root cause
Modules/_io/textio.c, in _io_TextIOWrapper_write_impl:
changed the title [-]io: TextIOWrapper silently corrupts its output when the encoder is a no-op[/-][+]io: TextIOWrapper silently corrupts its output when encoder is a no-op[/+]on Nov 6, 2026
changed the title [-]io: TextIOWrapper silently corrupts its output when encoder is a no-op[/-][+]io: TextIOWrapper silently corrupts output when encoder is a no-op[/+]on Nov 6, 2026
Activity
[-]io: TextIOWrapper silently corrupts its output when the encoder is a no-op[/-][+]io: TextIOWrapper silently corrupts its output when encoder is a no-op[/+][-]io: TextIOWrapper silently corrupts its output when encoder is a no-op[/-][+]io: TextIOWrapper silently corrupts output when encoder is a no-op[/+]