blob: 4cabc5bb7cfc68dffda9f51c6428d6078400c659 [file] [log] [blame]
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001
2#include "Python.h"
3#include "import.h"
4#include "cStringIO.h"
5
6PyDoc_STRVAR(cStringIO_module_documentation,
Guido van Rossum049cd901996-12-05 23:30:48 +00007"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"
Fred Drakeaef10002000-06-19 13:17:41 +000011"full generality of StringIO, but it provides enough for most\n"
Thomas Wouters7e474022000-07-16 12:04:32 +000012"applications and is especially useful in conjunction with the\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000013"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"
Jeremy Hyltonb189b072002-03-08 17:17:33 +000022" value=an_output_stream.getvalue()\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000023"\n"
24" an_input_stream=StringIO(a_string)\n"
25" spam=an_input_stream.readline()\n"
26" spam=an_input_stream.read(5)\n"
Guido van Rossum7d9b4131998-11-25 16:17:32 +000027" an_input_stream.seek(0) # OK, start over\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000028" 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"
Guido van Rossum142eeb81997-08-13 03:14:41 +000032"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
Guido van Rossum049cd901996-12-05 23:30:48 +000034
Jim Fultone60de4d2000-10-06 19:24:23 +000035#define UNLESS(E) if (!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +000036
Guido van Rossum049cd901996-12-05 23:30:48 +000037
Jim Fultone60de4d2000-10-06 19:24:23 +000038/* Declaration for file-like objects that manage data as strings
Guido van Rossum049cd901996-12-05 23:30:48 +000039
Jim Fultone60de4d2000-10-06 19:24:23 +000040 The IOobject type should be though of as a common base type for
41 Iobjects, which provide input (read-only) StringIO objects and
42 Oobjects, which provide read-write objects. Most of the methods
43 depend only on common data.
44*/
Guido van Rossum049cd901996-12-05 23:30:48 +000045
46typedef struct {
47 PyObject_HEAD
48 char *buf;
Guido van Rossum476e49f1998-12-15 21:43:15 +000049 int pos, string_size;
Jim Fultone60de4d2000-10-06 19:24:23 +000050} IOobject;
51
52#define IOOOBJECT(O) ((IOobject*)(O))
53
54/* Declarations for objects of type StringO */
55
56typedef struct { /* Subtype of IOobject */
57 PyObject_HEAD
58 char *buf;
59 int pos, string_size;
60
61 int buf_size, softspace;
62} Oobject;
63
64/* Declarations for objects of type StringI */
65
66typedef struct { /* Subtype of IOobject */
67 PyObject_HEAD
68 char *buf;
69 int pos, string_size;
Marc-André Lemburge47df7a2001-09-24 17:34:52 +000070 /* We store a reference to the object here in order to keep
71 the buffer alive during the lifetime of the Iobject. */
Guido van Rossum049cd901996-12-05 23:30:48 +000072 PyObject *pbuf;
73} Iobject;
74
Jim Fultone60de4d2000-10-06 19:24:23 +000075/* IOobject (common) methods */
76
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000077PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing.");
Jim Fultone60de4d2000-10-06 19:24:23 +000078
79static int
80IO__opencheck(IOobject *self) {
81 UNLESS (self->buf) {
82 PyErr_SetString(PyExc_ValueError,
83 "I/O operation on closed file");
84 return 0;
85 }
86 return 1;
87}
88
89static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +000090IO_flush(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +000091
92 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +000093
94 Py_INCREF(Py_None);
95 return Py_None;
96}
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(IO_getval__doc__,
99"getvalue([use_pos]) -- Get the string value."
100"\n"
101"If use_pos is specified and is a true value, then the string returned\n"
102"will include only the text up to the current file position.\n");
Guido van Rossum049cd901996-12-05 23:30:48 +0000103
104static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000105IO_cgetval(PyObject *self) {
106 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
107 return PyString_FromStringAndSize(((IOobject*)self)->buf,
108 ((IOobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000109}
110
Guido van Rossum049cd901996-12-05 23:30:48 +0000111static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000112IO_getval(IOobject *self, PyObject *args) {
113 PyObject *use_pos=Py_None;
114 int s;
115
116 UNLESS (IO__opencheck(self)) return NULL;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000117 UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000118
119 if (PyObject_IsTrue(use_pos)) {
120 s=self->pos;
121 if (s > self->string_size) s=self->string_size;
122 }
123 else
124 s=self->string_size;
125 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum049cd901996-12-05 23:30:48 +0000126}
127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000128PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
Guido van Rossum049cd901996-12-05 23:30:48 +0000129
130static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000131IO_isatty(IOobject *self, PyObject *unused) {
Guido van Rossum674deb22002-09-01 15:06:28 +0000132 Py_INCREF(Py_False);
133 return Py_False;
Guido van Rossum049cd901996-12-05 23:30:48 +0000134}
135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000136PyDoc_STRVAR(IO_read__doc__,
137"read([s]) -- Read s characters, or the rest of the string");
Guido van Rossum049cd901996-12-05 23:30:48 +0000138
139static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000140IO_cread(PyObject *self, char **output, int n) {
141 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000142
Jim Fultone60de4d2000-10-06 19:24:23 +0000143 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
144 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
145 if (n < 0 || n > l) {
146 n = l;
147 if (n < 0) n=0;
148 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000149
Jim Fultone60de4d2000-10-06 19:24:23 +0000150 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
151 ((IOobject*)self)->pos += n;
152 return n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000153}
154
155static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000156IO_read(IOobject *self, PyObject *args) {
157 int n = -1;
158 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000159
Jim Fultone60de4d2000-10-06 19:24:23 +0000160 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000161
Jim Fultone60de4d2000-10-06 19:24:23 +0000162 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000163
Jim Fultone60de4d2000-10-06 19:24:23 +0000164 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000165}
166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
Guido van Rossum049cd901996-12-05 23:30:48 +0000168
169static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000170IO_creadline(PyObject *self, char **output) {
171 char *n, *s;
172 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000173
Jim Fultone60de4d2000-10-06 19:24:23 +0000174 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
175
176 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
177 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
178 n < s && *n != '\n'; n++);
179 if (n < s) n++;
180
181 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
182 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
183 ((IOobject*)self)->pos += l;
184 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000185}
186
187static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000188IO_readline(IOobject *self, PyObject *args) {
189 int n, m=-1;
190 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000191
Jim Fultone60de4d2000-10-06 19:24:23 +0000192 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
193
194 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
195 if (m >= 0 && m < n) {
196 m = n - m;
197 n -= m;
198 self->pos -= m;
199 }
200 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000201}
202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000204
205static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000206IO_readlines(IOobject *self, PyObject *args) {
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000207 int n;
208 char *output;
209 PyObject *result, *line;
210 int hint = 0, length = 0;
211
Jim Fultone60de4d2000-10-06 19:24:23 +0000212 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
213
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000214 result = PyList_New(0);
215 if (!result)
216 return NULL;
217
Jim Fultone60de4d2000-10-06 19:24:23 +0000218 while (1){
219 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
220 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000221 if (n == 0)
222 break;
223 line = PyString_FromStringAndSize (output, n);
Jim Fultone60de4d2000-10-06 19:24:23 +0000224 if (!line)
225 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000226 PyList_Append (result, line);
227 Py_DECREF (line);
228 length += n;
229 if (hint > 0 && length >= hint)
230 break;
231 }
232 return result;
Jim Fultone60de4d2000-10-06 19:24:23 +0000233 err:
234 Py_DECREF(result);
235 return NULL;
236}
237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238PyDoc_STRVAR(IO_reset__doc__,
239"reset() -- Reset the file position to the beginning");
Jim Fultone60de4d2000-10-06 19:24:23 +0000240
241static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000242IO_reset(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000243
244 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000245
246 self->pos = 0;
247
248 Py_INCREF(Py_None);
249 return Py_None;
250}
251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000253
254static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000255IO_tell(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000256
257 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000258
259 return PyInt_FromLong(self->pos);
260}
261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000262PyDoc_STRVAR(IO_truncate__doc__,
263"truncate(): truncate the file at the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000264
265static PyObject *
266IO_truncate(IOobject *self, PyObject *args) {
267 int pos = -1;
268
269 UNLESS (IO__opencheck(self)) return NULL;
270 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
271 if (pos < 0) pos = self->pos;
272
273 if (self->string_size > pos) self->string_size = pos;
274
275 Py_INCREF(Py_None);
276 return Py_None;
277}
278
279
280
281
282/* Read-write object methods */
283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284PyDoc_STRVAR(O_seek__doc__,
Jim Fultone60de4d2000-10-06 19:24:23 +0000285"seek(position) -- set the current position\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000286"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
Jim Fultone60de4d2000-10-06 19:24:23 +0000287
288static PyObject *
289O_seek(Oobject *self, PyObject *args) {
290 int position, mode = 0;
291
292 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
293 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
294 return NULL;
295
296 if (mode == 2) {
297 position += self->string_size;
298 }
299 else if (mode == 1) {
300 position += self->pos;
301 }
302
303 if (position > self->buf_size) {
304 self->buf_size*=2;
305 if (self->buf_size <= position) self->buf_size=position+1;
306 UNLESS (self->buf=(char*)
307 realloc(self->buf,self->buf_size*sizeof(char))) {
308 self->buf_size=self->pos=0;
309 return PyErr_NoMemory();
310 }
311 }
312 else if (position < 0) position=0;
313
314 self->pos=position;
315
316 while (--position >= self->string_size) self->buf[position]=0;
317
318 Py_INCREF(Py_None);
319 return Py_None;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000320}
321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000322PyDoc_STRVAR(O_write__doc__,
Guido van Rossum049cd901996-12-05 23:30:48 +0000323"write(s) -- Write a string to the file"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324"\n\nNote (hack:) writing None resets the buffer");
Guido van Rossum049cd901996-12-05 23:30:48 +0000325
326
327static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000328O_cwrite(PyObject *self, char *c, int l) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000329 int newl;
Guido van Rossum2f098122001-12-07 20:20:28 +0000330 Oobject *oself;
Guido van Rossum55702f81997-01-06 22:57:52 +0000331
Jim Fultone60de4d2000-10-06 19:24:23 +0000332 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
Guido van Rossum2f098122001-12-07 20:20:28 +0000333 oself = (Oobject *)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000334
Guido van Rossum2f098122001-12-07 20:20:28 +0000335 newl = oself->pos+l;
336 if (newl >= oself->buf_size) {
337 oself->buf_size *= 2;
338 if (oself->buf_size <= newl)
339 oself->buf_size = newl+1;
340 UNLESS (oself->buf =
341 (char*)realloc(oself->buf,
342 (oself->buf_size) * sizeof(char))) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000343 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum2f098122001-12-07 20:20:28 +0000344 oself->buf_size = oself->pos = 0;
Jim Fultone60de4d2000-10-06 19:24:23 +0000345 return -1;
346 }
347 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000348
Guido van Rossum2f098122001-12-07 20:20:28 +0000349 memcpy(oself->buf+oself->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000350
Guido van Rossum2f098122001-12-07 20:20:28 +0000351 oself->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000352
Guido van Rossum2f098122001-12-07 20:20:28 +0000353 if (oself->string_size < oself->pos) {
354 oself->string_size = oself->pos;
355 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000356
357 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000358}
359
360static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000361O_write(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000362 char *c;
363 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000364
Guido van Rossuma883a3d2002-04-29 13:54:48 +0000365 UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000366
Jim Fultone60de4d2000-10-06 19:24:23 +0000367 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000368
Jim Fultone60de4d2000-10-06 19:24:23 +0000369 Py_INCREF(Py_None);
370 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000371}
372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000373PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000374
375static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000376O_close(Oobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000377 if (self->buf != NULL) free(self->buf);
378 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000379
Jim Fultone60de4d2000-10-06 19:24:23 +0000380 self->pos = self->string_size = self->buf_size = 0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000381
Jim Fultone60de4d2000-10-06 19:24:23 +0000382 Py_INCREF(Py_None);
383 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000384}
385
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(O_writelines__doc__,
388"writelines(sequence_of_strings): write each string");
Guido van Rossum049cd901996-12-05 23:30:48 +0000389static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000390O_writelines(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000391 PyObject *tmp = 0;
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000392 static PyObject *joiner = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000393
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000394 if (!joiner) {
395 PyObject *empty_string = PyString_FromString("");
396 if (empty_string == NULL)
397 return NULL;
398 joiner = PyObject_GetAttrString(empty_string, "join");
399 Py_DECREF(empty_string);
400 if (joiner == NULL)
401 return NULL;
402 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000403
Jim Fultone60de4d2000-10-06 19:24:23 +0000404 if (PyObject_Size(args) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000405
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000406 tmp = PyObject_CallFunction(joiner, "O", args);
Jim Fultone60de4d2000-10-06 19:24:23 +0000407 UNLESS (tmp) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000408
Jim Fultone60de4d2000-10-06 19:24:23 +0000409 args = Py_BuildValue("(O)", tmp);
410 Py_DECREF(tmp);
411 UNLESS (args) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000412
Jim Fultone60de4d2000-10-06 19:24:23 +0000413 tmp = O_write(self, args);
414 Py_DECREF(args);
415 return tmp;
Guido van Rossum049cd901996-12-05 23:30:48 +0000416}
417
418static struct PyMethodDef O_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000419 /* Common methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000420 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000421 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000422 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000423 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
424 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
425 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000426 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
427 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000428 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
429
430 /* Read-write StringIO specific methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000431 {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000432 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
433 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000434 {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000435 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000436};
437
Guido van Rossum049cd901996-12-05 23:30:48 +0000438static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000439O_dealloc(Oobject *self) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000440 if (self->buf != NULL)
441 free(self->buf);
442 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000443}
444
445static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000446O_getattr(Oobject *self, char *name) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000447 if (strcmp(name, "softspace") == 0) {
448 return PyInt_FromLong(self->softspace);
449 }
450 return Py_FindMethod(O_methods, (PyObject *)self, name);
Guido van Rossum049cd901996-12-05 23:30:48 +0000451}
452
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000453static int
454O_setattr(Oobject *self, char *name, PyObject *value) {
455 long x;
456 if (strcmp(name, "softspace") != 0) {
457 PyErr_SetString(PyExc_AttributeError, name);
458 return -1;
459 }
460 x = PyInt_AsLong(value);
Jim Fultone60de4d2000-10-06 19:24:23 +0000461 if (x < 0 && PyErr_Occurred())
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000462 return -1;
463 self->softspace = x;
464 return 0;
465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000468
469static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000470 PyObject_HEAD_INIT(NULL)
471 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000472 "cStringIO.StringO", /*tp_name*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000473 sizeof(Oobject), /*tp_basicsize*/
474 0, /*tp_itemsize*/
475 /* methods */
476 (destructor)O_dealloc, /*tp_dealloc*/
477 (printfunc)0, /*tp_print*/
478 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000479 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000480 (cmpfunc)0, /*tp_compare*/
481 (reprfunc)0, /*tp_repr*/
482 0, /*tp_as_number*/
483 0, /*tp_as_sequence*/
484 0, /*tp_as_mapping*/
485 (hashfunc)0, /*tp_hash*/
486 (ternaryfunc)0, /*tp_call*/
487 (reprfunc)0, /*tp_str*/
488
489 /* Space for future expansion */
490 0L,0L,0L,0L,
491 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000492};
493
Guido van Rossum142eeb81997-08-13 03:14:41 +0000494static PyObject *
495newOobject(int size) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000496 Oobject *self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000497
Jim Fultone60de4d2000-10-06 19:24:23 +0000498 self = PyObject_New(Oobject, &Otype);
499 if (self == NULL)
500 return NULL;
501 self->pos=0;
502 self->string_size = 0;
503 self->softspace = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000504
Jim Fultone60de4d2000-10-06 19:24:23 +0000505 UNLESS (self->buf=malloc(size*sizeof(char))) {
506 PyErr_SetString(PyExc_MemoryError,"out of memory");
507 self->buf_size = 0;
508 return NULL;
509 }
510
511 self->buf_size=size;
512 return (PyObject*)self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000513}
514
Guido van Rossum049cd901996-12-05 23:30:48 +0000515/* End of code for StringO objects */
516/* -------------------------------------------------------- */
517
518static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000519I_close(Iobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000520 Py_XDECREF(self->pbuf);
521 self->pbuf = NULL;
522 self->buf = NULL;
523
524 self->pos = self->string_size = 0;
525
526 Py_INCREF(Py_None);
527 return Py_None;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000528}
529
530static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000531I_seek(Iobject *self, PyObject *args) {
532 int position, mode = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000533
Jim Fultone60de4d2000-10-06 19:24:23 +0000534 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
535 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
536 return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000537
Jim Fultone60de4d2000-10-06 19:24:23 +0000538 if (mode == 2) position += self->string_size;
539 else if (mode == 1) position += self->pos;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000540
Jim Fultone60de4d2000-10-06 19:24:23 +0000541 if (position < 0) position=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000542
Jim Fultone60de4d2000-10-06 19:24:23 +0000543 self->pos=position;
544
545 Py_INCREF(Py_None);
546 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000547}
548
549static struct PyMethodDef I_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000550 /* Common methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000551 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000552 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000553 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000554 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
555 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
556 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000557 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
558 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000559 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
560
561 /* Read-only StringIO specific methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000562 {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000563 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000564 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000565};
566
Guido van Rossum049cd901996-12-05 23:30:48 +0000567static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000568I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000569 Py_XDECREF(self->pbuf);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000570 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000571}
572
573static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000574I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000575 return Py_FindMethod(I_methods, (PyObject *)self, name);
576}
577
Barry Warsaw3e8be722001-09-22 04:36:49 +0000578static PyObject *
579I_getiter(Iobject *self)
580{
581 PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
582 "readline");
583 PyObject *emptystring = PyString_FromString("");
584 PyObject *iter = NULL;
585 if (!myreadline || !emptystring)
586 goto finally;
587
588 iter = PyCallIter_New(myreadline, emptystring);
589 finally:
590 Py_XDECREF(myreadline);
591 Py_XDECREF(emptystring);
592 return iter;
593}
594
595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000596PyDoc_STRVAR(Itype__doc__,
597"Simple type for treating strings as input file streams");
Guido van Rossum049cd901996-12-05 23:30:48 +0000598
599static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000600 PyObject_HEAD_INIT(NULL)
Barry Warsaw3e8be722001-09-22 04:36:49 +0000601 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000602 "cStringIO.StringI", /*tp_name*/
Barry Warsaw3e8be722001-09-22 04:36:49 +0000603 sizeof(Iobject), /*tp_basicsize*/
604 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000605 /* methods */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000606 (destructor)I_dealloc, /*tp_dealloc*/
607 (printfunc)0, /*tp_print*/
608 (getattrfunc)I_getattr, /*tp_getattr*/
609 (setattrfunc)0, /*tp_setattr*/
610 (cmpfunc)0, /*tp_compare*/
611 (reprfunc)0, /*tp_repr*/
612 0, /*tp_as_number*/
613 0, /*tp_as_sequence*/
614 0, /*tp_as_mapping*/
615 (hashfunc)0, /*tp_hash*/
616 (ternaryfunc)0, /*tp_call*/
617 (reprfunc)0, /*tp_str*/
618 0, /* tp_getattro */
619 0, /* tp_setattro */
620 0, /* tp_as_buffer */
621 Py_TPFLAGS_DEFAULT, /* tp_flags */
622 Itype__doc__, /* tp_doc */
623 0, /* tp_traverse */
624 0, /* tp_clear */
625 0, /* tp_richcompare */
626 0, /* tp_weaklistoffset */
627 (getiterfunc)I_getiter, /* tp_iter */
628 0, /* tp_iternext */
Guido van Rossum049cd901996-12-05 23:30:48 +0000629};
630
Guido van Rossum142eeb81997-08-13 03:14:41 +0000631static PyObject *
632newIobject(PyObject *s) {
633 Iobject *self;
634 char *buf;
635 int size;
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000636
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000637 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
638 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000639 s->ob_type->tp_name);
640 return NULL;
641 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000642 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000643 Py_INCREF(s);
644 self->buf=buf;
645 self->string_size=size;
646 self->pbuf=s;
647 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000648
649 return (PyObject*)self;
650}
651
Guido van Rossum049cd901996-12-05 23:30:48 +0000652/* End of code for StringI objects */
653/* -------------------------------------------------------- */
654
655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000656PyDoc_STRVAR(IO_StringIO__doc__,
657"StringIO([s]) -- Return a StringIO-like stream for reading or writing");
Guido van Rossum049cd901996-12-05 23:30:48 +0000658
659static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000660IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000661 PyObject *s=0;
662
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000663 if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000664
665 if (s) return newIobject(s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000666 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000667}
668
669/* List of methods defined in the module */
670
671static struct PyMethodDef IO_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000672 {"StringIO", (PyCFunction)IO_StringIO,
673 METH_VARARGS, IO_StringIO__doc__},
Guido van Rossum55702f81997-01-06 22:57:52 +0000674 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000675};
676
677
678/* Initialization function for the module (*must* be called initcStringIO) */
679
Guido van Rossum154417e1997-04-09 17:35:33 +0000680static struct PycStringIO_CAPI CAPI = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000681 IO_cread,
682 IO_creadline,
Guido van Rossum154417e1997-04-09 17:35:33 +0000683 O_cwrite,
Jim Fultone60de4d2000-10-06 19:24:23 +0000684 IO_cgetval,
Guido van Rossum154417e1997-04-09 17:35:33 +0000685 newOobject,
686 newIobject,
687 &Itype,
688 &Otype,
689};
690
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000691#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
692#define PyMODINIT_FUNC void
Guido van Rossum476e49f1998-12-15 21:43:15 +0000693#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000694PyMODINIT_FUNC
Thomas Wouters58d05102000-07-24 14:43:35 +0000695initcStringIO(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000696 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000697
Guido van Rossum154417e1997-04-09 17:35:33 +0000698
Guido van Rossum049cd901996-12-05 23:30:48 +0000699 /* 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);
Guido van Rossum049cd901996-12-05 23:30:48 +0000706
Guido van Rossum55702f81997-01-06 22:57:52 +0000707 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000708 Itype.ob_type=&PyType_Type;
709 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000710 PyDict_SetItemString(d,"cStringIO_CAPI",
711 v = PyCObject_FromVoidPtr(&CAPI,NULL));
712 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000713
714 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000715 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
716 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000717
718 /* Maybe make certain warnings go away */
Jim Fultone60de4d2000-10-06 19:24:23 +0000719 if (0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000720}