11/*
22 An implementation of the I/O abstract base classes hierarchy
33 as defined by PEP 3116 - "New I/O"
4-
4+
55 Classes defined here: IOBase, RawIOBase.
6-
6+
77 Written by Amaury Forgeot d'Arc and Antoine Pitrou
88*/
99
1919
2020typedef struct {
2121 PyObject_HEAD
22-
22+
2323 PyObject * dict ;
2424 PyObject * weakreflist ;
2525} iobase ;
@@ -590,7 +590,7 @@ static PyObject *
590590iobase_readlines (PyObject * self , PyObject * args )
591591{
592592 Py_ssize_t hint = -1 , length = 0 ;
593- PyObject * result ;
593+ PyObject * result , * it = NULL ;
594594
595595 if (!PyArg_ParseTuple (args , "|O&:readlines" , & _PyIO_ConvertSsize_t , & hint )) {
596596 return NULL ;
@@ -606,36 +606,45 @@ iobase_readlines(PyObject *self, PyObject *args)
606606 probably be removed here. */
607607 PyObject * ret = PyObject_CallMethod (result , "extend" , "O" , self );
608608 if (ret == NULL ) {
609- Py_DECREF (result );
610- return NULL ;
609+ goto error ;
611610 }
612611 Py_DECREF (ret );
613612 return result ;
614613 }
615614
615+ it = PyObject_GetIter (self );
616+ if (it == NULL ) {
617+ goto error ;
618+ }
619+
616620 while (1 ) {
617- PyObject * line = PyIter_Next (self );
621+ PyObject * line = PyIter_Next (it );
618622 if (line == NULL ) {
619623 if (PyErr_Occurred ()) {
620- Py_DECREF (result );
621- return NULL ;
624+ goto error ;
622625 }
623626 else
624627 break ; /* StopIteration raised */
625628 }
626629
627630 if (PyList_Append (result , line ) < 0 ) {
628631 Py_DECREF (line );
629- Py_DECREF (result );
630- return NULL ;
632+ goto error ;
631633 }
632634 length += PyObject_Size (line );
633635 Py_DECREF (line );
634636
635637 if (length > hint )
636638 break ;
637639 }
640+
641+ Py_DECREF (it );
638642 return result ;
643+
644+ error :
645+ Py_XDECREF (it );
646+ Py_DECREF (result );
647+ return NULL ;
639648}
640649
641650static PyObject *
@@ -823,7 +832,7 @@ rawiobase_readall(PyObject *self, PyObject *args)
823832 int r ;
824833 PyObject * chunks = PyList_New (0 );
825834 PyObject * result ;
826-
835+
827836 if (chunks == NULL )
828837 return NULL ;
829838
0 commit comments