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
readline() causes output to be written at eof unless seek() is used #113439
I was able to reproduce this issue, and I'd like to work on fixing it.
I looked into the bug, and it seems like it's being caused by the TextIOWrapper class (in both the C io and Python pyio modules) reading an entire chunk at a time, then not rewinding its stream pointer before performing the write.
Reproducing this example with opening a file in binary mode (as opposed to text) works as expected, rather than being buggy. The TextIOWrapper class, which is used for text file I/O, has its own buffer it reads an entire chunk into for every read or readline call, and doesn't seek to the correct position when writing after reading. So I think the TextIOWrapper class should be changed.
The simplest fix would be to essentially add self.seek(self.tell()) to both the C and Python implementations of TextIOWrapper whenever we are writing to a stream that is readable and has a non-empty read buffer. For non-seekable streams, we may just want to leave the implementation as is (i.e. calling self.seek(self.tell()) only if the stream is seekable). The only way I see to make this bug not occur for non-seekable streams would be to change TextIOWrapper to not buffer its read calls if it is both readable and writable, but not seekable.
Activity
benjaminJohnson2204 commentedon Jan 14, 2024
I was able to reproduce this issue, and I'd like to work on fixing it.
I looked into the bug, and it seems like it's being caused by the
TextIOWrapperclass (in both the Cioand Pythonpyiomodules) reading an entire chunk at a time, then not rewinding its stream pointer before performing the write.Reproducing this example with opening a file in binary mode (as opposed to text) works as expected, rather than being buggy. The
TextIOWrapperclass, which is used for text file I/O, has its own buffer it reads an entire chunk into for everyreadorreadlinecall, and doesn't seek to the correct position when writing after reading. So I think theTextIOWrapperclass should be changed.The simplest fix would be to essentially add
self.seek(self.tell())to both the C and Python implementations ofTextIOWrapperwhenever we are writing to a stream that is readable and has a non-empty read buffer. For non-seekable streams, we may just want to leave the implementation as is (i.e. callingself.seek(self.tell())only if the stream is seekable). The only way I see to make this bug not occur for non-seekable streams would be to changeTextIOWrapperto not buffer itsreadcalls if it is both readable and writable, but not seekable.I'd be interested to hear other opinions on this.
vadmium commentedon Jul 26, 2024
Probably the same as Issue #82891 (and #56424, closed as not worth fixing)
StanFromIreland commentedon Apr 29, 2025
Duplicate: #117095
duaneg commentedon May 25, 2025
#93079 is another duplicate
picnixz commentedon May 25, 2025
Ok, so we haver many duplicates. I'll check which ones to close.