Kaydet (Commit) a1b49013 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

fix TextIOWrapper.read() when the buffer is not readable #5628

üst d2ee64d9
...@@ -1696,6 +1696,7 @@ class TextIOWrapper(TextIOBase): ...@@ -1696,6 +1696,7 @@ class TextIOWrapper(TextIOBase):
return cookie return cookie
def read(self, n=None): def read(self, n=None):
self._checkReadable()
if n is None: if n is None:
n = -1 n = -1
decoder = self._decoder or self._get_decoder() decoder = self._decoder or self._get_decoder()
......
...@@ -1754,6 +1754,13 @@ class TextIOWrapperTest(unittest.TestCase): ...@@ -1754,6 +1754,13 @@ class TextIOWrapperTest(unittest.TestCase):
self.assertEquals(f.read(), data * 2) self.assertEquals(f.read(), data * 2)
self.assertEquals(buf.getvalue(), (data * 2).encode(encoding)) self.assertEquals(buf.getvalue(), (data * 2).encode(encoding))
def test_unreadable(self):
class UnReadable(self.BytesIO):
def readable(self):
return False
txt = self.TextIOWrapper(UnReadable())
self.assertRaises(IOError, txt.read)
def test_read_one_by_one(self): def test_read_one_by_one(self):
txt = self.TextIOWrapper(self.BytesIO(b"AA\r\nBB")) txt = self.TextIOWrapper(self.BytesIO(b"AA\r\nBB"))
reads = "" reads = ""
......
...@@ -53,6 +53,8 @@ Core and Builtins ...@@ -53,6 +53,8 @@ Core and Builtins
Library Library
------- -------
- Issue #5628: Fix io.TextIOWrapper.read() with a unreadable buffer.
- Issue #5619: Multiprocessing children disobey the debug flag and causes - Issue #5619: Multiprocessing children disobey the debug flag and causes
popups on windows buildbots. Patch applied to work around this issue. popups on windows buildbots. Patch applied to work around this issue.
......
...@@ -1348,6 +1348,11 @@ TextIOWrapper_read(PyTextIOWrapperObject *self, PyObject *args) ...@@ -1348,6 +1348,11 @@ TextIOWrapper_read(PyTextIOWrapperObject *self, PyObject *args)
CHECK_CLOSED(self); CHECK_CLOSED(self);
if (self->decoder == NULL) {
PyErr_SetString(PyExc_IOError, "not readable");
return NULL;
}
if (_TextIOWrapper_writeflush(self) < 0) if (_TextIOWrapper_writeflush(self) < 0)
return NULL; return NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment