Bug in io.TextIOWrapper?

Inada Naoki songofacandy at gmail.com
Mon Jun 19 13:15:00 EDT 2023


stream.flush() doesn't mean final output.
Try stream.close()

2023年6月20日(火) 1:40 Jon Ribbens via Python-list <python-list at python.org>:

> 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.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list