bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130) (#1150)

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 @@
 /*[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 @@
         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 @@
 
         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 @@
         if (length > hint)
             break;
     }
+
+    Py_DECREF(it);
     return result;
+
+ error:
+    Py_XDECREF(it);
+    Py_DECREF(result);
+    return NULL;
 }
 
 /*[clinic input]