diff options
author | Guido van Rossum <guido@python.org> | 2001-05-22 16:48:37 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-05-22 16:48:37 (GMT) |
commit | 5b021848ac728b9815d6c5c5b9622d3956665bbb (patch) | |
tree | c12c5ead6cde6a50e06a1eacf2d5148c78e301db | |
parent | d992c2c74df75509b7120820307e9f26bf90ac06 (diff) | |
download | cpython-5b021848ac728b9815d6c5c5b9622d3956665bbb.zip cpython-5b021848ac728b9815d6c5c5b9622d3956665bbb.tar.gz cpython-5b021848ac728b9815d6c5c5b9622d3956665bbb.tar.bz2 |
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.
-rw-r--r-- | Objects/fileobject.c | 15 |
1 files 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 */ }; |