mirror of
https://github.com/python/cpython.git
synced 2025-08-28 04:35:02 +00:00
Issue #22982: Improve BOM handling when seeking to multiple positions of a writable text file.
This commit is contained in:
parent
20d31b5182
commit
85e3ee749c
4 changed files with 52 additions and 15 deletions
26
Lib/_pyio.py
26
Lib/_pyio.py
|
@ -1865,6 +1865,19 @@ class TextIOWrapper(TextIOBase):
|
|||
return buffer
|
||||
|
||||
def seek(self, cookie, whence=0):
|
||||
def _reset_encoder(position):
|
||||
"""Reset the encoder (merely useful for proper BOM handling)"""
|
||||
try:
|
||||
encoder = self._encoder or self._get_encoder()
|
||||
except LookupError:
|
||||
# Sometimes the encoder doesn't exist
|
||||
pass
|
||||
else:
|
||||
if position != 0:
|
||||
encoder.setstate(0)
|
||||
else:
|
||||
encoder.reset()
|
||||
|
||||
if self.closed:
|
||||
raise ValueError("tell on closed file")
|
||||
if not self._seekable:
|
||||
|
@ -1885,6 +1898,7 @@ class TextIOWrapper(TextIOBase):
|
|||
self._snapshot = None
|
||||
if self._decoder:
|
||||
self._decoder.reset()
|
||||
_reset_encoder(position)
|
||||
return position
|
||||
if whence != 0:
|
||||
raise ValueError("unsupported whence (%r)" % (whence,))
|
||||
|
@ -1922,17 +1936,7 @@ class TextIOWrapper(TextIOBase):
|
|||
raise OSError("can't restore logical file position")
|
||||
self._decoded_chars_used = chars_to_skip
|
||||
|
||||
# Finally, reset the encoder (merely useful for proper BOM handling)
|
||||
try:
|
||||
encoder = self._encoder or self._get_encoder()
|
||||
except LookupError:
|
||||
# Sometimes the encoder doesn't exist
|
||||
pass
|
||||
else:
|
||||
if cookie != 0:
|
||||
encoder.setstate(0)
|
||||
else:
|
||||
encoder.reset()
|
||||
_reset_encoder(cookie)
|
||||
return cookie
|
||||
|
||||
def read(self, size=None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue