annotate Modules/xreadlinesmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 4c856a180458
children f1a49eec7c2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #include "Python.h"
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 static char xreadlines_doc [] =
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 "xreadlines(f)\n\
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 \n\
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 Return an xreadlines object for the file f.";
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 typedef struct {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 PyObject_HEAD
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 PyObject *file;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 PyObject *lines;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 int lineslen;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 int lineno;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 int abslineno;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 } PyXReadlinesObject;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 staticforward PyTypeObject XReadlinesObject_Type;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 static void
15819
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
20 xreadlines_dealloc(PyXReadlinesObject *op)
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
21 {
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Py_XDECREF(op->file);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 Py_XDECREF(op->lines);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 PyObject_DEL(op);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 /* A larger chunk size doesn't seem to make a difference */
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 #define CHUNKSIZE 8192
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 static PyXReadlinesObject *
15819
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
31 newreadlinesobject(PyObject *file)
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
32 {
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 PyXReadlinesObject *op;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 op = PyObject_NEW(PyXReadlinesObject, &XReadlinesObject_Type);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 if (op == NULL)
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 return NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 Py_XINCREF(file);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 op->file = file;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 op->lines = NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 op->abslineno = op->lineno = op->lineslen = 0;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 return op;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 static PyObject *
15819
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
45 xreadlines(PyObject *self, PyObject *args)
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
46 {
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 PyObject *file;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 PyXReadlinesObject *ret;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 if (!PyArg_ParseTuple(args, "O:xreadlines", &file))
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 return NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 ret = newreadlinesobject(file);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 Py_XINCREF(ret);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 return (PyObject*)ret;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
15819
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
57 static PyObject *
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
58 xreadlines_item(PyXReadlinesObject *a, int i)
79770ca15c5a Conform the new module to /the/ C style.
Thomas Wouters <thomas@python.org>
parents: 15791
diff changeset
59 {
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if (i != a->abslineno) {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 PyErr_SetString(PyExc_RuntimeError,
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 "xreadlines object accessed out of order");
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if (a->lineno >= a->lineslen) {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 Py_XDECREF(a->lines);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 a->lines = PyObject_CallMethod(a->file, "readlines", "(i)",
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 CHUNKSIZE);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if (a->lines == NULL)
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 return NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 a->lineno = 0;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if ((a->lineslen = PySequence_Size(a->lines)) < 0)
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return NULL;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 a->abslineno++;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return PySequence_GetItem(a->lines, a->lineno++);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 static PySequenceMethods xreadlines_as_sequence = {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 0, /*sq_length*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 0, /*sq_concat*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 0, /*sq_repeat*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 (intargfunc)xreadlines_item, /*sq_item*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 };
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 static PyTypeObject XReadlinesObject_Type = {
16008
4c856a180458 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15819
diff changeset
87 PyObject_HEAD_INIT(NULL)
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 0,
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 "xreadlines",
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 sizeof(PyXReadlinesObject) + PyGC_HEAD_SIZE,
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 0,
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 (destructor)xreadlines_dealloc, /*tp_dealloc*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 0, /*tp_print*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 0, /*tp_getattr*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 0, /*tp_setattr*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 0, /*tp_compare*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 0, /*tp_repr*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 0, /*tp_as_number*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 &xreadlines_as_sequence, /*tp_as_sequence*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 0, /*tp_as_mapping*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 0, /*tp_hash*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 0, /*tp_call*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 0, /*tp_str*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 0, /*tp_getattro*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 0, /*tp_setattro*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 0, /*tp_as_buffer*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 Py_TPFLAGS_DEFAULT, /*tp_flags*/
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 0, /* tp_doc */
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 };
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 static PyMethodDef xreadlines_methods[] = {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {"xreadlines", xreadlines, METH_VARARGS, xreadlines_doc},
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {NULL, NULL}
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 };
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
15791
4854ee585e0d Assorted xreadlines problems:
Tim Peters <tim.peters@gmail.com>
parents: 15778
diff changeset
116 DL_EXPORT(void)
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 initxreadlines(void)
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 {
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 PyObject *m;
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
16008
4c856a180458 SF Patch #103185, by jlt63: Some more standard modules cleanup for Cygwin
Guido van Rossum <guido@python.org>
parents: 15819
diff changeset
121 XReadlinesObject_Type.ob_type = &PyType_Type;
15778
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 m = Py_InitModule("xreadlines", xreadlines_methods);
b5a37bf9d12d Jeff Epler's xreadlines module, with slight reformatting and some
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 }