Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3 | PyDoc_STRVAR(xreadlines_doc, |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 4 | "xreadlines(f)\n\ |
| 5 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 6 | Return an xreadlines object for the file f."); |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 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 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 17 | static PyTypeObject XReadlinesObject_Type; |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 18 | |
| 19 | static void |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 20 | xreadlines_dealloc(PyXReadlinesObject *op) |
| 21 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 22 | Py_XDECREF(op->file); |
| 23 | Py_XDECREF(op->lines); |
| 24 | PyObject_DEL(op); |
| 25 | } |
| 26 | |
| 27 | /* A larger chunk size doesn't seem to make a difference */ |
| 28 | #define CHUNKSIZE 8192 |
| 29 | |
| 30 | static PyXReadlinesObject * |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 31 | newreadlinesobject(PyObject *file) |
| 32 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 33 | PyXReadlinesObject *op; |
| 34 | op = PyObject_NEW(PyXReadlinesObject, &XReadlinesObject_Type); |
| 35 | if (op == NULL) |
| 36 | return NULL; |
| 37 | Py_XINCREF(file); |
| 38 | op->file = file; |
| 39 | op->lines = NULL; |
| 40 | op->abslineno = op->lineno = op->lineslen = 0; |
| 41 | return op; |
| 42 | } |
| 43 | |
| 44 | static PyObject * |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 45 | xreadlines(PyObject *self, PyObject *args) |
| 46 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 47 | PyObject *file; |
| 48 | PyXReadlinesObject *ret; |
| 49 | |
| 50 | if (!PyArg_ParseTuple(args, "O:xreadlines", &file)) |
| 51 | return NULL; |
| 52 | ret = newreadlinesobject(file); |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 53 | return (PyObject*)ret; |
| 54 | } |
| 55 | |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 56 | static PyObject * |
Guido van Rossum | c8555b3 | 2001-05-22 16:41:32 +0000 | [diff] [blame] | 57 | xreadlines_common(PyXReadlinesObject *a) |
Thomas Wouters | 9fdcf4a | 2001-01-11 15:40:39 +0000 | [diff] [blame] | 58 | { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 59 | if (a->lineno >= a->lineslen) { |
| 60 | Py_XDECREF(a->lines); |
| 61 | a->lines = PyObject_CallMethod(a->file, "readlines", "(i)", |
| 62 | CHUNKSIZE); |
| 63 | if (a->lines == NULL) |
| 64 | return NULL; |
| 65 | a->lineno = 0; |
| 66 | if ((a->lineslen = PySequence_Size(a->lines)) < 0) |
| 67 | return NULL; |
| 68 | } |
| 69 | a->abslineno++; |
| 70 | return PySequence_GetItem(a->lines, a->lineno++); |
| 71 | } |
| 72 | |
Guido van Rossum | c8555b3 | 2001-05-22 16:41:32 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 115 | PyDoc_STRVAR(next_doc, "x.next() -> the next line or raise StopIteration"); |
Guido van Rossum | c8555b3 | 2001-05-22 16:41:32 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 128 | static PySequenceMethods xreadlines_as_sequence = { |
| 129 | 0, /*sq_length*/ |
| 130 | 0, /*sq_concat*/ |
| 131 | 0, /*sq_repeat*/ |
| 132 | (intargfunc)xreadlines_item, /*sq_item*/ |
| 133 | }; |
| 134 | |
| 135 | static PyTypeObject XReadlinesObject_Type = { |
Guido van Rossum | 7150a77 | 2001-01-19 00:29:06 +0000 | [diff] [blame] | 136 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 137 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 138 | "xreadlines.xreadlines", |
Neil Schemenauer | 01b66a8 | 2001-08-29 23:50:42 +0000 | [diff] [blame] | 139 | sizeof(PyXReadlinesObject), |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 140 | 0, |
Guido van Rossum | c8555b3 | 2001-05-22 16:41:32 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Guido van Rossum | c8555b3 | 2001-05-22 16:41:32 +0000 | [diff] [blame] | 166 | static PyMethodDef xreadlines_functions[] = { |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 167 | {"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc}, |
| 168 | {NULL, NULL} |
| 169 | }; |
| 170 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 171 | PyMODINIT_FUNC |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 172 | initxreadlines(void) |
| 173 | { |
Guido van Rossum | 7150a77 | 2001-01-19 00:29:06 +0000 | [diff] [blame] | 174 | XReadlinesObject_Type.ob_type = &PyType_Type; |
Fred Drake | dff3a37 | 2001-07-19 21:29:49 +0000 | [diff] [blame] | 175 | Py_InitModule("xreadlines", xreadlines_functions); |
Guido van Rossum | 8ee5243 | 2002-08-06 17:14:04 +0000 | [diff] [blame] | 176 | PyErr_Warn(PyExc_DeprecationWarning, |
| 177 | "xreadlines is deprecated; use 'for line in file'."); |
Guido van Rossum | ea3375d | 2001-01-09 21:46:50 +0000 | [diff] [blame] | 178 | } |