bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1152)
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 2fec7e8..066dc8e 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -1,9 +1,9 @@
/*
An implementation of the I/O abstract base classes hierarchy
as defined by PEP 3116 - "New I/O"
-
+
Classes defined here: IOBase, RawIOBase.
-
+
Written by Amaury Forgeot d'Arc and Antoine Pitrou
*/
@@ -19,7 +19,7 @@
typedef struct {
PyObject_HEAD
-
+
PyObject *dict;
PyObject *weakreflist;
} iobase;
@@ -590,7 +590,7 @@
iobase_readlines(PyObject *self, PyObject *args)
{
Py_ssize_t hint = -1, length = 0;
- PyObject *result;
+ PyObject *result, *it = NULL;
if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
return NULL;
@@ -606,19 +606,22 @@
probably be removed here. */
PyObject *ret = PyObject_CallMethod(result, "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 */
@@ -626,8 +629,7 @@
if (PyList_Append(result, line) < 0) {
Py_DECREF(line);
- Py_DECREF(result);
- return NULL;
+ goto error;
}
length += PyObject_Size(line);
Py_DECREF(line);
@@ -635,7 +637,14 @@
if (length > hint)
break;
}
+
+ Py_DECREF(it);
return result;
+
+ error:
+ Py_XDECREF(it);
+ Py_DECREF(result);
+ return NULL;
}
static PyObject *
@@ -823,7 +832,7 @@
int r;
PyObject *chunks = PyList_New(0);
PyObject *result;
-
+
if (chunks == NULL)
return NULL;