gh-80109: Fix io.TextIOWrapper dropping the internal buffer during write() (GH-22535)

io.TextIOWrapper was dropping the internal decoding buffer
during read() and write() calls.
This commit is contained in:
Zackery Spytz 2024-01-08 02:33:34 -08:00 committed by GitHub
parent e5d0316f35
commit 73c9326563
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 8 deletions

View file

@ -2198,8 +2198,9 @@ class TextIOWrapper(TextIOBase):
self.buffer.write(b)
if self._line_buffering and (haslf or "\r" in s):
self.flush()
self._set_decoded_chars('')
self._snapshot = None
if self._snapshot is not None:
self._set_decoded_chars('')
self._snapshot = None
if self._decoder:
self._decoder.reset()
return length
@ -2513,8 +2514,9 @@ class TextIOWrapper(TextIOBase):
# Read everything.
result = (self._get_decoded_chars() +
decoder.decode(self.buffer.read(), final=True))
self._set_decoded_chars('')
self._snapshot = None
if self._snapshot is not None:
self._set_decoded_chars('')
self._snapshot = None
return result
else:
# Keep reading chunks until we have size characters to return.