Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.7] bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918) #8013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2018
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
1 change: 1 addition & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ def write(self, s):
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._decoder:
self._decoder.reset()
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,17 @@ def _get_bad_decoder(dummy):
with self.maybeRaises(TypeError):
t.read(42)

def test_issue25862(self):
# Assertion failures occurred in tell() after read() and write().
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
t.read(1)
t.read()
t.tell()
t = self.TextIOWrapper(self.BytesIO(b'test'), encoding='ascii')
t.read(1)
t.write('x')
t.tell()


class CTextIOWrapperTest(TextIOWrapperTest):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
Patch by Zackery Spytz.
4 changes: 4 additions & 0 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ typedef struct
PyObject *dict;
} textio;

static void
textiowrapper_set_decoded_chars(textio *self, PyObject *chars);

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

textiowrapper_set_decoded_chars(self, NULL);
Py_CLEAR(self->snapshot);

if (self->decoder) {
Expand Down Expand Up @@ -1534,6 +1537,7 @@ textiowrapper_read(textio *self, PyObject *args)
if (final == NULL)
goto fail;

textiowrapper_set_decoded_chars(self, NULL);
Py_CLEAR(self->snapshot);
return final;
}
Expand Down