]> granicus.if.org Git - python/commitdiff
file_getiter(): make iter(file) be equivalent to file.xreadlines().
authorGuido van Rossum <guido@python.org>
Tue, 22 May 2001 16:48:37 +0000 (16:48 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 22 May 2001 16:48:37 +0000 (16:48 +0000)
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

index 70833845bca6635ca63428557d941586762d420c..c4b10dcd3e1b7b51ae6309ab2f75631f59685574 100644 (file)
@@ -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 */
 };