mirror of
https://github.com/python/cpython
synced 2026-09-29 11:35:29 +03:00
file_getiter(): make iter(file) be equivalent to file.xreadlines().
This should be faster.
This means:
(1) "for line in file:" won't work if the xreadlines module can't be
imported.
(2) The body of "for line in file:" shouldn't use the file directly;
the effects (e.g. of file.readline(), file.seek() or even
file.tell()) would be undefined because of the buffering that goes
on in the xreadlines module.
This commit is contained in:
parent
d992c2c74d
commit
5b021848ac
1 changed files with 3 additions and 12 deletions
|
|
@ -1298,18 +1298,9 @@ file_setattr(PyFileObject *f, char *name, PyObject *v)
|
|||
}
|
||||
|
||||
static PyObject *
|
||||
file_getiter(PyFileObject *f)
|
||||
file_getiter(PyObject *f)
|
||||
{
|
||||
static PyObject *es;
|
||||
PyObject *iter;
|
||||
PyObject *rl = Py_FindMethod(file_methods, (PyObject *)f, "readline");
|
||||
if (rl == NULL)
|
||||
return NULL;
|
||||
if (es == NULL)
|
||||
es = PyString_FromString("");
|
||||
iter = PyCallIter_New(rl, es);
|
||||
Py_DECREF(rl);
|
||||
return iter;
|
||||
return PyObject_CallMethod(f, "xreadlines", "");
|
||||
}
|
||||
|
||||
PyTypeObject PyFile_Type = {
|
||||
|
|
@ -1339,7 +1330,7 @@ PyTypeObject PyFile_Type = {
|
|||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
(getiterfunc)file_getiter, /* tp_iter */
|
||||
file_getiter, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue