From 5b021848ac728b9815d6c5c5b9622d3956665bbb Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 22 May 2001 16:48:37 +0000 Subject: 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. --- Objects/fileobject.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 7083384..c4b10dc 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -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 */ }; -- cgit v0.12