diff options
author | Xiang Zhang <angwerzx@126.com> | 2017-04-15 04:47:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-15 04:47:28 (GMT) |
commit | 026435ce49419a3366171416c68114dd8a1144c7 (patch) | |
tree | bc20a7681507de85662e789a3331b3a048bdaed6 | |
parent | eaeda64c2fd2abd33e59b03298f9cdc9e8efef89 (diff) | |
download | cpython-026435ce49419a3366171416c68114dd8a1144c7.zip cpython-026435ce49419a3366171416c68114dd8a1144c7.tar.gz cpython-026435ce49419a3366171416c68114dd8a1144c7.tar.bz2 |
bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130)
-rw-r--r-- | Lib/test/test_io.py | 1 | ||||
-rw-r--r-- | Misc/NEWS | 6 | ||||
-rw-r--r-- | Modules/_io/iobase.c | 25 |
3 files changed, 22 insertions, 10 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 929865d..32f76a6 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3510,6 +3510,7 @@ class MiscIOTest(unittest.TestCase): self.assertRaises(ValueError, f.readinto1, bytearray(1024)) self.assertRaises(ValueError, f.readline) self.assertRaises(ValueError, f.readlines) + self.assertRaises(ValueError, f.readlines, 1) self.assertRaises(ValueError, f.seek, 0) self.assertRaises(ValueError, f.tell) self.assertRaises(ValueError, f.truncate) @@ -310,12 +310,14 @@ Extension Modules Library ------- +- bpo-30068: _io._IOBase.readlines will check if it's closed first when + hint is present. + - bpo-29694: Fixed race condition in pathlib mkdir with flags parents=True. Patch by Armin Rigo. - bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in - contextlib.contextmanager. - Patch by Siddharth Velankar. + contextlib.contextmanager. Patch by Siddharth Velankar. - bpo-26187: Test that sqlite3 trace callback is not called multiple times when schema is changing. Indirectly fixed by switching to diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 75cfe59..1a687c5 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -643,7 +643,7 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) /*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/ { Py_ssize_t length = 0; - PyObject *result; + PyObject *result, *it = NULL; result = PyList_New(0); if (result == NULL) @@ -658,19 +658,22 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) self, NULL); if (ret == NULL) { - Py_DECREF(result); - return NULL; + goto error; } Py_DECREF(ret); return result; } + it = PyObject_GetIter(self); + if (it == NULL) { + goto error; + } + while (1) { - PyObject *line = PyIter_Next(self); + PyObject *line = PyIter_Next(it); if (line == NULL) { if (PyErr_Occurred()) { - Py_DECREF(result); - return NULL; + goto error; } else break; /* StopIteration raised */ @@ -678,8 +681,7 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) if (PyList_Append(result, line) < 0) { Py_DECREF(line); - Py_DECREF(result); - return NULL; + goto error; } length += PyObject_Size(line); Py_DECREF(line); @@ -687,7 +689,14 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) if (length > hint) break; } + + Py_DECREF(it); return result; + + error: + Py_XDECREF(it); + Py_DECREF(result); + return NULL; } /*[clinic input] |