Bug in io.TextIOWrapper?

Jon Ribbens jon+usenet at unequivocal.eu
Mon Jun 19 10:55:04 EDT 2023


io.TextIOWrapper() wraps a binary stream so you can write text to it.
It takes an 'encoding' parameter, which it uses to look up the codec
in the codecs registry, and then it uses the IncrementalEncoder and
IncrementalDecoder classes for the appropriate codec.

The IncrementalEncoder.encode() function is given the object to encode
of course, and also an optional second parameter which indicates if
this is the final output.

The bug is that TextIOWrapper() never sets the second parameter to
indicate that the output is complete - not even if you call close().

Example:

    >>> import io
    >>> buffer = io.BytesIO()
    >>> stream = io.TextIOWrapper(buffer, encoding='idna')
    >>> stream.write('abc.example.com')
    15
    >>> stream.flush()
    >>> buffer.getvalue()
    b'abc.example.'

Obviously using the 'idna' wrapper as an encoding on a stream is a bit
unlikely, but nevertheless any other codec which cares about the 'final'
parameter will also have this problem.


More information about the Python-list mailing list