[issue41221] io.TextIOWrapper ignores silently partial write if buffer is unbuffered

STINNER Victor report at bugs.python.org
Tue Jul 7 06:35:55 EDT 2020


STINNER Victor <vstinner at python.org> added the comment:

cc Antoine Pitrou who was involved in io module design.

Currently, the io.TextIOWrapper implementation doesn't handle partial write: it doesn't fully support an unbuffered 'buffer' object.

It should either handle partial write internally, or it should inject a buffered writer between itself (TextIOWrapper) and the unbuffered buffer so handling partial writes who be handled by the buffered writer.

The socket.socket class has a sendall() method which helps to handle such problem. In the io module, sometimes write() can do a partial write (unbuffered writer like FileIO), sometimes it doesn't (buffered writer like BufferedWriter).

== C implementation ==

Modules/_io/text.c. The _io_TextIOWrapper_write_impl() function puts the encoded string into an internal pending_bytes list. If needed, it calls flush(): _textiowrapper_writeflush().

The pseudo-code of _textiowrapper_writeflush() is to call "self.buffer.write(b)" where b is made of all "pending bytes". write() result is ignored: partial write is silently ignored.

== Python implementation ==

_pyio.TextIOWrapper.write() simply calls: "self.buffer.write(b)". It ignores partial write silently.

----------
nosy: +pitrou
title: Output of print() might get truncated in unbuffered mode -> io.TextIOWrapper ignores silently partial write if buffer is unbuffered

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41221>
_______________________________________


More information about the Python-bugs-list mailing list