blob: 1dbec846516c629f1ecab5733f614e4d4e8a3b15 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001static char cStringIO_module_documentation[] =
2"A simple fast partial StringIO replacement.\n"
3"\n"
4"This module provides a simple useful replacement for\n"
5"the StringIO module that is written in C. It does not provide the\n"
Fred Drakeaef10002000-06-19 13:17:41 +00006"full generality of StringIO, but it provides enough for most\n"
Thomas Wouters7e474022000-07-16 12:04:32 +00007"applications and is especially useful in conjunction with the\n"
Guido van Rossum049cd901996-12-05 23:30:48 +00008"pickle module.\n"
9"\n"
10"Usage:\n"
11"\n"
12" from cStringIO import StringIO\n"
13"\n"
14" an_output_stream=StringIO()\n"
15" an_output_stream.write(some_stuff)\n"
16" ...\n"
Jeremy Hyltonb189b072002-03-08 17:17:33 +000017" value=an_output_stream.getvalue()\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000018"\n"
19" an_input_stream=StringIO(a_string)\n"
20" spam=an_input_stream.readline()\n"
21" spam=an_input_stream.read(5)\n"
Guido van Rossum7d9b4131998-11-25 16:17:32 +000022" an_input_stream.seek(0) # OK, start over\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000023" spam=an_input_stream.read() # and read it all\n"
24" \n"
25"If someone else wants to provide a more complete implementation,\n"
26"go for it. :-) \n"
Guido van Rossum142eeb81997-08-13 03:14:41 +000027"\n"
Guido van Rossum17d53ec1999-06-15 14:35:48 +000028"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000029;
30
31#include "Python.h"
32#include "import.h"
Guido van Rossum154417e1997-04-09 17:35:33 +000033#include "cStringIO.h"
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
77static char IO_flush__doc__[] = "flush(): does nothing.";
78
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 *
90IO_flush(IOobject *self, PyObject *args) {
91
92 UNLESS (IO__opencheck(self)) return NULL;
93 UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL;
94
95 Py_INCREF(Py_None);
96 return Py_None;
97}
98
99static char IO_getval__doc__[] =
100 "getvalue([use_pos]) -- Get the string value."
101 "\n"
102 "If use_pos is specified and is a true value, then the string returned\n"
103 "will include only the text up to the current file position.\n"
Guido van Rossum049cd901996-12-05 23:30:48 +0000104;
105
106static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000107IO_cgetval(PyObject *self) {
108 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
109 return PyString_FromStringAndSize(((IOobject*)self)->buf,
110 ((IOobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000111}
112
Guido van Rossum049cd901996-12-05 23:30:48 +0000113static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000114IO_getval(IOobject *self, PyObject *args) {
115 PyObject *use_pos=Py_None;
116 int s;
117
118 UNLESS (IO__opencheck(self)) return NULL;
119 UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
120
121 if (PyObject_IsTrue(use_pos)) {
122 s=self->pos;
123 if (s > self->string_size) s=self->string_size;
124 }
125 else
126 s=self->string_size;
127 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum049cd901996-12-05 23:30:48 +0000128}
129
Jim Fultone60de4d2000-10-06 19:24:23 +0000130static char IO_isatty__doc__[] = "isatty(): always returns 0";
Guido van Rossum049cd901996-12-05 23:30:48 +0000131
132static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000133IO_isatty(IOobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000134
Jim Fultone60de4d2000-10-06 19:24:23 +0000135 UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000136
Jim Fultone60de4d2000-10-06 19:24:23 +0000137 return PyInt_FromLong(0);
Guido van Rossum049cd901996-12-05 23:30:48 +0000138}
139
Jim Fultone60de4d2000-10-06 19:24:23 +0000140static char IO_read__doc__[] =
Guido van Rossum049cd901996-12-05 23:30:48 +0000141"read([s]) -- Read s characters, or the rest of the string"
142;
143
144static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000145IO_cread(PyObject *self, char **output, int n) {
146 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000147
Jim Fultone60de4d2000-10-06 19:24:23 +0000148 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
149 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
150 if (n < 0 || n > l) {
151 n = l;
152 if (n < 0) n=0;
153 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000154
Jim Fultone60de4d2000-10-06 19:24:23 +0000155 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
156 ((IOobject*)self)->pos += n;
157 return n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000158}
159
160static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000161IO_read(IOobject *self, PyObject *args) {
162 int n = -1;
163 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000164
Jim Fultone60de4d2000-10-06 19:24:23 +0000165 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000166
Jim Fultone60de4d2000-10-06 19:24:23 +0000167 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000168
Jim Fultone60de4d2000-10-06 19:24:23 +0000169 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000170}
171
Jim Fultone60de4d2000-10-06 19:24:23 +0000172static char IO_readline__doc__[] =
Guido van Rossum049cd901996-12-05 23:30:48 +0000173"readline() -- Read one line"
174;
175
176static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000177IO_creadline(PyObject *self, char **output) {
178 char *n, *s;
179 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000180
Jim Fultone60de4d2000-10-06 19:24:23 +0000181 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
182
183 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
184 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
185 n < s && *n != '\n'; n++);
186 if (n < s) n++;
187
188 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
189 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
190 ((IOobject*)self)->pos += l;
191 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000192}
193
194static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000195IO_readline(IOobject *self, PyObject *args) {
196 int n, m=-1;
197 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000198
Jim Fultone60de4d2000-10-06 19:24:23 +0000199 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
200
201 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
202 if (m >= 0 && m < n) {
203 m = n - m;
204 n -= m;
205 self->pos -= m;
206 }
207 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000208}
209
Jim Fultone60de4d2000-10-06 19:24:23 +0000210static char IO_readlines__doc__[] =
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000211"readlines() -- Read all lines"
212;
213
214static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000215IO_readlines(IOobject *self, PyObject *args) {
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000216 int n;
217 char *output;
218 PyObject *result, *line;
219 int hint = 0, length = 0;
220
Jim Fultone60de4d2000-10-06 19:24:23 +0000221 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
222
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000223 result = PyList_New(0);
224 if (!result)
225 return NULL;
226
Jim Fultone60de4d2000-10-06 19:24:23 +0000227 while (1){
228 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
229 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000230 if (n == 0)
231 break;
232 line = PyString_FromStringAndSize (output, n);
Jim Fultone60de4d2000-10-06 19:24:23 +0000233 if (!line)
234 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000235 PyList_Append (result, line);
236 Py_DECREF (line);
237 length += n;
238 if (hint > 0 && length >= hint)
239 break;
240 }
241 return result;
Jim Fultone60de4d2000-10-06 19:24:23 +0000242 err:
243 Py_DECREF(result);
244 return NULL;
245}
246
247static char IO_reset__doc__[] =
248"reset() -- Reset the file position to the beginning"
249;
250
251static PyObject *
252IO_reset(IOobject *self, PyObject *args) {
253
254 UNLESS (IO__opencheck(self)) return NULL;
255 UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL;
256
257 self->pos = 0;
258
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263static char IO_tell__doc__[] =
264"tell() -- get the current position.";
265
266static PyObject *
267IO_tell(IOobject *self, PyObject *args) {
268
269 UNLESS (IO__opencheck(self)) return NULL;
270 UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL;
271
272 return PyInt_FromLong(self->pos);
273}
274
275static char IO_truncate__doc__[] =
276"truncate(): truncate the file at the current position.";
277
278static PyObject *
279IO_truncate(IOobject *self, PyObject *args) {
280 int pos = -1;
281
282 UNLESS (IO__opencheck(self)) return NULL;
283 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
284 if (pos < 0) pos = self->pos;
285
286 if (self->string_size > pos) self->string_size = pos;
287
288 Py_INCREF(Py_None);
289 return Py_None;
290}
291
292
293
294
295/* Read-write object methods */
296
297static char O_seek__doc__[] =
298"seek(position) -- set the current position\n"
299"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
300
301static PyObject *
302O_seek(Oobject *self, PyObject *args) {
303 int position, mode = 0;
304
305 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
306 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
307 return NULL;
308
309 if (mode == 2) {
310 position += self->string_size;
311 }
312 else if (mode == 1) {
313 position += self->pos;
314 }
315
316 if (position > self->buf_size) {
317 self->buf_size*=2;
318 if (self->buf_size <= position) self->buf_size=position+1;
319 UNLESS (self->buf=(char*)
320 realloc(self->buf,self->buf_size*sizeof(char))) {
321 self->buf_size=self->pos=0;
322 return PyErr_NoMemory();
323 }
324 }
325 else if (position < 0) position=0;
326
327 self->pos=position;
328
329 while (--position >= self->string_size) self->buf[position]=0;
330
331 Py_INCREF(Py_None);
332 return Py_None;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000333}
334
Guido van Rossum049cd901996-12-05 23:30:48 +0000335static char O_write__doc__[] =
336"write(s) -- Write a string to the file"
337"\n\nNote (hack:) writing None resets the buffer"
338;
339
340
341static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000342O_cwrite(PyObject *self, char *c, int l) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000343 int newl;
Guido van Rossum2f098122001-12-07 20:20:28 +0000344 Oobject *oself;
Guido van Rossum55702f81997-01-06 22:57:52 +0000345
Jim Fultone60de4d2000-10-06 19:24:23 +0000346 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
Guido van Rossum2f098122001-12-07 20:20:28 +0000347 oself = (Oobject *)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000348
Guido van Rossum2f098122001-12-07 20:20:28 +0000349 newl = oself->pos+l;
350 if (newl >= oself->buf_size) {
351 oself->buf_size *= 2;
352 if (oself->buf_size <= newl)
353 oself->buf_size = newl+1;
354 UNLESS (oself->buf =
355 (char*)realloc(oself->buf,
356 (oself->buf_size) * sizeof(char))) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000357 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum2f098122001-12-07 20:20:28 +0000358 oself->buf_size = oself->pos = 0;
Jim Fultone60de4d2000-10-06 19:24:23 +0000359 return -1;
360 }
361 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000362
Guido van Rossum2f098122001-12-07 20:20:28 +0000363 memcpy(oself->buf+oself->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000364
Guido van Rossum2f098122001-12-07 20:20:28 +0000365 oself->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000366
Guido van Rossum2f098122001-12-07 20:20:28 +0000367 if (oself->string_size < oself->pos) {
368 oself->string_size = oself->pos;
369 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000370
371 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000372}
373
374static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000375O_write(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000376 char *c;
377 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000378
Guido van Rossuma883a3d2002-04-29 13:54:48 +0000379 UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000380
Jim Fultone60de4d2000-10-06 19:24:23 +0000381 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000382
Jim Fultone60de4d2000-10-06 19:24:23 +0000383 Py_INCREF(Py_None);
384 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000385}
386
387static char O_close__doc__[] = "close(): explicitly release resources held.";
388
389static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000390O_close(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000391
Jim Fultone60de4d2000-10-06 19:24:23 +0000392 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000393
Jim Fultone60de4d2000-10-06 19:24:23 +0000394 if (self->buf != NULL) free(self->buf);
395 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000396
Jim Fultone60de4d2000-10-06 19:24:23 +0000397 self->pos = self->string_size = self->buf_size = 0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000398
Jim Fultone60de4d2000-10-06 19:24:23 +0000399 Py_INCREF(Py_None);
400 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000401}
402
403
Guido van Rossum68de0641999-02-08 17:03:27 +0000404static char O_writelines__doc__[] =
405"writelines(sequence_of_strings): write each string";
Guido van Rossum049cd901996-12-05 23:30:48 +0000406static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000407O_writelines(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000408 PyObject *tmp = 0;
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000409 static PyObject *joiner = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000410
Jim Fultone60de4d2000-10-06 19:24:23 +0000411 UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000412
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000413 if (!joiner) {
414 PyObject *empty_string = PyString_FromString("");
415 if (empty_string == NULL)
416 return NULL;
417 joiner = PyObject_GetAttrString(empty_string, "join");
418 Py_DECREF(empty_string);
419 if (joiner == NULL)
420 return NULL;
421 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000422
Jim Fultone60de4d2000-10-06 19:24:23 +0000423 if (PyObject_Size(args) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000424
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000425 tmp = PyObject_CallFunction(joiner, "O", args);
Jim Fultone60de4d2000-10-06 19:24:23 +0000426 UNLESS (tmp) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000427
Jim Fultone60de4d2000-10-06 19:24:23 +0000428 args = Py_BuildValue("(O)", tmp);
429 Py_DECREF(tmp);
430 UNLESS (args) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000431
Jim Fultone60de4d2000-10-06 19:24:23 +0000432 tmp = O_write(self, args);
433 Py_DECREF(args);
434 return tmp;
Guido van Rossum049cd901996-12-05 23:30:48 +0000435}
436
437static struct PyMethodDef O_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000438 /* Common methods: */
439 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
440 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
441 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
442 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
443 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
444 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
445 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
446 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
447 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
448
449 /* Read-write StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000450 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000451 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
452 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000453 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
454 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000455};
456
Guido van Rossum049cd901996-12-05 23:30:48 +0000457static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000458O_dealloc(Oobject *self) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000459 if (self->buf != NULL)
460 free(self->buf);
461 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000462}
463
464static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000465O_getattr(Oobject *self, char *name) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000466 if (strcmp(name, "softspace") == 0) {
467 return PyInt_FromLong(self->softspace);
468 }
469 return Py_FindMethod(O_methods, (PyObject *)self, name);
Guido van Rossum049cd901996-12-05 23:30:48 +0000470}
471
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000472static int
473O_setattr(Oobject *self, char *name, PyObject *value) {
474 long x;
475 if (strcmp(name, "softspace") != 0) {
476 PyErr_SetString(PyExc_AttributeError, name);
477 return -1;
478 }
479 x = PyInt_AsLong(value);
Jim Fultone60de4d2000-10-06 19:24:23 +0000480 if (x < 0 && PyErr_Occurred())
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000481 return -1;
482 self->softspace = x;
483 return 0;
484}
485
Guido van Rossum049cd901996-12-05 23:30:48 +0000486static char Otype__doc__[] =
487"Simple type for output to strings."
488;
489
490static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000491 PyObject_HEAD_INIT(NULL)
492 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000493 "cStringIO.StringO", /*tp_name*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000494 sizeof(Oobject), /*tp_basicsize*/
495 0, /*tp_itemsize*/
496 /* methods */
497 (destructor)O_dealloc, /*tp_dealloc*/
498 (printfunc)0, /*tp_print*/
499 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000500 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000501 (cmpfunc)0, /*tp_compare*/
502 (reprfunc)0, /*tp_repr*/
503 0, /*tp_as_number*/
504 0, /*tp_as_sequence*/
505 0, /*tp_as_mapping*/
506 (hashfunc)0, /*tp_hash*/
507 (ternaryfunc)0, /*tp_call*/
508 (reprfunc)0, /*tp_str*/
509
510 /* Space for future expansion */
511 0L,0L,0L,0L,
512 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000513};
514
Guido van Rossum142eeb81997-08-13 03:14:41 +0000515static PyObject *
516newOobject(int size) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000517 Oobject *self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000518
Jim Fultone60de4d2000-10-06 19:24:23 +0000519 self = PyObject_New(Oobject, &Otype);
520 if (self == NULL)
521 return NULL;
522 self->pos=0;
523 self->string_size = 0;
524 self->softspace = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000525
Jim Fultone60de4d2000-10-06 19:24:23 +0000526 UNLESS (self->buf=malloc(size*sizeof(char))) {
527 PyErr_SetString(PyExc_MemoryError,"out of memory");
528 self->buf_size = 0;
529 return NULL;
530 }
531
532 self->buf_size=size;
533 return (PyObject*)self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000534}
535
Guido van Rossum049cd901996-12-05 23:30:48 +0000536/* End of code for StringO objects */
537/* -------------------------------------------------------- */
538
539static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000540I_close(Iobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000541
Jim Fultone60de4d2000-10-06 19:24:23 +0000542 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000543
Jim Fultone60de4d2000-10-06 19:24:23 +0000544 Py_XDECREF(self->pbuf);
545 self->pbuf = NULL;
546 self->buf = NULL;
547
548 self->pos = self->string_size = 0;
549
550 Py_INCREF(Py_None);
551 return Py_None;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000552}
553
554static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000555I_seek(Iobject *self, PyObject *args) {
556 int position, mode = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000557
Jim Fultone60de4d2000-10-06 19:24:23 +0000558 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
559 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
560 return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000561
Jim Fultone60de4d2000-10-06 19:24:23 +0000562 if (mode == 2) position += self->string_size;
563 else if (mode == 1) position += self->pos;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000564
Jim Fultone60de4d2000-10-06 19:24:23 +0000565 if (position < 0) position=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000566
Jim Fultone60de4d2000-10-06 19:24:23 +0000567 self->pos=position;
568
569 Py_INCREF(Py_None);
570 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000571}
572
573static struct PyMethodDef I_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000574 /* Common methods: */
575 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
576 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
577 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
578 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
579 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
580 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
581 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
582 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
583 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
584
585 /* Read-only StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000586 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000587 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000588 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000589};
590
Guido van Rossum049cd901996-12-05 23:30:48 +0000591static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000592I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000593 Py_XDECREF(self->pbuf);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000594 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000595}
596
597static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000598I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000599 return Py_FindMethod(I_methods, (PyObject *)self, name);
600}
601
Barry Warsaw3e8be722001-09-22 04:36:49 +0000602static PyObject *
603I_getiter(Iobject *self)
604{
605 PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
606 "readline");
607 PyObject *emptystring = PyString_FromString("");
608 PyObject *iter = NULL;
609 if (!myreadline || !emptystring)
610 goto finally;
611
612 iter = PyCallIter_New(myreadline, emptystring);
613 finally:
614 Py_XDECREF(myreadline);
615 Py_XDECREF(emptystring);
616 return iter;
617}
618
619
Guido van Rossum049cd901996-12-05 23:30:48 +0000620static char Itype__doc__[] =
621"Simple type for treating strings as input file streams"
622;
623
624static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000625 PyObject_HEAD_INIT(NULL)
Barry Warsaw3e8be722001-09-22 04:36:49 +0000626 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000627 "cStringIO.StringI", /*tp_name*/
Barry Warsaw3e8be722001-09-22 04:36:49 +0000628 sizeof(Iobject), /*tp_basicsize*/
629 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000630 /* methods */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000631 (destructor)I_dealloc, /*tp_dealloc*/
632 (printfunc)0, /*tp_print*/
633 (getattrfunc)I_getattr, /*tp_getattr*/
634 (setattrfunc)0, /*tp_setattr*/
635 (cmpfunc)0, /*tp_compare*/
636 (reprfunc)0, /*tp_repr*/
637 0, /*tp_as_number*/
638 0, /*tp_as_sequence*/
639 0, /*tp_as_mapping*/
640 (hashfunc)0, /*tp_hash*/
641 (ternaryfunc)0, /*tp_call*/
642 (reprfunc)0, /*tp_str*/
643 0, /* tp_getattro */
644 0, /* tp_setattro */
645 0, /* tp_as_buffer */
646 Py_TPFLAGS_DEFAULT, /* tp_flags */
647 Itype__doc__, /* tp_doc */
648 0, /* tp_traverse */
649 0, /* tp_clear */
650 0, /* tp_richcompare */
651 0, /* tp_weaklistoffset */
652 (getiterfunc)I_getiter, /* tp_iter */
653 0, /* tp_iternext */
Guido van Rossum049cd901996-12-05 23:30:48 +0000654};
655
Guido van Rossum142eeb81997-08-13 03:14:41 +0000656static PyObject *
657newIobject(PyObject *s) {
658 Iobject *self;
659 char *buf;
660 int size;
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000661
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000662 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
663 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000664 s->ob_type->tp_name);
665 return NULL;
666 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000667 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000668 Py_INCREF(s);
669 self->buf=buf;
670 self->string_size=size;
671 self->pbuf=s;
672 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000673
674 return (PyObject*)self;
675}
676
Guido van Rossum049cd901996-12-05 23:30:48 +0000677/* End of code for StringI objects */
678/* -------------------------------------------------------- */
679
680
681static char IO_StringIO__doc__[] =
682"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
683;
684
685static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000686IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000687 PyObject *s=0;
688
Jim Fultone60de4d2000-10-06 19:24:23 +0000689 if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
690
691 if (s) return newIobject(s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000692 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000693}
694
695/* List of methods defined in the module */
696
697static struct PyMethodDef IO_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000698 {"StringIO", (PyCFunction)IO_StringIO,
699 METH_VARARGS, IO_StringIO__doc__},
Guido van Rossum55702f81997-01-06 22:57:52 +0000700 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000701};
702
703
704/* Initialization function for the module (*must* be called initcStringIO) */
705
Guido van Rossum154417e1997-04-09 17:35:33 +0000706static struct PycStringIO_CAPI CAPI = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000707 IO_cread,
708 IO_creadline,
Guido van Rossum154417e1997-04-09 17:35:33 +0000709 O_cwrite,
Jim Fultone60de4d2000-10-06 19:24:23 +0000710 IO_cgetval,
Guido van Rossum154417e1997-04-09 17:35:33 +0000711 newOobject,
712 newIobject,
713 &Itype,
714 &Otype,
715};
716
Guido van Rossum476e49f1998-12-15 21:43:15 +0000717#ifndef DL_EXPORT /* declarations for DLL import/export */
718#define DL_EXPORT(RTYPE) RTYPE
719#endif
Guido van Rossum3886bb61998-12-04 18:50:17 +0000720DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +0000721initcStringIO(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000722 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000723
Guido van Rossum154417e1997-04-09 17:35:33 +0000724
Guido van Rossum049cd901996-12-05 23:30:48 +0000725 /* Create the module and add the functions */
726 m = Py_InitModule4("cStringIO", IO_methods,
727 cStringIO_module_documentation,
728 (PyObject*)NULL,PYTHON_API_VERSION);
729
730 /* Add some symbolic constants to the module */
731 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000732
Guido van Rossum55702f81997-01-06 22:57:52 +0000733 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000734 Itype.ob_type=&PyType_Type;
735 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000736 PyDict_SetItemString(d,"cStringIO_CAPI",
737 v = PyCObject_FromVoidPtr(&CAPI,NULL));
738 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000739
740 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000741 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
742 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000743
744 /* Maybe make certain warnings go away */
Jim Fultone60de4d2000-10-06 19:24:23 +0000745 if (0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000746}