Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,9 +2417,12 @@ def tell(self):
# be O(1) in most situations (common decoders, sensible input).
# Actually, it will be exactly 1 for fixed-size codecs (all
# 8-bit codecs, also UTF-16 and UTF-32).
skip_bytes = int(self._b2cratio * chars_to_skip)
skip_estimate = self._b2cratio * chars_to_skip
if 0 <= skip_estimate <= len(next_input):
skip_bytes = int(skip_estimate)
else:
skip_bytes = 0
skip_back = 1
assert skip_bytes <= len(next_input)
while skip_bytes > 0:
decoder.setstate((b'', dec_flags))
# Decode up to temptative start point
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,23 @@ def test_tell_after_readline_with_cr(self):
remaining = f.read()
self.assertEqual(remaining, "")

def test_tell_after_seek_past_cr(self):
# tell() must tolerate a stale bytes-per-char ratio after seek().
for data in (b'one\rtwo\rthree\r',
('ab\r' + '\u20ac' * 15).encode()):
expected = data.decode().replace('\r', '\n')
for i in range(len(expected)):
with self.subTest(data=data, i=i):
f = self.TextIOWrapper(self.BytesIO(data),
encoding='utf-8')
f.read(i)
pos = f.tell()
f.read(1)
f.seek(pos)
self.assertEqual(f.read(1), expected[i])
f.seek(f.tell())
self.assertEqual(f.read(), expected[i + 1:])

def test_seek_with_encoder_state(self):
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
f.write("\u00e6\u0300")
Expand Down Expand Up @@ -4412,6 +4429,17 @@ class PyTextIOWrapperTest(TextIOWrapperTest):
io = pyio
shutdown_error = "LookupError: unknown encoding: ascii"

def test_tell_with_invalid_b2cratio(self):
for ratio in (float('nan'), float('inf'), -float('inf'), 1e100):
with self.subTest(ratio=ratio):
with self.TextIOWrapper(self.BytesIO(b'abc'),
encoding='utf-8') as f:
self.assertEqual(f.read(1), 'a')
f._b2cratio = ratio
position = f.tell()
f.seek(position)
self.assertEqual(f.read(), 'bc')


class IncrementalNewlineDecoderTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`io.TextIOWrapper.tell` reading past the end of the buffer after
:meth:`~io.TextIOWrapper.seek`.
11 changes: 9 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2967,9 +2967,16 @@ _io_TextIOWrapper_tell_impl(textio *self)

/* Fast search for an acceptable start point, close to our
current pos */
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
double skip_estimate = self->b2cratio * chars_to_skip;
if (skip_estimate >= 0 &&
skip_estimate <= PyBytes_GET_SIZE(next_input))
{
skip_bytes = (Py_ssize_t)skip_estimate;
}
else {
skip_bytes = 0;
}
skip_back = 1;
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
input = PyBytes_AS_STRING(next_input);
while (skip_bytes > 0) {
/* Decode up to temptative start point */
Expand Down
Loading