Skip to content

Commit 0464de0

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
[2.7] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918). (GH-8013)
(cherry picked from commit 23db935) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
1 parent 11ba050 commit 0464de0

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/_pyio.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ def write(self, s):
16191619
self.buffer.write(b)
16201620
if self._line_buffering and (haslf or "\r" in s):
16211621
self.flush()
1622+
self._set_decoded_chars('')
16221623
self._snapshot = None
16231624
if self._decoder:
16241625
self._decoder.reset()

Lib/test/test_io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,17 @@ def _get_bad_decoder(dummy):
27412741
with self.maybeRaises(TypeError):
27422742
t.read(42)
27432743

2744+
def test_issue25862(self):
2745+
# Assertion failures occurred in tell() after read() and write().
2746+
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
2747+
t.read(1)
2748+
t.read()
2749+
t.tell()
2750+
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
2751+
t.read(1)
2752+
t.write('x')
2753+
t.tell()
2754+
27442755

27452756
class CTextIOWrapperTest(TextIOWrapperTest):
27462757

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ typedef struct
707707
PyObject *dict;
708708
} textio;
709709

710+
static void
711+
textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
710712

711713
/* A couple of specialized cases in order to bypass the slow incremental
712714
encoding methods for the most popular encodings. */
@@ -1329,6 +1331,7 @@ textiowrapper_write(textio *self, PyObject *args)
13291331
Py_DECREF(ret);
13301332
}
13311333

1334+
textiowrapper_set_decoded_chars(self, NULL);
13321335
Py_CLEAR(self->snapshot);
13331336

13341337
if (self->decoder) {
@@ -1534,6 +1537,7 @@ textiowrapper_read(textio *self, PyObject *args)
15341537
if (final == NULL)
15351538
goto fail;
15361539

1540+
textiowrapper_set_decoded_chars(self, NULL);
15371541
Py_CLEAR(self->snapshot);
15381542
return final;
15391543
}

0 commit comments

Comments
 (0)