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 @@ -2933,6 +2933,7 @@ def test_io_after_close(self):
self.assertRaises(ValueError, f.readinto, 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
4 changes: 3 additions & 1 deletion Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Extension Modules
Library
-------

- bpo-30068: _io._IOBase.readlines will check if it's closed first when
hint is present.

- bpo-27863: Fixed multiple crashes in ElementTree caused by race conditions
and wrong types.

Expand Down Expand Up @@ -12349,4 +12352,3 @@ Mac
----

**(For information about older versions, consult the HISTORY file.)**

33 changes: 21 additions & 12 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
@@ -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
*/

Expand All @@ -19,7 +19,7 @@

typedef struct {
PyObject_HEAD

PyObject *dict;
PyObject *weakreflist;
} iobase;
Expand Down Expand Up @@ -590,7 +590,7 @@ static PyObject *
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;
Expand All @@ -606,36 +606,45 @@ iobase_readlines(PyObject *self, PyObject *args)
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 */
}

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;
}

static PyObject *
Expand Down Expand Up @@ -823,7 +832,7 @@ rawiobase_readall(PyObject *self, PyObject *args)
int r;
PyObject *chunks = PyList_New(0);
PyObject *result;

if (chunks == NULL)
return NULL;

Expand Down