diff options
author | Xiang Zhang <angwerzx@126.com> | 2017-04-15 05:25:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-15 05:25:15 (GMT) |
commit | d5fa5f3ce7d9003bbd3975d1bf634043305ae18f (patch) | |
tree | 82915931446d0a3eb293741ea8c593a35657449e /Modules/_io | |
parent | 05bfbcd233b2f5ba0d0634a380092d6ead6b35e1 (diff) | |
download | cpython-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)
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/iobase.c | 25 |
1 files changed, 17 insertions, 8 deletions
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] |