Skip to content
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 @@ -3498,6 +3498,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
8 changes: 5 additions & 3 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 17 additions & 8 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -664,36 +664,45 @@ _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 */
}

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