]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Oops, forgot to merge this from the iter-branch to the trunk.
authorGuido van Rossum <guido@python.org>
Sat, 21 Apr 2001 13:20:18 +0000 (13:20 +0000)
committerGuido van Rossum <guido@python.org>
Sat, 21 Apr 2001 13:20:18 +0000 (13:20 +0000)
This adds "for line in file" iteration, as promised.

Objects/fileobject.c

index 9af03b7a8e95423d6b453c666576acd1303ad8aa..b6cfb928bbb921276ea458455ea2c98031b49a88 100644 (file)
@@ -1297,18 +1297,49 @@ file_setattr(PyFileObject *f, char *name, PyObject *v)
        return PyMember_Set((char *)f, file_memberlist, name, v);
 }
 
+static PyObject *
+file_getiter(PyFileObject *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;
+}
+
 PyTypeObject PyFile_Type = {
        PyObject_HEAD_INIT(&PyType_Type)
        0,
        "file",
        sizeof(PyFileObject),
        0,
-       (destructor)file_dealloc, /*tp_dealloc*/
-       0,              /*tp_print*/
-       (getattrfunc)file_getattr, /*tp_getattr*/
-       (setattrfunc)file_setattr, /*tp_setattr*/
-       0,              /*tp_compare*/
-       (reprfunc)file_repr, /*tp_repr*/
+       (destructor)file_dealloc,               /* tp_dealloc */
+       0,                                      /* tp_print */
+       (getattrfunc)file_getattr,              /* tp_getattr */
+       (setattrfunc)file_setattr,              /* tp_setattr */
+       0,                                      /* tp_compare */
+       (reprfunc)file_repr,                    /* tp_repr */
+       0,                                      /* tp_as_number */
+       0,                                      /* tp_as_sequence */
+       0,                                      /* tp_as_mapping */
+       0,                                      /* tp_hash */
+       0,                                      /* tp_call */
+       0,                                      /* tp_str */
+       0,                                      /* tp_getattro */
+       0,                                      /* tp_setattro */
+       0,                                      /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT,                     /* tp_flags */
+       0,                                      /* tp_doc */
+       0,                                      /* tp_traverse */
+       0,                                      /* tp_clear */
+       0,                                      /* tp_richcompare */
+       0,                                      /* tp_weaklistoffset */
+       (getiterfunc)file_getiter,              /* tp_iter */
 };
 
 /* Interface for the 'soft space' between print items. */
@@ -1477,6 +1508,3 @@ int PyObject_AsFileDescriptor(PyObject *o)
        }
        return fd;
 }
-
-
-