summaryrefslogtreecommitdiffstats
path: root/Objects/fileobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-04-21 13:20:18 (GMT)
committerGuido van Rossum <guido@python.org>2001-04-21 13:20:18 (GMT)
commit65967259f2db65ad301b9a69ff91af27e7737b15 (patch)
treee96262a89fffa5434dbeb7a0bd5490b775ad784f /Objects/fileobject.c
parenta3f98d6bac3fbfc0e010360b11793bbd8a8f4c85 (diff)
downloadcpython-65967259f2db65ad301b9a69ff91af27e7737b15.zip
cpython-65967259f2db65ad301b9a69ff91af27e7737b15.tar.gz
cpython-65967259f2db65ad301b9a69ff91af27e7737b15.tar.bz2
Oops, forgot to merge this from the iter-branch to the trunk.
This adds "for line in file" iteration, as promised.
Diffstat (limited to 'Objects/fileobject.c')
-rw-r--r--Objects/fileobject.c46
1 files changed, 37 insertions, 9 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 9af03b7..b6cfb92 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -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;
}
-
-
-