Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8555b3

Browse files
committed
Iterator support: made the xreadlines object its own iterator. This
ought to be faster.
1 parent 6626c1f commit c8555b3

1 file changed

Lines changed: 81 additions & 25 deletions

File tree

Modules/xreadlinesmodule.c

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,8 @@ xreadlines(PyObject *self, PyObject *args)
5454
}
5555

5656
static PyObject *
57-
xreadlines_item(PyXReadlinesObject *a, int i)
57+
xreadlines_common(PyXReadlinesObject *a)
5858
{
59-
if (i != a->abslineno) {
60-
PyErr_SetString(PyExc_RuntimeError,
61-
"xreadlines object accessed out of order");
62-
return NULL;
63-
}
6459
if (a->lineno >= a->lineslen) {
6560
Py_XDECREF(a->lines);
6661
a->lines = PyObject_CallMethod(a->file, "readlines", "(i)",
@@ -75,6 +70,61 @@ xreadlines_item(PyXReadlinesObject *a, int i)
7570
return PySequence_GetItem(a->lines, a->lineno++);
7671
}
7772

73+
static PyObject *
74+
xreadlines_item(PyXReadlinesObject *a, int i)
75+
{
76+
if (i != a->abslineno) {
77+
PyErr_SetString(PyExc_RuntimeError,
78+
"xreadlines object accessed out of order");
79+
return NULL;
80+
}
81+
return xreadlines_common(a);
82+
}
83+
84+
static PyObject *
85+
xreadlines_getiter(PyXReadlinesObject *a)
86+
{
87+
Py_INCREF(a);
88+
return (PyObject *)a;
89+
}
90+
91+
static PyObject *
92+
xreadlines_iternext(PyXReadlinesObject *a)
93+
{
94+
PyObject *res;
95+
96+
res = xreadlines_common(a);
97+
if (res == NULL && PyErr_ExceptionMatches(PyExc_IndexError))
98+
PyErr_Clear();
99+
return res;
100+
}
101+
102+
static PyObject *
103+
xreadlines_next(PyXReadlinesObject *a, PyObject *args)
104+
{
105+
PyObject *res;
106+
107+
if (!PyArg_ParseTuple(args, ""))
108+
return NULL;
109+
res = xreadlines_common(a);
110+
if (res == NULL && PyErr_ExceptionMatches(PyExc_IndexError))
111+
PyErr_SetObject(PyExc_StopIteration, Py_None);
112+
return res;
113+
}
114+
115+
static char next_doc[] = "x.next() -> the next line or raise StopIteration";
116+
117+
static PyMethodDef xreadlines_methods[] = {
118+
{"next", (PyCFunction)xreadlines_next, METH_VARARGS, next_doc},
119+
{NULL, NULL}
120+
};
121+
122+
static PyObject *
123+
xreadlines_getattr(PyObject *a, char *name)
124+
{
125+
return Py_FindMethod(xreadlines_methods, a, name);
126+
}
127+
78128
static PySequenceMethods xreadlines_as_sequence = {
79129
0, /*sq_length*/
80130
0, /*sq_concat*/
@@ -88,26 +138,32 @@ static PyTypeObject XReadlinesObject_Type = {
88138
"xreadlines",
89139
sizeof(PyXReadlinesObject) + PyGC_HEAD_SIZE,
90140
0,
91-
(destructor)xreadlines_dealloc, /*tp_dealloc*/
92-
0, /*tp_print*/
93-
0, /*tp_getattr*/
94-
0, /*tp_setattr*/
95-
0, /*tp_compare*/
96-
0, /*tp_repr*/
97-
0, /*tp_as_number*/
98-
&xreadlines_as_sequence, /*tp_as_sequence*/
99-
0, /*tp_as_mapping*/
100-
0, /*tp_hash*/
101-
0, /*tp_call*/
102-
0, /*tp_str*/
103-
0, /*tp_getattro*/
104-
0, /*tp_setattro*/
105-
0, /*tp_as_buffer*/
106-
Py_TPFLAGS_DEFAULT, /*tp_flags*/
107-
0, /* tp_doc */
141+
(destructor)xreadlines_dealloc, /* tp_dealloc */
142+
0, /* tp_print */
143+
xreadlines_getattr, /* tp_getattr */
144+
0, /* tp_setattr */
145+
0, /* tp_compare */
146+
0, /* tp_repr */
147+
0, /* tp_as_number */
148+
&xreadlines_as_sequence, /* tp_as_sequence */
149+
0, /* tp_as_mapping */
150+
0, /* tp_hash */
151+
0, /* tp_call */
152+
0, /* tp_str */
153+
0, /* tp_getattro */
154+
0, /* tp_setattro */
155+
0, /* tp_as_buffer */
156+
Py_TPFLAGS_DEFAULT, /* tp_flags */
157+
0, /* tp_doc */
158+
0, /* tp_traverse */
159+
0, /* tp_clear */
160+
0, /* tp_richcompare */
161+
0, /* tp_weaklistoffset */
162+
(getiterfunc)xreadlines_getiter, /* tp_iter */
163+
(iternextfunc)xreadlines_iternext, /* tp_iternext */
108164
};
109165

110-
static PyMethodDef xreadlines_methods[] = {
166+
static PyMethodDef xreadlines_functions[] = {
111167
{"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc},
112168
{NULL, NULL}
113169
};
@@ -118,5 +174,5 @@ initxreadlines(void)
118174
PyObject *m;
119175

120176
XReadlinesObject_Type.ob_type = &PyType_Type;
121-
m = Py_InitModule("xreadlines", xreadlines_methods);
177+
m = Py_InitModule("xreadlines", xreadlines_functions);
122178
}

0 commit comments

Comments
 (0)