summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2017-04-15 05:25:15 (GMT)
committerGitHub <noreply@github.com>2017-04-15 05:25:15 (GMT)
commitd5fa5f3ce7d9003bbd3975d1bf634043305ae18f (patch)
tree82915931446d0a3eb293741ea8c593a35657449e
parent05bfbcd233b2f5ba0d0634a380092d6ead6b35e1 (diff)
downloadcpython-d5fa5f3ce7d9003bbd3975d1bf634043305ae18f.zip
cpython-d5fa5f3ce7d9003bbd3975d1bf634043305ae18f.tar.gz
cpython-d5fa5f3ce7d9003bbd3975d1bf634043305ae18f.tar.bz2
bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130) (#1150)
-rw-r--r--Lib/test/test_io.py1
-rw-r--r--Misc/NEWS8
-rw-r--r--Modules/_io/iobase.c25
3 files changed, 23 insertions, 11 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 8f895fe..e04baef 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -3498,6 +3498,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)
diff --git a/Misc/NEWS b/Misc/NEWS
index e623457c..7628ed5 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,12 +32,14 @@ Core and Builtins
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.
+- bpo-29692: Fixed arbitrary unchaining of RuntimeError exceptions in
+ contextlib.contextmanager. Patch by Siddharth Velankar.
- bpo-29998: Pickling and copying ImportError now preserves name and path
attributes.
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 472ef3b..f7986d7 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -650,7 +650,7 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
/*[clinic end generated code: output=2f50421677fa3dea input=1961c4a95e96e661]*/
{
Py_ssize_t length = 0;
- PyObject *result;
+ PyObject *result, *it = NULL;
result = PyList_New(0);
if (result == NULL)
@@ -664,19 +664,22 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);
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 */
@@ -684,8 +687,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);
@@ -693,7 +695,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]