3 PyDoc_STRVAR(xreadlines_doc
,
6 Return an xreadlines object for the file f.");
17 static PyTypeObject XReadlinesObject_Type
;
20 xreadlines_dealloc(PyXReadlinesObject
*op
)
23 Py_XDECREF(op
->lines
);
27 /* A larger chunk size doesn't seem to make a difference */
28 #define CHUNKSIZE 8192
30 static PyXReadlinesObject
*
31 newreadlinesobject(PyObject
*file
)
33 PyXReadlinesObject
*op
;
34 op
= PyObject_NEW(PyXReadlinesObject
, &XReadlinesObject_Type
);
40 op
->abslineno
= op
->lineno
= op
->lineslen
= 0;
45 xreadlines(PyObject
*self
, PyObject
*args
)
48 PyXReadlinesObject
*ret
;
50 if (!PyArg_ParseTuple(args
, "O:xreadlines", &file
))
52 ret
= newreadlinesobject(file
);
53 return (PyObject
*)ret
;
57 xreadlines_common(PyXReadlinesObject
*a
)
59 if (a
->lineno
>= a
->lineslen
) {
61 a
->lines
= PyObject_CallMethod(a
->file
, "readlines", "(i)",
66 if ((a
->lineslen
= PySequence_Size(a
->lines
)) < 0)
70 return PySequence_GetItem(a
->lines
, a
->lineno
++);
74 xreadlines_item(PyXReadlinesObject
*a
, int i
)
76 if (i
!= a
->abslineno
) {
77 PyErr_SetString(PyExc_RuntimeError
,
78 "xreadlines object accessed out of order");
81 return xreadlines_common(a
);
85 xreadlines_getiter(PyXReadlinesObject
*a
)
92 xreadlines_iternext(PyXReadlinesObject
*a
)
96 res
= xreadlines_common(a
);
97 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_IndexError
))
103 xreadlines_next(PyXReadlinesObject
*a
, PyObject
*args
)
107 if (!PyArg_ParseTuple(args
, ""))
109 res
= xreadlines_common(a
);
110 if (res
== NULL
&& PyErr_ExceptionMatches(PyExc_IndexError
))
111 PyErr_SetObject(PyExc_StopIteration
, Py_None
);
115 PyDoc_STRVAR(next_doc
, "x.next() -> the next line or raise StopIteration");
117 static PyMethodDef xreadlines_methods
[] = {
118 {"next", (PyCFunction
)xreadlines_next
, METH_VARARGS
, next_doc
},
123 xreadlines_getattr(PyObject
*a
, char *name
)
125 return Py_FindMethod(xreadlines_methods
, a
, name
);
128 static PySequenceMethods xreadlines_as_sequence
= {
132 (intargfunc
)xreadlines_item
, /*sq_item*/
135 static PyTypeObject XReadlinesObject_Type
= {
136 PyObject_HEAD_INIT(NULL
)
138 "xreadlines.xreadlines",
139 sizeof(PyXReadlinesObject
),
141 (destructor
)xreadlines_dealloc
, /* tp_dealloc */
143 xreadlines_getattr
, /* tp_getattr */
147 0, /* tp_as_number */
148 &xreadlines_as_sequence
, /* tp_as_sequence */
149 0, /* tp_as_mapping */
155 0, /* tp_as_buffer */
156 Py_TPFLAGS_DEFAULT
, /* tp_flags */
160 0, /* tp_richcompare */
161 0, /* tp_weaklistoffset */
162 (getiterfunc
)xreadlines_getiter
, /* tp_iter */
163 (iternextfunc
)xreadlines_iternext
, /* tp_iternext */
166 static PyMethodDef xreadlines_functions
[] = {
167 {"xreadlines", xreadlines
, METH_VARARGS
, xreadlines_doc
},
174 XReadlinesObject_Type
.ob_type
= &PyType_Type
;
175 Py_InitModule("xreadlines", xreadlines_functions
);
176 PyErr_Warn(PyExc_DeprecationWarning
,
177 "xreadlines is deprecated; use 'for line in file'.");