Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6596725

Browse files
committed
Oops, forgot to merge this from the iter-branch to the trunk.
This adds "for line in file" iteration, as promised.
1 parent a3f98d6 commit 6596725

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

Objects/fileobject.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,18 +1297,49 @@ file_setattr(PyFileObject *f, char *name, PyObject *v)
12971297
return PyMember_Set((char *)f, file_memberlist, name, v);
12981298
}
12991299

1300+
static PyObject *
1301+
file_getiter(PyFileObject *f)
1302+
{
1303+
static PyObject *es;
1304+
PyObject *iter;
1305+
PyObject *rl = Py_FindMethod(file_methods, (PyObject *)f, "readline");
1306+
if (rl == NULL)
1307+
return NULL;
1308+
if (es == NULL)
1309+
es = PyString_FromString("");
1310+
iter = PyCallIter_New(rl, es);
1311+
Py_DECREF(rl);
1312+
return iter;
1313+
}
1314+
13001315
PyTypeObject PyFile_Type = {
13011316
PyObject_HEAD_INIT(&PyType_Type)
13021317
0,
13031318
"file",
13041319
sizeof(PyFileObject),
13051320
0,
1306-
(destructor)file_dealloc, /*tp_dealloc*/
1307-
0, /*tp_print*/
1308-
(getattrfunc)file_getattr, /*tp_getattr*/
1309-
(setattrfunc)file_setattr, /*tp_setattr*/
1310-
0, /*tp_compare*/
1311-
(reprfunc)file_repr, /*tp_repr*/
1321+
(destructor)file_dealloc, /* tp_dealloc */
1322+
0, /* tp_print */
1323+
(getattrfunc)file_getattr, /* tp_getattr */
1324+
(setattrfunc)file_setattr, /* tp_setattr */
1325+
0, /* tp_compare */
1326+
(reprfunc)file_repr, /* tp_repr */
1327+
0, /* tp_as_number */
1328+
0, /* tp_as_sequence */
1329+
0, /* tp_as_mapping */
1330+
0, /* tp_hash */
1331+
0, /* tp_call */
1332+
0, /* tp_str */
1333+
0, /* tp_getattro */
1334+
0, /* tp_setattro */
1335+
0, /* tp_as_buffer */
1336+
Py_TPFLAGS_DEFAULT, /* tp_flags */
1337+
0, /* tp_doc */
1338+
0, /* tp_traverse */
1339+
0, /* tp_clear */
1340+
0, /* tp_richcompare */
1341+
0, /* tp_weaklistoffset */
1342+
(getiterfunc)file_getiter, /* tp_iter */
13121343
};
13131344

13141345
/* Interface for the 'soft space' between print items. */
@@ -1477,6 +1508,3 @@ int PyObject_AsFileDescriptor(PyObject *o)
14771508
}
14781509
return fd;
14791510
}
1480-
1481-
1482-

0 commit comments

Comments
 (0)