Skip to content

Commit d6a283b

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
[3.6] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918). (GH-8012)
(cherry picked from commit 23db935) Co-authored-by: Zackery Spytz <[email protected]>
1 parent a730279 commit d6a283b

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

Lib/_pyio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,7 @@ def write(self, s):
20642064
self.buffer.write(b)
20652065
if self._line_buffering and (haslf or "\r" in s):
20662066
self.flush()
2067+
self._set_decoded_chars('')
20672068
self._snapshot = None
20682069
if self._decoder:
20692070
self._decoder.reset()

Lib/test/test_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,6 +3348,17 @@ def seekable(self): return True
33483348
F.tell = lambda x: 0
33493349
t = self.TextIOWrapper(F(), encoding='utf-8')
33503350

3351+
def test_issue25862(self):
3352+
# Assertion failures occurred in tell() after read() and write().
3353+
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
3354+
t.read(1)
3355+
t.read()
3356+
t.tell()
3357+
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
3358+
t.read(1)
3359+
t.write('x')
3360+
t.tell()
3361+
33513362

33523363
class MemviewBytesIO(io.BytesIO):
33533364
'''A BytesIO object whose read method returns memoryviews
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
2+
Patch by Zackery Spytz.

Modules/_io/textio.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,9 @@ typedef struct
696696
PyObject *dict;
697697
} textio;
698698

699+
static void
700+
textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
701+
699702
/* A couple of specialized cases in order to bypass the slow incremental
700703
encoding methods for the most popular encodings. */
701704

@@ -1367,6 +1370,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
13671370
Py_DECREF(ret);
13681371
}
13691372

1373+
textiowrapper_set_decoded_chars(self, NULL);
13701374
Py_CLEAR(self->snapshot);
13711375

13721376
if (self->decoder) {
@@ -1602,6 +1606,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
16021606
if (result == NULL)
16031607
goto fail;
16041608

1609+
textiowrapper_set_decoded_chars(self, NULL);
16051610
Py_CLEAR(self->snapshot);
16061611
return result;
16071612
}

0 commit comments

Comments
 (0)