Mercurial > cpython-lru_cache-weakref
annotate Modules/cStringIO.c @ 27210:428eecdfd57f legacy-trunk
Get build working on Redhat 7.2 linux 2.4.7
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Fri, 10 Jan 2003 21:27:54 +0000 |
| parents | 23280a64288e |
| children | 0ea5991f6edb |
| rev | line source |
|---|---|
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
1 |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
2 #include "Python.h" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
3 #include "import.h" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
4 #include "cStringIO.h" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
5 |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
6 PyDoc_STRVAR(cStringIO_module_documentation, |
| 4598 | 7 "A simple fast partial StringIO replacement.\n" |
| 8 "\n" | |
| 9 "This module provides a simple useful replacement for\n" | |
| 10 "the StringIO module that is written in C. It does not provide the\n" | |
|
12959
2513cd74147c
Fixed docstring typo, reported by Skip Montanaro <skip@mojam.com>.
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
11 "full generality of StringIO, but it provides enough for most\n" |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13576
diff
changeset
|
12 "applications and is especially useful in conjunction with the\n" |
| 4598 | 13 "pickle module.\n" |
| 14 "\n" | |
| 15 "Usage:\n" | |
| 16 "\n" | |
| 17 " from cStringIO import StringIO\n" | |
| 18 "\n" | |
| 19 " an_output_stream=StringIO()\n" | |
| 20 " an_output_stream.write(some_stuff)\n" | |
| 21 " ...\n" | |
|
22218
5440e4b17aff
backport jhylton's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21313
diff
changeset
|
22 " value=an_output_stream.getvalue()\n" |
| 4598 | 23 "\n" |
| 24 " an_input_stream=StringIO(a_string)\n" | |
| 25 " spam=an_input_stream.readline()\n" | |
| 26 " spam=an_input_stream.read(5)\n" | |
|
9521
0f27257fb6cf
New version from Jim Fulton:
Guido van Rossum <guido@python.org>
parents:
8741
diff
changeset
|
27 " an_input_stream.seek(0) # OK, start over\n" |
| 4598 | 28 " spam=an_input_stream.read() # and read it all\n" |
| 29 " \n" | |
| 30 "If someone else wants to provide a more complete implementation,\n" | |
| 31 "go for it. :-) \n" | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
32 "\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
33 "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"); |
| 4598 | 34 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
35 #define UNLESS(E) if (!(E)) |
| 4598 | 36 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
37 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
38 /* Declaration for file-like objects that manage data as strings |
| 4598 | 39 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
40 The IOobject type should be though of as a common base type for |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
41 Iobjects, which provide input (read-only) StringIO objects and |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
42 Oobjects, which provide read-write objects. Most of the methods |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
43 depend only on common data. |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
44 */ |
| 4598 | 45 |
| 46 typedef struct { | |
| 47 PyObject_HEAD | |
| 48 char *buf; | |
| 9642 | 49 int pos, string_size; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
50 } IOobject; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
51 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
52 #define IOOOBJECT(O) ((IOobject*)(O)) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
53 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
54 /* Declarations for objects of type StringO */ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
55 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
56 typedef struct { /* Subtype of IOobject */ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
57 PyObject_HEAD |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
58 char *buf; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
59 int pos, string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
60 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
61 int buf_size, softspace; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
62 } Oobject; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
63 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
64 /* Declarations for objects of type StringI */ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
65 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
66 typedef struct { /* Subtype of IOobject */ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
67 PyObject_HEAD |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
68 char *buf; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
69 int pos, string_size; |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20033
diff
changeset
|
70 /* We store a reference to the object here in order to keep |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20033
diff
changeset
|
71 the buffer alive during the lifetime of the Iobject. */ |
| 4598 | 72 PyObject *pbuf; |
| 73 } Iobject; | |
| 74 | |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
75 /* IOobject (common) methods */ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
76 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
77 PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing."); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
78 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
79 static int |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
80 IO__opencheck(IOobject *self) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
81 UNLESS (self->buf) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
82 PyErr_SetString(PyExc_ValueError, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
83 "I/O operation on closed file"); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
84 return 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
85 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
86 return 1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
87 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
88 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
89 static PyObject * |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
90 IO_flush(IOobject *self, PyObject *unused) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
91 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
92 UNLESS (IO__opencheck(self)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
93 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
94 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
95 return Py_None; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
96 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
97 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
98 PyDoc_STRVAR(IO_getval__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
99 "getvalue([use_pos]) -- Get the string value." |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
100 "\n" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
101 "If use_pos is specified and is a true value, then the string returned\n" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
102 "will include only the text up to the current file position.\n"); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
103 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
104 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
105 IO_cgetval(PyObject *self) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
106 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
107 return PyString_FromStringAndSize(((IOobject*)self)->buf, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
108 ((IOobject*)self)->pos); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
109 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
110 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
111 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
112 IO_getval(IOobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
113 PyObject *use_pos=Py_None; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
114 int s; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
115 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
116 UNLESS (IO__opencheck(self)) return NULL; |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
117 UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
118 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
119 if (PyObject_IsTrue(use_pos)) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
120 s=self->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
121 if (s > self->string_size) s=self->string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
122 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
123 else |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
124 s=self->string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
125 return PyString_FromStringAndSize(self->buf, s); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
126 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
127 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
128 PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0"); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
129 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
130 static PyObject * |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
131 IO_isatty(IOobject *self, PyObject *unused) { |
|
25087
9a178adb4e2d
SF bug 601775 - some int results that should be bool.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
132 Py_INCREF(Py_False); |
|
9a178adb4e2d
SF bug 601775 - some int results that should be bool.
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
133 return Py_False; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
134 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
135 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
136 PyDoc_STRVAR(IO_read__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
137 "read([s]) -- Read s characters, or the rest of the string"); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
138 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
139 static int |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
140 IO_cread(PyObject *self, char **output, int n) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
141 int l; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
142 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
143 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
144 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
145 if (n < 0 || n > l) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
146 n = l; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
147 if (n < 0) n=0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
148 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
149 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
150 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
151 ((IOobject*)self)->pos += n; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
152 return n; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
153 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
154 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
155 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
156 IO_read(IOobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
157 int n = -1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
158 char *output; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
159 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
160 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
161 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
162 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
163 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
164 return PyString_FromStringAndSize(output, n); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
165 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
166 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
167 PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line"); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
168 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
169 static int |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
170 IO_creadline(PyObject *self, char **output) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
171 char *n, *s; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
172 int l; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
173 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
174 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
175 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
176 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
177 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
178 n < s && *n != '\n'; n++); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
179 if (n < s) n++; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
180 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
181 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
182 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
183 ((IOobject*)self)->pos += l; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
184 return l; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
185 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
186 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
187 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
188 IO_readline(IOobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
189 int n, m=-1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
190 char *output; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
191 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
192 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
193 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
194 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
195 if (m >= 0 && m < n) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
196 m = n - m; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
197 n -= m; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
198 self->pos -= m; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
199 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
200 return PyString_FromStringAndSize(output, n); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
201 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
202 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
203 PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines"); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
204 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
205 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
206 IO_readlines(IOobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
207 int n; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
208 char *output; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
209 PyObject *result, *line; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
210 int hint = 0, length = 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
211 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
212 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
213 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
214 result = PyList_New(0); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
215 if (!result) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
216 return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
217 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
218 while (1){ |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
219 if ( (n = IO_creadline((PyObject*)self,&output)) < 0) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
220 goto err; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
221 if (n == 0) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
222 break; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
223 line = PyString_FromStringAndSize (output, n); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
224 if (!line) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
225 goto err; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
226 PyList_Append (result, line); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
227 Py_DECREF (line); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
228 length += n; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
229 if (hint > 0 && length >= hint) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
230 break; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
231 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
232 return result; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
233 err: |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
234 Py_DECREF(result); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
235 return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
236 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
237 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
238 PyDoc_STRVAR(IO_reset__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
239 "reset() -- Reset the file position to the beginning"); |
| 4598 | 240 |
| 241 static PyObject * | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
242 IO_reset(IOobject *self, PyObject *unused) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
243 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
244 UNLESS (IO__opencheck(self)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
245 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
246 self->pos = 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
247 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
248 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
249 return Py_None; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
250 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
251 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
252 PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position."); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
253 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
254 static PyObject * |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
255 IO_tell(IOobject *self, PyObject *unused) { |
| 4598 | 256 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
257 UNLESS (IO__opencheck(self)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
258 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
259 return PyInt_FromLong(self->pos); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
260 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
261 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
262 PyDoc_STRVAR(IO_truncate__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
263 "truncate(): truncate the file at the current position."); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
264 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
265 static PyObject * |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
266 IO_truncate(IOobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
267 int pos = -1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
268 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
269 UNLESS (IO__opencheck(self)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
270 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
271 if (pos < 0) pos = self->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
272 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
273 if (self->string_size > pos) self->string_size = pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
274 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
275 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
276 return Py_None; |
| 4598 | 277 } |
| 278 | |
| 279 | |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
280 |
| 4598 | 281 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
282 /* Read-write object methods */ |
| 4598 | 283 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
284 PyDoc_STRVAR(O_seek__doc__, |
| 4598 | 285 "seek(position) -- set the current position\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
286 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"); |
| 4598 | 287 |
| 288 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
289 O_seek(Oobject *self, PyObject *args) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
290 int position, mode = 0; |
| 4598 | 291 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
292 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
293 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
294 return NULL; |
| 4598 | 295 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
296 if (mode == 2) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
297 position += self->string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
298 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
299 else if (mode == 1) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
300 position += self->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
301 } |
| 4598 | 302 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
303 if (position > self->buf_size) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
304 self->buf_size*=2; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
305 if (self->buf_size <= position) self->buf_size=position+1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
306 UNLESS (self->buf=(char*) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
307 realloc(self->buf,self->buf_size*sizeof(char))) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
308 self->buf_size=self->pos=0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
309 return PyErr_NoMemory(); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
310 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
311 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
312 else if (position < 0) position=0; |
| 4598 | 313 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
314 self->pos=position; |
|
14679
08823099ae62
Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents:
13817
diff
changeset
|
315 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
316 while (--position >= self->string_size) self->buf[position]=0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
317 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
318 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
319 return Py_None; |
|
14679
08823099ae62
Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents:
13817
diff
changeset
|
320 } |
|
08823099ae62
Implement readlines function. Closes Bug #110686.
Martin v. Löwis <martin@v.loewis.de>
parents:
13817
diff
changeset
|
321 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
322 PyDoc_STRVAR(O_write__doc__, |
| 4598 | 323 "write(s) -- Write a string to the file" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
324 "\n\nNote (hack:) writing None resets the buffer"); |
| 4598 | 325 |
| 326 | |
| 327 static int | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
328 O_cwrite(PyObject *self, char *c, int l) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
329 int newl; |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
330 Oobject *oself; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
331 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
332 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
333 oself = (Oobject *)self; |
| 4906 | 334 |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
335 newl = oself->pos+l; |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
336 if (newl >= oself->buf_size) { |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
337 oself->buf_size *= 2; |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
338 if (oself->buf_size <= newl) |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
339 oself->buf_size = newl+1; |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
340 UNLESS (oself->buf = |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
341 (char*)realloc(oself->buf, |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
342 (oself->buf_size) * sizeof(char))) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
343 PyErr_SetString(PyExc_MemoryError,"out of memory"); |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
344 oself->buf_size = oself->pos = 0; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
345 return -1; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
346 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
347 } |
| 4598 | 348 |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
349 memcpy(oself->buf+oself->pos,c,l); |
| 4598 | 350 |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
351 oself->pos += l; |
| 4598 | 352 |
|
21294
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
353 if (oself->string_size < oself->pos) { |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
354 oself->string_size = oself->pos; |
|
24a6c3fd0e58
O_cwrite(): rewrote for clarity, replacing all the (Oobject *)self
Guido van Rossum <guido@python.org>
parents:
20060
diff
changeset
|
355 } |
| 4598 | 356 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
357 return l; |
| 4598 | 358 } |
| 359 | |
| 360 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
361 O_write(Oobject *self, PyObject *args) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
362 char *c; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
363 int l; |
| 4598 | 364 |
|
23301
5732a9cc91c7
See discussion at SF bug 547537.
Guido van Rossum <guido@python.org>
parents:
22805
diff
changeset
|
365 UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL; |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
366 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
367 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL; |
| 4598 | 368 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
369 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
370 return Py_None; |
| 4598 | 371 } |
| 372 | |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
373 PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held."); |
| 4598 | 374 |
| 375 static PyObject * | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
376 O_close(Oobject *self, PyObject *unused) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
377 if (self->buf != NULL) free(self->buf); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
378 self->buf = NULL; |
| 4598 | 379 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
380 self->pos = self->string_size = self->buf_size = 0; |
| 4598 | 381 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
382 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
383 return Py_None; |
| 4598 | 384 } |
| 385 | |
| 386 | |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
387 PyDoc_STRVAR(O_writelines__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
388 "writelines(sequence_of_strings): write each string"); |
| 4598 | 389 static PyObject * |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
390 O_writelines(Oobject *self, PyObject *args) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
391 PyObject *tmp = 0; |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
392 static PyObject *joiner = NULL; |
| 4598 | 393 |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
394 if (!joiner) { |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
395 PyObject *empty_string = PyString_FromString(""); |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
396 if (empty_string == NULL) |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
397 return NULL; |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
398 joiner = PyObject_GetAttrString(empty_string, "join"); |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
399 Py_DECREF(empty_string); |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
400 if (joiner == NULL) |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
401 return NULL; |
|
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
402 } |
| 4598 | 403 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
404 if (PyObject_Size(args) < 0) return NULL; |
| 4598 | 405 |
|
16546
60a2e99c863b
In O_writelines: Replace use of string.joinfields with "".join.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15059
diff
changeset
|
406 tmp = PyObject_CallFunction(joiner, "O", args); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
407 UNLESS (tmp) return NULL; |
| 4598 | 408 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
409 args = Py_BuildValue("(O)", tmp); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
410 Py_DECREF(tmp); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
411 UNLESS (args) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
412 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
413 tmp = O_write(self, args); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
414 Py_DECREF(args); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
415 return tmp; |
| 4598 | 416 } |
| 417 | |
| 418 static struct PyMethodDef O_methods[] = { | |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
419 /* Common methods: */ |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
420 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
421 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
422 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
423 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
424 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
425 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
426 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__}, |
|
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
427 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
428 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
429 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
430 /* Read-write StringIO specific methods: */ |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
431 {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__}, |
| 9642 | 432 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
433 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__}, |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
434 {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__}, |
| 9642 | 435 {NULL, NULL} /* sentinel */ |
| 4598 | 436 }; |
| 437 | |
| 438 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
439 O_dealloc(Oobject *self) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
440 if (self->buf != NULL) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
441 free(self->buf); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
442 PyObject_Del(self); |
| 4598 | 443 } |
| 444 | |
| 445 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
446 O_getattr(Oobject *self, char *name) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
447 if (strcmp(name, "softspace") == 0) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
448 return PyInt_FromLong(self->softspace); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
449 } |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
450 return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 4598 | 451 } |
| 452 | |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
453 static int |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
454 O_setattr(Oobject *self, char *name, PyObject *value) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
455 long x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
456 if (strcmp(name, "softspace") != 0) { |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
457 PyErr_SetString(PyExc_AttributeError, name); |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
458 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
459 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
460 x = PyInt_AsLong(value); |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
461 if (x < 0 && PyErr_Occurred()) |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
462 return -1; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
463 self->softspace = x; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
464 return 0; |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
465 } |
|
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
466 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
467 PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings."); |
| 4598 | 468 |
| 469 static PyTypeObject Otype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
470 PyObject_HEAD_INIT(NULL) |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
471 0, /*ob_size*/ |
|
21313
91dd223829c6
Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents:
21294
diff
changeset
|
472 "cStringIO.StringO", /*tp_name*/ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
473 sizeof(Oobject), /*tp_basicsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
474 0, /*tp_itemsize*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
475 /* methods */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
476 (destructor)O_dealloc, /*tp_dealloc*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
477 (printfunc)0, /*tp_print*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
478 (getattrfunc)O_getattr, /*tp_getattr*/ |
|
5315
65c9e90d8142
My own patch: support writable 'softspace' attribute.
Guido van Rossum <guido@python.org>
parents:
5280
diff
changeset
|
479 (setattrfunc)O_setattr, /*tp_setattr*/ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
480 (cmpfunc)0, /*tp_compare*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
481 (reprfunc)0, /*tp_repr*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
482 0, /*tp_as_number*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
483 0, /*tp_as_sequence*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
484 0, /*tp_as_mapping*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
485 (hashfunc)0, /*tp_hash*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
486 (ternaryfunc)0, /*tp_call*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
487 (reprfunc)0, /*tp_str*/ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
488 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
489 /* Space for future expansion */ |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
490 0L,0L,0L,0L, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
491 Otype__doc__ /* Documentation string */ |
| 4598 | 492 }; |
| 493 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
494 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
495 newOobject(int size) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
496 Oobject *self; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
497 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
498 self = PyObject_New(Oobject, &Otype); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
499 if (self == NULL) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
500 return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
501 self->pos=0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
502 self->string_size = 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
503 self->softspace = 0; |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
504 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
505 UNLESS (self->buf=malloc(size*sizeof(char))) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
506 PyErr_SetString(PyExc_MemoryError,"out of memory"); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
507 self->buf_size = 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
508 return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
509 } |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
510 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
511 self->buf_size=size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
512 return (PyObject*)self; |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
513 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
514 |
| 4598 | 515 /* End of code for StringO objects */ |
| 516 /* -------------------------------------------------------- */ | |
| 517 | |
| 518 static PyObject * | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
519 I_close(Iobject *self, PyObject *unused) { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
520 Py_XDECREF(self->pbuf); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
521 self->pbuf = NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
522 self->buf = NULL; |
| 9642 | 523 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
524 self->pos = self->string_size = 0; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
525 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
526 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
527 return Py_None; |
| 9642 | 528 } |
| 529 | |
| 530 static PyObject * | |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
531 I_seek(Iobject *self, PyObject *args) { |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
532 int position, mode = 0; |
| 9642 | 533 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
534 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
535 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
536 return NULL; |
| 9642 | 537 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
538 if (mode == 2) position += self->string_size; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
539 else if (mode == 1) position += self->pos; |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
540 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
541 if (position < 0) position=0; |
| 9642 | 542 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
543 self->pos=position; |
| 4598 | 544 |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
545 Py_INCREF(Py_None); |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
546 return Py_None; |
| 4598 | 547 } |
| 548 | |
| 549 static struct PyMethodDef I_methods[] = { | |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
550 /* Common methods: */ |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
551 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
552 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
553 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
554 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
555 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
556 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
557 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__}, |
|
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
558 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__}, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
559 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
560 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
561 /* Read-only StringIO specific methods: */ |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
562 {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__}, |
| 9642 | 563 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__}, |
| 564 {NULL, NULL} | |
| 4598 | 565 }; |
| 566 | |
| 567 static void | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
568 I_dealloc(Iobject *self) { |
|
6215
eb7a01b86f93
Fix the bug Jeremy was experiencing: both the close() and the
Guido van Rossum <guido@python.org>
parents:
5991
diff
changeset
|
569 Py_XDECREF(self->pbuf); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12385
diff
changeset
|
570 PyObject_Del(self); |
| 4598 | 571 } |
| 572 | |
| 573 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
574 I_getattr(Iobject *self, char *name) { |
| 4598 | 575 return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 576 } | |
| 577 | |
|
20033
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
578 static PyObject * |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
579 I_getiter(Iobject *self) |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
580 { |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
581 PyObject *myreadline = PyObject_GetAttrString((PyObject*)self, |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
582 "readline"); |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
583 PyObject *emptystring = PyString_FromString(""); |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
584 PyObject *iter = NULL; |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
585 if (!myreadline || !emptystring) |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
586 goto finally; |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
587 |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
588 iter = PyCallIter_New(myreadline, emptystring); |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
589 finally: |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
590 Py_XDECREF(myreadline); |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
591 Py_XDECREF(emptystring); |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
592 return iter; |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
593 } |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
594 |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
595 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
596 PyDoc_STRVAR(Itype__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
597 "Simple type for treating strings as input file streams"); |
| 4598 | 598 |
| 599 static PyTypeObject Itype = { | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
600 PyObject_HEAD_INIT(NULL) |
|
20033
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
601 0, /*ob_size*/ |
|
21313
91dd223829c6
Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents:
21294
diff
changeset
|
602 "cStringIO.StringI", /*tp_name*/ |
|
20033
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
603 sizeof(Iobject), /*tp_basicsize*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
604 0, /*tp_itemsize*/ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
605 /* methods */ |
|
20033
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
606 (destructor)I_dealloc, /*tp_dealloc*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
607 (printfunc)0, /*tp_print*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
608 (getattrfunc)I_getattr, /*tp_getattr*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
609 (setattrfunc)0, /*tp_setattr*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
610 (cmpfunc)0, /*tp_compare*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
611 (reprfunc)0, /*tp_repr*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
612 0, /*tp_as_number*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
613 0, /*tp_as_sequence*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
614 0, /*tp_as_mapping*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
615 (hashfunc)0, /*tp_hash*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
616 (ternaryfunc)0, /*tp_call*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
617 (reprfunc)0, /*tp_str*/ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
618 0, /* tp_getattro */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
619 0, /* tp_setattro */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
620 0, /* tp_as_buffer */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
621 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
622 Itype__doc__, /* tp_doc */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
623 0, /* tp_traverse */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
624 0, /* tp_clear */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
625 0, /* tp_richcompare */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
626 0, /* tp_weaklistoffset */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
627 (getiterfunc)I_getiter, /* tp_iter */ |
|
104b5c9b88ea
I_getiter(): Function for the tp_iter slot of Itype so that
Barry Warsaw <barry@python.org>
parents:
16546
diff
changeset
|
628 0, /* tp_iternext */ |
| 4598 | 629 }; |
| 630 | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
631 static PyObject * |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
632 newIobject(PyObject *s) { |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
633 Iobject *self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
634 char *buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
635 int size; |
|
12385
627226df9f01
raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11882
diff
changeset
|
636 |
|
20060
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20033
diff
changeset
|
637 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) { |
|
ccec02bfe9a0
StringIO patch #462596: let's [c]StringIO accept read buffers on
Marc-André Lemburg <mal@egenix.com>
parents:
20033
diff
changeset
|
638 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found", |
|
12385
627226df9f01
raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11882
diff
changeset
|
639 s->ob_type->tp_name); |
|
627226df9f01
raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11882
diff
changeset
|
640 return NULL; |
|
627226df9f01
raise TypeError when bad argument passed to cStringIO.StringIO
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11882
diff
changeset
|
641 } |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
642 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL; |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
643 Py_INCREF(s); |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
644 self->buf=buf; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
645 self->string_size=size; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
646 self->pbuf=s; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
647 self->pos=0; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
648 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
649 return (PyObject*)self; |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
650 } |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
651 |
| 4598 | 652 /* End of code for StringI objects */ |
| 653 /* -------------------------------------------------------- */ | |
| 654 | |
| 655 | |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
656 PyDoc_STRVAR(IO_StringIO__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23301
diff
changeset
|
657 "StringIO([s]) -- Return a StringIO-like stream for reading or writing"); |
| 4598 | 658 |
| 659 static PyObject * | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
660 IO_StringIO(PyObject *self, PyObject *args) { |
| 4598 | 661 PyObject *s=0; |
| 662 | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
25087
diff
changeset
|
663 if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL; |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
664 |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
665 if (s) return newIobject(s); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
666 return newOobject(128); |
| 4598 | 667 } |
| 668 | |
| 669 /* List of methods defined in the module */ | |
| 670 | |
| 671 static struct PyMethodDef IO_methods[] = { | |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13690
diff
changeset
|
672 {"StringIO", (PyCFunction)IO_StringIO, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13690
diff
changeset
|
673 METH_VARARGS, IO_StringIO__doc__}, |
| 4906 | 674 {NULL, NULL} /* sentinel */ |
| 4598 | 675 }; |
| 676 | |
| 677 | |
| 678 /* Initialization function for the module (*must* be called initcStringIO) */ | |
| 679 | |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
680 static struct PycStringIO_CAPI CAPI = { |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
681 IO_cread, |
|
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
682 IO_creadline, |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
683 O_cwrite, |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
684 IO_cgetval, |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
685 newOobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
686 newIobject, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
687 &Itype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
688 &Otype, |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
689 }; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
690 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
691 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
|
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
692 #define PyMODINIT_FUNC void |
| 9642 | 693 #endif |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
694 PyMODINIT_FUNC |
|
13690
c09c0171474f
ANSIfy some more forward declarations.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
695 initcStringIO(void) { |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
696 PyObject *m, *d, *v; |
| 4598 | 697 |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
698 |
| 4598 | 699 /* Create the module and add the functions */ |
| 700 m = Py_InitModule4("cStringIO", IO_methods, | |
| 701 cStringIO_module_documentation, | |
| 702 (PyObject*)NULL,PYTHON_API_VERSION); | |
| 703 | |
| 704 /* Add some symbolic constants to the module */ | |
| 705 d = PyModule_GetDict(m); | |
| 706 | |
| 4906 | 707 /* Export C API */ |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
708 Itype.ob_type=&PyType_Type; |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
709 Otype.ob_type=&PyType_Type; |
|
6227
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
710 PyDict_SetItemString(d,"cStringIO_CAPI", |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
711 v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
|
613f976a57b4
#Plug small memory leaks in constructors.
Guido van Rossum <guido@python.org>
parents:
6215
diff
changeset
|
712 Py_XDECREF(v); |
|
5280
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
713 |
|
e8f8cf358054
Unknown changes by Jim Fulton.
Guido van Rossum <guido@python.org>
parents:
4957
diff
changeset
|
714 /* Export Types */ |
| 4598 | 715 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 716 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); | |
|
5991
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
717 |
|
a5737dd96979
cPickle release 0.3 from Jim Fulton
Guido van Rossum <guido@python.org>
parents:
5315
diff
changeset
|
718 /* Maybe make certain warnings go away */ |
|
15059
acb88607b4bd
Added a new "base" type, IOobject for which most of the
Jim Fulton <jim@zope.com>
parents:
14679
diff
changeset
|
719 if (0) PycString_IMPORT; |
| 4598 | 720 } |
