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

Skip to content

Commit ea3375d

Browse files
committed
Jeff Epler's xreadlines module, with slight reformatting and some
changes for safety and tuning.
1 parent c49480e commit ea3375d

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Modules/Setup.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ _socket socketmodule.c # socket(2)
156156
# Memory-mapped files (also works on Win32).
157157
mmap mmapmodule.c
158158

159+
# Dynamic readlines
160+
xreadlines xreadlinesmodule.c
161+
159162
# Socket module compiled with SSL support; you must edit the SSL variable:
160163
#SSL=/usr/local/ssl
161164
#_socket socketmodule.c \

Modules/xreadlinesmodule.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "Python.h"
2+
3+
static char xreadlines_doc [] =
4+
"xreadlines(f)\n\
5+
\n\
6+
Return an xreadlines object for the file f.";
7+
8+
typedef struct {
9+
PyObject_HEAD
10+
PyObject *file;
11+
PyObject *lines;
12+
int lineslen;
13+
int lineno;
14+
int abslineno;
15+
} PyXReadlinesObject;
16+
17+
staticforward PyTypeObject XReadlinesObject_Type;
18+
19+
static void
20+
xreadlines_dealloc(PyXReadlinesObject *op) {
21+
Py_XDECREF(op->file);
22+
Py_XDECREF(op->lines);
23+
PyObject_DEL(op);
24+
}
25+
26+
/* A larger chunk size doesn't seem to make a difference */
27+
#define CHUNKSIZE 8192
28+
29+
static PyXReadlinesObject *
30+
newreadlinesobject(PyObject *file) {
31+
PyXReadlinesObject *op;
32+
op = PyObject_NEW(PyXReadlinesObject, &XReadlinesObject_Type);
33+
if (op == NULL)
34+
return NULL;
35+
Py_XINCREF(file);
36+
op->file = file;
37+
op->lines = NULL;
38+
op->abslineno = op->lineno = op->lineslen = 0;
39+
return op;
40+
}
41+
42+
static PyObject *
43+
xreadlines(PyObject *self, PyObject *args) {
44+
PyObject *file;
45+
PyXReadlinesObject *ret;
46+
47+
if (!PyArg_ParseTuple(args, "O:xreadlines", &file))
48+
return NULL;
49+
ret = newreadlinesobject(file);
50+
Py_XINCREF(ret);
51+
return (PyObject*)ret;
52+
}
53+
54+
static PyObject*
55+
xreadlines_item(PyXReadlinesObject *a, int i) {
56+
if (i != a->abslineno) {
57+
PyErr_SetString(PyExc_RuntimeError,
58+
"xreadlines object accessed out of order");
59+
return NULL;
60+
}
61+
if (a->lineno >= a->lineslen) {
62+
Py_XDECREF(a->lines);
63+
a->lines = PyObject_CallMethod(a->file, "readlines", "(i)",
64+
CHUNKSIZE);
65+
if (a->lines == NULL)
66+
return NULL;
67+
a->lineno = 0;
68+
if ((a->lineslen = PySequence_Size(a->lines)) < 0)
69+
return NULL;
70+
}
71+
a->abslineno++;
72+
return PySequence_GetItem(a->lines, a->lineno++);
73+
}
74+
75+
static PySequenceMethods xreadlines_as_sequence = {
76+
0, /*sq_length*/
77+
0, /*sq_concat*/
78+
0, /*sq_repeat*/
79+
(intargfunc)xreadlines_item, /*sq_item*/
80+
};
81+
82+
static PyTypeObject XReadlinesObject_Type = {
83+
PyObject_HEAD_INIT(&PyType_Type)
84+
0,
85+
"xreadlines",
86+
sizeof(PyXReadlinesObject) + PyGC_HEAD_SIZE,
87+
0,
88+
(destructor)xreadlines_dealloc, /*tp_dealloc*/
89+
0, /*tp_print*/
90+
0, /*tp_getattr*/
91+
0, /*tp_setattr*/
92+
0, /*tp_compare*/
93+
0, /*tp_repr*/
94+
0, /*tp_as_number*/
95+
&xreadlines_as_sequence, /*tp_as_sequence*/
96+
0, /*tp_as_mapping*/
97+
0, /*tp_hash*/
98+
0, /*tp_call*/
99+
0, /*tp_str*/
100+
0, /*tp_getattro*/
101+
0, /*tp_setattro*/
102+
0, /*tp_as_buffer*/
103+
Py_TPFLAGS_DEFAULT, /*tp_flags*/
104+
0, /* tp_doc */
105+
};
106+
107+
static PyMethodDef xreadlines_methods[] = {
108+
{"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc},
109+
{NULL, NULL}
110+
};
111+
112+
void
113+
initxreadlines(void)
114+
{
115+
PyObject *m;
116+
117+
m = Py_InitModule("xreadlines", xreadlines_methods);
118+
}

0 commit comments

Comments
 (0)