blob: bc23c77fb60aac0e1679ab967de43646ac492764 [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"
Raymond Hettinger352f9472003-04-24 15:50:11 +00005#include "structmember.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006
7PyDoc_STRVAR(cStringIO_module_documentation,
Guido van Rossum049cd901996-12-05 23:30:48 +00008"A simple fast partial StringIO replacement.\n"
9"\n"
10"This module provides a simple useful replacement for\n"
11"the StringIO module that is written in C. It does not provide the\n"
Fred Drakeaef10002000-06-19 13:17:41 +000012"full generality of StringIO, but it provides enough for most\n"
Thomas Wouters7e474022000-07-16 12:04:32 +000013"applications and is especially useful in conjunction with the\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000014"pickle module.\n"
15"\n"
16"Usage:\n"
17"\n"
18" from cStringIO import StringIO\n"
19"\n"
20" an_output_stream=StringIO()\n"
21" an_output_stream.write(some_stuff)\n"
22" ...\n"
Jeremy Hyltonb189b072002-03-08 17:17:33 +000023" value=an_output_stream.getvalue()\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000024"\n"
25" an_input_stream=StringIO(a_string)\n"
26" spam=an_input_stream.readline()\n"
27" spam=an_input_stream.read(5)\n"
Guido van Rossum7d9b4131998-11-25 16:17:32 +000028" an_input_stream.seek(0) # OK, start over\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000029" spam=an_input_stream.read() # and read it all\n"
30" \n"
31"If someone else wants to provide a more complete implementation,\n"
32"go for it. :-) \n"
Guido van Rossum142eeb81997-08-13 03:14:41 +000033"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
Guido van Rossum049cd901996-12-05 23:30:48 +000035
Jim Fultone60de4d2000-10-06 19:24:23 +000036#define UNLESS(E) if (!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +000037
Guido van Rossum049cd901996-12-05 23:30:48 +000038
Jim Fultone60de4d2000-10-06 19:24:23 +000039/* Declaration for file-like objects that manage data as strings
Guido van Rossum049cd901996-12-05 23:30:48 +000040
Jim Fultone60de4d2000-10-06 19:24:23 +000041 The IOobject type should be though of as a common base type for
42 Iobjects, which provide input (read-only) StringIO objects and
43 Oobjects, which provide read-write objects. Most of the methods
44 depend only on common data.
45*/
Guido van Rossum049cd901996-12-05 23:30:48 +000046
47typedef struct {
48 PyObject_HEAD
49 char *buf;
Guido van Rossum476e49f1998-12-15 21:43:15 +000050 int pos, string_size;
Jim Fultone60de4d2000-10-06 19:24:23 +000051} IOobject;
52
53#define IOOOBJECT(O) ((IOobject*)(O))
54
55/* Declarations for objects of type StringO */
56
57typedef struct { /* Subtype of IOobject */
58 PyObject_HEAD
59 char *buf;
60 int pos, string_size;
61
62 int buf_size, softspace;
63} Oobject;
64
65/* Declarations for objects of type StringI */
66
67typedef struct { /* Subtype of IOobject */
68 PyObject_HEAD
69 char *buf;
70 int pos, string_size;
Marc-André Lemburge47df7a2001-09-24 17:34:52 +000071 /* We store a reference to the object here in order to keep
72 the buffer alive during the lifetime of the Iobject. */
Guido van Rossum049cd901996-12-05 23:30:48 +000073 PyObject *pbuf;
74} Iobject;
75
Jim Fultone60de4d2000-10-06 19:24:23 +000076/* IOobject (common) methods */
77
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing.");
Jim Fultone60de4d2000-10-06 19:24:23 +000079
80static int
81IO__opencheck(IOobject *self) {
82 UNLESS (self->buf) {
83 PyErr_SetString(PyExc_ValueError,
84 "I/O operation on closed file");
85 return 0;
86 }
87 return 1;
88}
89
90static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +000091IO_flush(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +000092
93 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +000094
95 Py_INCREF(Py_None);
96 return Py_None;
97}
98
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(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
105static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000106IO_cgetval(PyObject *self) {
107 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
108 return PyString_FromStringAndSize(((IOobject*)self)->buf,
109 ((IOobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000110}
111
Guido van Rossum049cd901996-12-05 23:30:48 +0000112static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000113IO_getval(IOobject *self, PyObject *args) {
114 PyObject *use_pos=Py_None;
115 int s;
116
117 UNLESS (IO__opencheck(self)) return NULL;
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000118 UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000119
120 if (PyObject_IsTrue(use_pos)) {
121 s=self->pos;
122 if (s > self->string_size) s=self->string_size;
123 }
124 else
125 s=self->string_size;
126 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum049cd901996-12-05 23:30:48 +0000127}
128
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000129PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
Guido van Rossum049cd901996-12-05 23:30:48 +0000130
131static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000132IO_isatty(IOobject *self, PyObject *unused) {
Guido van Rossum674deb22002-09-01 15:06:28 +0000133 Py_INCREF(Py_False);
134 return Py_False;
Guido van Rossum049cd901996-12-05 23:30:48 +0000135}
136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137PyDoc_STRVAR(IO_read__doc__,
138"read([s]) -- Read s characters, or the rest of the string");
Guido van Rossum049cd901996-12-05 23:30:48 +0000139
140static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000141IO_cread(PyObject *self, char **output, int n) {
142 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000143
Jim Fultone60de4d2000-10-06 19:24:23 +0000144 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
145 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
146 if (n < 0 || n > l) {
147 n = l;
148 if (n < 0) n=0;
149 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000150
Jim Fultone60de4d2000-10-06 19:24:23 +0000151 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
152 ((IOobject*)self)->pos += n;
153 return n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000154}
155
156static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000157IO_read(IOobject *self, PyObject *args) {
158 int n = -1;
159 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000160
Jim Fultone60de4d2000-10-06 19:24:23 +0000161 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000162
Jim Fultone60de4d2000-10-06 19:24:23 +0000163 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000164
Jim Fultone60de4d2000-10-06 19:24:23 +0000165 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000166}
167
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000168PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
Guido van Rossum049cd901996-12-05 23:30:48 +0000169
170static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000171IO_creadline(PyObject *self, char **output) {
172 char *n, *s;
173 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000174
Jim Fultone60de4d2000-10-06 19:24:23 +0000175 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
176
177 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
178 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
179 n < s && *n != '\n'; n++);
180 if (n < s) n++;
181
182 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
183 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
184 ((IOobject*)self)->pos += l;
185 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000186}
187
188static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000189IO_readline(IOobject *self, PyObject *args) {
190 int n, m=-1;
191 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000192
Raymond Hettinger352f9472003-04-24 15:50:11 +0000193 if (args)
194 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000195
196 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
197 if (m >= 0 && m < n) {
198 m = n - m;
199 n -= m;
200 self->pos -= m;
201 }
202 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000203}
204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000206
207static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000208IO_readlines(IOobject *self, PyObject *args) {
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000209 int n;
210 char *output;
211 PyObject *result, *line;
212 int hint = 0, length = 0;
213
Jim Fultone60de4d2000-10-06 19:24:23 +0000214 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
215
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000216 result = PyList_New(0);
217 if (!result)
218 return NULL;
219
Jim Fultone60de4d2000-10-06 19:24:23 +0000220 while (1){
221 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
222 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000223 if (n == 0)
224 break;
225 line = PyString_FromStringAndSize (output, n);
Jim Fultone60de4d2000-10-06 19:24:23 +0000226 if (!line)
227 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000228 PyList_Append (result, line);
229 Py_DECREF (line);
230 length += n;
231 if (hint > 0 && length >= hint)
232 break;
233 }
234 return result;
Jim Fultone60de4d2000-10-06 19:24:23 +0000235 err:
236 Py_DECREF(result);
237 return NULL;
238}
239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240PyDoc_STRVAR(IO_reset__doc__,
241"reset() -- Reset the file position to the beginning");
Jim Fultone60de4d2000-10-06 19:24:23 +0000242
243static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000244IO_reset(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000245
246 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000247
248 self->pos = 0;
249
250 Py_INCREF(Py_None);
251 return Py_None;
252}
253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000254PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000255
256static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000257IO_tell(IOobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000258
259 UNLESS (IO__opencheck(self)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000260
261 return PyInt_FromLong(self->pos);
262}
263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264PyDoc_STRVAR(IO_truncate__doc__,
265"truncate(): truncate the file at the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000266
267static PyObject *
268IO_truncate(IOobject *self, PyObject *args) {
269 int pos = -1;
270
271 UNLESS (IO__opencheck(self)) return NULL;
272 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
273 if (pos < 0) pos = self->pos;
274
275 if (self->string_size > pos) self->string_size = pos;
276
277 Py_INCREF(Py_None);
278 return Py_None;
279}
280
Raymond Hettinger352f9472003-04-24 15:50:11 +0000281static PyObject *
282IO_iternext(Iobject *self)
283{
284 PyObject *next;
285 next = IO_readline((IOobject *)self, NULL);
286 if (!next)
287 return NULL;
288 if (!PyString_GET_SIZE(next)) {
289 Py_DECREF(next);
290 PyErr_SetNone(PyExc_StopIteration);
291 return NULL;
292 }
293 return next;
294}
295
Jim Fultone60de4d2000-10-06 19:24:23 +0000296
297
298
299/* Read-write object methods */
300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000301PyDoc_STRVAR(O_seek__doc__,
Jim Fultone60de4d2000-10-06 19:24:23 +0000302"seek(position) -- set the current position\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000303"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
Jim Fultone60de4d2000-10-06 19:24:23 +0000304
305static PyObject *
306O_seek(Oobject *self, PyObject *args) {
307 int position, mode = 0;
308
309 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
310 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
311 return NULL;
312
313 if (mode == 2) {
314 position += self->string_size;
315 }
316 else if (mode == 1) {
317 position += self->pos;
318 }
319
320 if (position > self->buf_size) {
321 self->buf_size*=2;
322 if (self->buf_size <= position) self->buf_size=position+1;
323 UNLESS (self->buf=(char*)
324 realloc(self->buf,self->buf_size*sizeof(char))) {
325 self->buf_size=self->pos=0;
326 return PyErr_NoMemory();
327 }
328 }
329 else if (position < 0) position=0;
330
331 self->pos=position;
332
333 while (--position >= self->string_size) self->buf[position]=0;
334
335 Py_INCREF(Py_None);
336 return Py_None;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000337}
338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(O_write__doc__,
Guido van Rossum049cd901996-12-05 23:30:48 +0000340"write(s) -- Write a string to the file"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000341"\n\nNote (hack:) writing None resets the buffer");
Guido van Rossum049cd901996-12-05 23:30:48 +0000342
343
344static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000345O_cwrite(PyObject *self, char *c, int l) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000346 int newl;
Guido van Rossum2f098122001-12-07 20:20:28 +0000347 Oobject *oself;
Guido van Rossum55702f81997-01-06 22:57:52 +0000348
Jim Fultone60de4d2000-10-06 19:24:23 +0000349 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
Guido van Rossum2f098122001-12-07 20:20:28 +0000350 oself = (Oobject *)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000351
Guido van Rossum2f098122001-12-07 20:20:28 +0000352 newl = oself->pos+l;
353 if (newl >= oself->buf_size) {
354 oself->buf_size *= 2;
355 if (oself->buf_size <= newl)
356 oself->buf_size = newl+1;
357 UNLESS (oself->buf =
358 (char*)realloc(oself->buf,
359 (oself->buf_size) * sizeof(char))) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000360 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum2f098122001-12-07 20:20:28 +0000361 oself->buf_size = oself->pos = 0;
Jim Fultone60de4d2000-10-06 19:24:23 +0000362 return -1;
363 }
364 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000365
Guido van Rossum2f098122001-12-07 20:20:28 +0000366 memcpy(oself->buf+oself->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000367
Guido van Rossum2f098122001-12-07 20:20:28 +0000368 oself->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000369
Guido van Rossum2f098122001-12-07 20:20:28 +0000370 if (oself->string_size < oself->pos) {
371 oself->string_size = oself->pos;
372 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000373
374 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000375}
376
377static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000378O_write(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000379 char *c;
380 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000381
Guido van Rossuma883a3d2002-04-29 13:54:48 +0000382 UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000383
Jim Fultone60de4d2000-10-06 19:24:23 +0000384 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000385
Jim Fultone60de4d2000-10-06 19:24:23 +0000386 Py_INCREF(Py_None);
387 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000388}
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000391
392static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000393O_close(Oobject *self, PyObject *unused) {
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000404PyDoc_STRVAR(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
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000411 if (!joiner) {
412 PyObject *empty_string = PyString_FromString("");
413 if (empty_string == NULL)
414 return NULL;
415 joiner = PyObject_GetAttrString(empty_string, "join");
416 Py_DECREF(empty_string);
417 if (joiner == NULL)
418 return NULL;
419 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000420
Jim Fultone60de4d2000-10-06 19:24:23 +0000421 if (PyObject_Size(args) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000422
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000423 tmp = PyObject_CallFunction(joiner, "O", args);
Jim Fultone60de4d2000-10-06 19:24:23 +0000424 UNLESS (tmp) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000425
Jim Fultone60de4d2000-10-06 19:24:23 +0000426 args = Py_BuildValue("(O)", tmp);
427 Py_DECREF(tmp);
428 UNLESS (args) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000429
Jim Fultone60de4d2000-10-06 19:24:23 +0000430 tmp = O_write(self, args);
431 Py_DECREF(args);
432 return tmp;
Guido van Rossum049cd901996-12-05 23:30:48 +0000433}
434
435static struct PyMethodDef O_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000436 /* Common methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000437 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000438 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000439 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000440 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
441 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
442 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000443 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
444 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000445 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
446
447 /* Read-write StringIO specific methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000448 {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000449 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
450 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000451 {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000452 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000453};
454
Raymond Hettinger352f9472003-04-24 15:50:11 +0000455static PyMemberDef O_memberlist[] = {
456 {"softspace", T_INT, offsetof(Oobject, softspace), 0,
457 "flag indicating that a space needs to be printed; used by print"},
458 {NULL} /* Sentinel */
459};
460
Guido van Rossum049cd901996-12-05 23:30:48 +0000461static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000462O_dealloc(Oobject *self) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000463 if (self->buf != NULL)
464 free(self->buf);
465 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000466}
467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000468PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000469
470static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000471 PyObject_HEAD_INIT(NULL)
Raymond Hettinger352f9472003-04-24 15:50:11 +0000472 0, /*ob_size*/
473 "cStringIO.StringO", /*tp_name*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000474 sizeof(Oobject), /*tp_basicsize*/
Raymond Hettinger352f9472003-04-24 15:50:11 +0000475 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000476 /* methods */
477 (destructor)O_dealloc, /*tp_dealloc*/
Raymond Hettinger352f9472003-04-24 15:50:11 +0000478 (printfunc)0, /*tp_print*/
479 0, /*tp_getattr */
480 0, /*tp_setattr */
481 (cmpfunc)0, /*tp_compare*/
482 (reprfunc)0, /*tp_repr*/
483 0, /*tp_as_number*/
484 0, /*tp_as_sequence*/
485 0, /*tp_as_mapping*/
486 (hashfunc)0, /*tp_hash*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000487 (ternaryfunc)0, /*tp_call*/
Raymond Hettinger352f9472003-04-24 15:50:11 +0000488 (reprfunc)0, /*tp_str*/
489 0, /*tp_getattro */
490 0, /*tp_setattro */
491 0, /*tp_as_buffer */
492 Py_TPFLAGS_DEFAULT, /*tp_flags*/
493 Otype__doc__, /*tp_doc */
494 0, /*tp_traverse */
495 0, /*tp_clear */
496 0, /*tp_richcompare */
497 0, /*tp_weaklistoffset */
498 PyObject_SelfIter, /*tp_iter */
499 (iternextfunc)IO_iternext, /*tp_iternext */
500 O_methods, /*tp_methods */
501 O_memberlist /*tp_members */
Guido van Rossum049cd901996-12-05 23:30:48 +0000502};
503
Guido van Rossum142eeb81997-08-13 03:14:41 +0000504static PyObject *
505newOobject(int size) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000506 Oobject *self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000507
Jim Fultone60de4d2000-10-06 19:24:23 +0000508 self = PyObject_New(Oobject, &Otype);
509 if (self == NULL)
510 return NULL;
511 self->pos=0;
512 self->string_size = 0;
513 self->softspace = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000514
Jim Fultone60de4d2000-10-06 19:24:23 +0000515 UNLESS (self->buf=malloc(size*sizeof(char))) {
516 PyErr_SetString(PyExc_MemoryError,"out of memory");
517 self->buf_size = 0;
518 return NULL;
519 }
520
521 self->buf_size=size;
522 return (PyObject*)self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000523}
524
Guido van Rossum049cd901996-12-05 23:30:48 +0000525/* End of code for StringO objects */
526/* -------------------------------------------------------- */
527
528static PyObject *
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000529I_close(Iobject *self, PyObject *unused) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000530 Py_XDECREF(self->pbuf);
531 self->pbuf = NULL;
532 self->buf = NULL;
533
534 self->pos = self->string_size = 0;
535
536 Py_INCREF(Py_None);
537 return Py_None;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000538}
539
540static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000541I_seek(Iobject *self, PyObject *args) {
542 int position, mode = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000543
Jim Fultone60de4d2000-10-06 19:24:23 +0000544 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
545 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
546 return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000547
Jim Fultone60de4d2000-10-06 19:24:23 +0000548 if (mode == 2) position += self->string_size;
549 else if (mode == 1) position += self->pos;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000550
Jim Fultone60de4d2000-10-06 19:24:23 +0000551 if (position < 0) position=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000552
Jim Fultone60de4d2000-10-06 19:24:23 +0000553 self->pos=position;
554
555 Py_INCREF(Py_None);
556 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000557}
558
559static struct PyMethodDef I_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000560 /* Common methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000561 {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000562 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000563 {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000564 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
565 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
566 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000567 {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
568 {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000569 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
570
571 /* Read-only StringIO specific methods: */
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000572 {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000573 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000574 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000575};
576
Guido van Rossum049cd901996-12-05 23:30:48 +0000577static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000578I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000579 Py_XDECREF(self->pbuf);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000580 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000581}
582
Barry Warsaw3e8be722001-09-22 04:36:49 +0000583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000584PyDoc_STRVAR(Itype__doc__,
585"Simple type for treating strings as input file streams");
Guido van Rossum049cd901996-12-05 23:30:48 +0000586
587static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000588 PyObject_HEAD_INIT(NULL)
Barry Warsaw3e8be722001-09-22 04:36:49 +0000589 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000590 "cStringIO.StringI", /*tp_name*/
Barry Warsaw3e8be722001-09-22 04:36:49 +0000591 sizeof(Iobject), /*tp_basicsize*/
592 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000593 /* methods */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000594 (destructor)I_dealloc, /*tp_dealloc*/
595 (printfunc)0, /*tp_print*/
Raymond Hettinger352f9472003-04-24 15:50:11 +0000596 0, /* tp_getattr */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000597 (setattrfunc)0, /*tp_setattr*/
598 (cmpfunc)0, /*tp_compare*/
599 (reprfunc)0, /*tp_repr*/
600 0, /*tp_as_number*/
601 0, /*tp_as_sequence*/
602 0, /*tp_as_mapping*/
603 (hashfunc)0, /*tp_hash*/
604 (ternaryfunc)0, /*tp_call*/
605 (reprfunc)0, /*tp_str*/
606 0, /* tp_getattro */
607 0, /* tp_setattro */
608 0, /* tp_as_buffer */
609 Py_TPFLAGS_DEFAULT, /* tp_flags */
610 Itype__doc__, /* tp_doc */
611 0, /* tp_traverse */
612 0, /* tp_clear */
613 0, /* tp_richcompare */
614 0, /* tp_weaklistoffset */
Raymond Hettinger352f9472003-04-24 15:50:11 +0000615 PyObject_SelfIter, /* tp_iter */
616 (iternextfunc)IO_iternext, /* tp_iternext */
617 I_methods /* tp_methods */
Guido van Rossum049cd901996-12-05 23:30:48 +0000618};
619
Guido van Rossum142eeb81997-08-13 03:14:41 +0000620static PyObject *
621newIobject(PyObject *s) {
622 Iobject *self;
623 char *buf;
624 int size;
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000625
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000626 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
627 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000628 s->ob_type->tp_name);
629 return NULL;
630 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000631 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000632 Py_INCREF(s);
633 self->buf=buf;
634 self->string_size=size;
635 self->pbuf=s;
636 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000637
638 return (PyObject*)self;
639}
640
Guido van Rossum049cd901996-12-05 23:30:48 +0000641/* End of code for StringI objects */
642/* -------------------------------------------------------- */
643
644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000645PyDoc_STRVAR(IO_StringIO__doc__,
646"StringIO([s]) -- Return a StringIO-like stream for reading or writing");
Guido van Rossum049cd901996-12-05 23:30:48 +0000647
648static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000649IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000650 PyObject *s=0;
651
Raymond Hettinger36cd2bf2003-01-03 08:24:58 +0000652 if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
Jim Fultone60de4d2000-10-06 19:24:23 +0000653
654 if (s) return newIobject(s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000655 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000656}
657
658/* List of methods defined in the module */
659
660static struct PyMethodDef IO_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000661 {"StringIO", (PyCFunction)IO_StringIO,
662 METH_VARARGS, IO_StringIO__doc__},
Guido van Rossum55702f81997-01-06 22:57:52 +0000663 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000664};
665
666
667/* Initialization function for the module (*must* be called initcStringIO) */
668
Guido van Rossum154417e1997-04-09 17:35:33 +0000669static struct PycStringIO_CAPI CAPI = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000670 IO_cread,
671 IO_creadline,
Guido van Rossum154417e1997-04-09 17:35:33 +0000672 O_cwrite,
Jim Fultone60de4d2000-10-06 19:24:23 +0000673 IO_cgetval,
Guido van Rossum154417e1997-04-09 17:35:33 +0000674 newOobject,
675 newIobject,
676 &Itype,
677 &Otype,
678};
679
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000680#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
681#define PyMODINIT_FUNC void
Guido van Rossum476e49f1998-12-15 21:43:15 +0000682#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000683PyMODINIT_FUNC
Thomas Wouters58d05102000-07-24 14:43:35 +0000684initcStringIO(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000685 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000686
Guido van Rossum154417e1997-04-09 17:35:33 +0000687
Guido van Rossum049cd901996-12-05 23:30:48 +0000688 /* Create the module and add the functions */
689 m = Py_InitModule4("cStringIO", IO_methods,
690 cStringIO_module_documentation,
691 (PyObject*)NULL,PYTHON_API_VERSION);
692
693 /* Add some symbolic constants to the module */
694 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000695
Guido van Rossum55702f81997-01-06 22:57:52 +0000696 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000697 Itype.ob_type=&PyType_Type;
698 Otype.ob_type=&PyType_Type;
Raymond Hettinger352f9472003-04-24 15:50:11 +0000699 if (PyType_Ready(&Otype) < 0) return;
700 if (PyType_Ready(&Itype) < 0) return;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000701 PyDict_SetItemString(d,"cStringIO_CAPI",
702 v = PyCObject_FromVoidPtr(&CAPI,NULL));
703 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000704
705 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000706 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
707 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000708
709 /* Maybe make certain warnings go away */
Jim Fultone60de4d2000-10-06 19:24:23 +0000710 if (0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000711}