mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
bpo-25862: Fix assertion failures in io.TextIOWrapper.tell(). (GH-3918)
This commit is contained in:
parent
bda9c3eae3
commit
23db935bcf
4 changed files with 19 additions and 0 deletions
|
@ -2149,6 +2149,7 @@ class TextIOWrapper(TextIOBase):
|
||||||
self.buffer.write(b)
|
self.buffer.write(b)
|
||||||
if self._line_buffering and (haslf or "\r" in s):
|
if self._line_buffering and (haslf or "\r" in s):
|
||||||
self.flush()
|
self.flush()
|
||||||
|
self._set_decoded_chars('')
|
||||||
self._snapshot = None
|
self._snapshot = None
|
||||||
if self._decoder:
|
if self._decoder:
|
||||||
self._decoder.reset()
|
self._decoder.reset()
|
||||||
|
|
|
@ -3549,6 +3549,17 @@ class TextIOWrapperTest(unittest.TestCase):
|
||||||
expected = 'linesep' + os.linesep + 'LF\nLF\nCR\rCRLF\r\n'
|
expected = 'linesep' + os.linesep + 'LF\nLF\nCR\rCRLF\r\n'
|
||||||
self.assertEqual(txt.detach().getvalue().decode('ascii'), expected)
|
self.assertEqual(txt.detach().getvalue().decode('ascii'), expected)
|
||||||
|
|
||||||
|
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 MemviewBytesIO(io.BytesIO):
|
class MemviewBytesIO(io.BytesIO):
|
||||||
'''A BytesIO object whose read method returns memoryviews
|
'''A BytesIO object whose read method returns memoryviews
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix assertion failures in the ``tell()`` method of ``io.TextIOWrapper``.
|
||||||
|
Patch by Zackery Spytz.
|
|
@ -694,6 +694,9 @@ typedef struct
|
||||||
PyObject *dict;
|
PyObject *dict;
|
||||||
} textio;
|
} textio;
|
||||||
|
|
||||||
|
static void
|
||||||
|
textiowrapper_set_decoded_chars(textio *self, PyObject *chars);
|
||||||
|
|
||||||
/* A couple of specialized cases in order to bypass the slow incremental
|
/* A couple of specialized cases in order to bypass the slow incremental
|
||||||
encoding methods for the most popular encodings. */
|
encoding methods for the most popular encodings. */
|
||||||
|
|
||||||
|
@ -1606,6 +1609,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
|
||||||
Py_DECREF(ret);
|
Py_DECREF(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
textiowrapper_set_decoded_chars(self, NULL);
|
||||||
Py_CLEAR(self->snapshot);
|
Py_CLEAR(self->snapshot);
|
||||||
|
|
||||||
if (self->decoder) {
|
if (self->decoder) {
|
||||||
|
@ -1835,6 +1839,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
textiowrapper_set_decoded_chars(self, NULL);
|
||||||
Py_CLEAR(self->snapshot);
|
Py_CLEAR(self->snapshot);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue