From 534cbb2f2336c6f3fa7ccf769fc5b6c8d93c6306 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Sat, 15 Apr 2017 12:47:28 +0800 Subject: [PATCH 1/2] bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130) --- Lib/test/test_io.py | 1 + Misc/NEWS | 4 +++- Modules/_io/iobase.c | 28 +++++++++++++++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 3d4da46113927b..fb91a2dbca4104 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index acd20159bc93ae..5f1b64bf769f55 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -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. @@ -12349,4 +12352,3 @@ Mac ---- **(For information about older versions, consult the HISTORY file.)** - diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 2fec7e87e23bee..50acf4d4176441 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -589,8 +589,13 @@ PyDoc_STRVAR(iobase_readlines_doc, static PyObject * iobase_readlines(PyObject *self, PyObject *args) { +<<<<<<< HEAD Py_ssize_t hint = -1, length = 0; PyObject *result; +======= + Py_ssize_t length = 0; + PyObject *result, *it = NULL; +>>>>>>> 026435c... bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130) if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) { return NULL; @@ -606,19 +611,22 @@ 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 */ @@ -626,8 +634,7 @@ iobase_readlines(PyObject *self, PyObject *args) 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 +642,14 @@ iobase_readlines(PyObject *self, PyObject *args) if (length > hint) break; } + + Py_DECREF(it); return result; + + error: + Py_XDECREF(it); + Py_DECREF(result); + return NULL; } static PyObject * From b639a586c7e086a889c5c94628fc7f8db27b9e88 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Sat, 15 Apr 2017 13:01:55 +0800 Subject: [PATCH 2/2] resolve conflict --- Modules/_io/iobase.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 50acf4d4176441..066dc8edad6adb 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; @@ -589,13 +589,8 @@ PyDoc_STRVAR(iobase_readlines_doc, static PyObject * iobase_readlines(PyObject *self, PyObject *args) { -<<<<<<< HEAD Py_ssize_t hint = -1, length = 0; - PyObject *result; -======= - Py_ssize_t length = 0; PyObject *result, *it = NULL; ->>>>>>> 026435c... bpo-30068: add missing iter(self) in _io._IOBase.readlines when hint is present (#1130) if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) { return NULL; @@ -837,7 +832,7 @@ rawiobase_readall(PyObject *self, PyObject *args) int r; PyObject *chunks = PyList_New(0); PyObject *result; - + if (chunks == NULL) return NULL;