Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,7 @@ def test_io_after_close(self):
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)
Expand Down
6 changes: 4 additions & 2 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -658,36 +658,45 @@ _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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is leaked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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 */
}

if (PyList_Append(result, line) < 0) {
Py_DECREF(line);
Py_DECREF(result);
return NULL;
goto error;
}
length += PyObject_Size(line);
Py_DECREF(line);

if (length > hint)
break;
}

Py_DECREF(it);
return result;

error:
Py_XDECREF(it);
Py_DECREF(result);
return NULL;
}

/*[clinic input]
Expand Down