blob: 2f85f417d9f6abf5f375d7029cd5b2c0d616585f [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 *
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
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;
118 UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
119
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 *
Jim Fultone60de4d2000-10-06 19:24:23 +0000132IO_isatty(IOobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000133
Jim Fultone60de4d2000-10-06 19:24:23 +0000134 UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000135
Jim Fultone60de4d2000-10-06 19:24:23 +0000136 return PyInt_FromLong(0);
Guido van Rossum049cd901996-12-05 23:30:48 +0000137}
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(IO_read__doc__,
140"read([s]) -- Read s characters, or the rest of the string");
Guido van Rossum049cd901996-12-05 23:30:48 +0000141
142static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000143IO_cread(PyObject *self, char **output, int n) {
144 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000145
Jim Fultone60de4d2000-10-06 19:24:23 +0000146 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
147 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
148 if (n < 0 || n > l) {
149 n = l;
150 if (n < 0) n=0;
151 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000152
Jim Fultone60de4d2000-10-06 19:24:23 +0000153 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
154 ((IOobject*)self)->pos += n;
155 return n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000156}
157
158static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000159IO_read(IOobject *self, PyObject *args) {
160 int n = -1;
161 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000162
Jim Fultone60de4d2000-10-06 19:24:23 +0000163 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000164
Jim Fultone60de4d2000-10-06 19:24:23 +0000165 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000166
Jim Fultone60de4d2000-10-06 19:24:23 +0000167 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000168}
169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000170PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
Guido van Rossum049cd901996-12-05 23:30:48 +0000171
172static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000173IO_creadline(PyObject *self, char **output) {
174 char *n, *s;
175 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000176
Jim Fultone60de4d2000-10-06 19:24:23 +0000177 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
178
179 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
180 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
181 n < s && *n != '\n'; n++);
182 if (n < s) n++;
183
184 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
185 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
186 ((IOobject*)self)->pos += l;
187 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000188}
189
190static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000191IO_readline(IOobject *self, PyObject *args) {
192 int n, m=-1;
193 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000194
Jim Fultone60de4d2000-10-06 19:24:23 +0000195 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
196
197 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
198 if (m >= 0 && m < n) {
199 m = n - m;
200 n -= m;
201 self->pos -= m;
202 }
203 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000204}
205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000207
208static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000209IO_readlines(IOobject *self, PyObject *args) {
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000210 int n;
211 char *output;
212 PyObject *result, *line;
213 int hint = 0, length = 0;
214
Jim Fultone60de4d2000-10-06 19:24:23 +0000215 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
216
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000217 result = PyList_New(0);
218 if (!result)
219 return NULL;
220
Jim Fultone60de4d2000-10-06 19:24:23 +0000221 while (1){
222 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
223 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000224 if (n == 0)
225 break;
226 line = PyString_FromStringAndSize (output, n);
Jim Fultone60de4d2000-10-06 19:24:23 +0000227 if (!line)
228 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000229 PyList_Append (result, line);
230 Py_DECREF (line);
231 length += n;
232 if (hint > 0 && length >= hint)
233 break;
234 }
235 return result;
Jim Fultone60de4d2000-10-06 19:24:23 +0000236 err:
237 Py_DECREF(result);
238 return NULL;
239}
240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000241PyDoc_STRVAR(IO_reset__doc__,
242"reset() -- Reset the file position to the beginning");
Jim Fultone60de4d2000-10-06 19:24:23 +0000243
244static PyObject *
245IO_reset(IOobject *self, PyObject *args) {
246
247 UNLESS (IO__opencheck(self)) return NULL;
248 UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL;
249
250 self->pos = 0;
251
252 Py_INCREF(Py_None);
253 return Py_None;
254}
255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000257
258static PyObject *
259IO_tell(IOobject *self, PyObject *args) {
260
261 UNLESS (IO__opencheck(self)) return NULL;
262 UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL;
263
264 return PyInt_FromLong(self->pos);
265}
266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000267PyDoc_STRVAR(IO_truncate__doc__,
268"truncate(): truncate the file at the current position.");
Jim Fultone60de4d2000-10-06 19:24:23 +0000269
270static PyObject *
271IO_truncate(IOobject *self, PyObject *args) {
272 int pos = -1;
273
274 UNLESS (IO__opencheck(self)) return NULL;
275 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
276 if (pos < 0) pos = self->pos;
277
278 if (self->string_size > pos) self->string_size = pos;
279
280 Py_INCREF(Py_None);
281 return Py_None;
282}
283
284
285
286
287/* Read-write object methods */
288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289PyDoc_STRVAR(O_seek__doc__,
Jim Fultone60de4d2000-10-06 19:24:23 +0000290"seek(position) -- set the current position\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
Jim Fultone60de4d2000-10-06 19:24:23 +0000292
293static PyObject *
294O_seek(Oobject *self, PyObject *args) {
295 int position, mode = 0;
296
297 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
298 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
299 return NULL;
300
301 if (mode == 2) {
302 position += self->string_size;
303 }
304 else if (mode == 1) {
305 position += self->pos;
306 }
307
308 if (position > self->buf_size) {
309 self->buf_size*=2;
310 if (self->buf_size <= position) self->buf_size=position+1;
311 UNLESS (self->buf=(char*)
312 realloc(self->buf,self->buf_size*sizeof(char))) {
313 self->buf_size=self->pos=0;
314 return PyErr_NoMemory();
315 }
316 }
317 else if (position < 0) position=0;
318
319 self->pos=position;
320
321 while (--position >= self->string_size) self->buf[position]=0;
322
323 Py_INCREF(Py_None);
324 return Py_None;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000325}
326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327PyDoc_STRVAR(O_write__doc__,
Guido van Rossum049cd901996-12-05 23:30:48 +0000328"write(s) -- Write a string to the file"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000329"\n\nNote (hack:) writing None resets the buffer");
Guido van Rossum049cd901996-12-05 23:30:48 +0000330
331
332static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000333O_cwrite(PyObject *self, char *c, int l) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000334 int newl;
Guido van Rossum2f098122001-12-07 20:20:28 +0000335 Oobject *oself;
Guido van Rossum55702f81997-01-06 22:57:52 +0000336
Jim Fultone60de4d2000-10-06 19:24:23 +0000337 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
Guido van Rossum2f098122001-12-07 20:20:28 +0000338 oself = (Oobject *)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000339
Guido van Rossum2f098122001-12-07 20:20:28 +0000340 newl = oself->pos+l;
341 if (newl >= oself->buf_size) {
342 oself->buf_size *= 2;
343 if (oself->buf_size <= newl)
344 oself->buf_size = newl+1;
345 UNLESS (oself->buf =
346 (char*)realloc(oself->buf,
347 (oself->buf_size) * sizeof(char))) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000348 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum2f098122001-12-07 20:20:28 +0000349 oself->buf_size = oself->pos = 0;
Jim Fultone60de4d2000-10-06 19:24:23 +0000350 return -1;
351 }
352 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000353
Guido van Rossum2f098122001-12-07 20:20:28 +0000354 memcpy(oself->buf+oself->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000355
Guido van Rossum2f098122001-12-07 20:20:28 +0000356 oself->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000357
Guido van Rossum2f098122001-12-07 20:20:28 +0000358 if (oself->string_size < oself->pos) {
359 oself->string_size = oself->pos;
360 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000361
362 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000363}
364
365static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000366O_write(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000367 char *c;
368 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000369
Guido van Rossuma883a3d2002-04-29 13:54:48 +0000370 UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000371
Jim Fultone60de4d2000-10-06 19:24:23 +0000372 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000373
Jim Fultone60de4d2000-10-06 19:24:23 +0000374 Py_INCREF(Py_None);
375 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000376}
377
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000379
380static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000381O_close(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000382
Jim Fultone60de4d2000-10-06 19:24:23 +0000383 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000384
Jim Fultone60de4d2000-10-06 19:24:23 +0000385 if (self->buf != NULL) free(self->buf);
386 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000387
Jim Fultone60de4d2000-10-06 19:24:23 +0000388 self->pos = self->string_size = self->buf_size = 0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000389
Jim Fultone60de4d2000-10-06 19:24:23 +0000390 Py_INCREF(Py_None);
391 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000392}
393
394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(O_writelines__doc__,
396"writelines(sequence_of_strings): write each string");
Guido van Rossum049cd901996-12-05 23:30:48 +0000397static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000398O_writelines(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000399 PyObject *tmp = 0;
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000400 static PyObject *joiner = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000401
Jim Fultone60de4d2000-10-06 19:24:23 +0000402 UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000403
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000404 if (!joiner) {
405 PyObject *empty_string = PyString_FromString("");
406 if (empty_string == NULL)
407 return NULL;
408 joiner = PyObject_GetAttrString(empty_string, "join");
409 Py_DECREF(empty_string);
410 if (joiner == NULL)
411 return NULL;
412 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000413
Jim Fultone60de4d2000-10-06 19:24:23 +0000414 if (PyObject_Size(args) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000415
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000416 tmp = PyObject_CallFunction(joiner, "O", args);
Jim Fultone60de4d2000-10-06 19:24:23 +0000417 UNLESS (tmp) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000418
Jim Fultone60de4d2000-10-06 19:24:23 +0000419 args = Py_BuildValue("(O)", tmp);
420 Py_DECREF(tmp);
421 UNLESS (args) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000422
Jim Fultone60de4d2000-10-06 19:24:23 +0000423 tmp = O_write(self, args);
424 Py_DECREF(args);
425 return tmp;
Guido van Rossum049cd901996-12-05 23:30:48 +0000426}
427
428static struct PyMethodDef O_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000429 /* Common methods: */
430 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
431 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
432 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
433 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
434 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
435 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
436 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
437 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
438 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
439
440 /* Read-write StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000441 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000442 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
443 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000444 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
445 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000446};
447
Guido van Rossum049cd901996-12-05 23:30:48 +0000448static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000449O_dealloc(Oobject *self) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000450 if (self->buf != NULL)
451 free(self->buf);
452 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000453}
454
455static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000456O_getattr(Oobject *self, char *name) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000457 if (strcmp(name, "softspace") == 0) {
458 return PyInt_FromLong(self->softspace);
459 }
460 return Py_FindMethod(O_methods, (PyObject *)self, name);
Guido van Rossum049cd901996-12-05 23:30:48 +0000461}
462
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000463static int
464O_setattr(Oobject *self, char *name, PyObject *value) {
465 long x;
466 if (strcmp(name, "softspace") != 0) {
467 PyErr_SetString(PyExc_AttributeError, name);
468 return -1;
469 }
470 x = PyInt_AsLong(value);
Jim Fultone60de4d2000-10-06 19:24:23 +0000471 if (x < 0 && PyErr_Occurred())
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000472 return -1;
473 self->softspace = x;
474 return 0;
475}
476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000477PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
Guido van Rossum049cd901996-12-05 23:30:48 +0000478
479static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000480 PyObject_HEAD_INIT(NULL)
481 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000482 "cStringIO.StringO", /*tp_name*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000483 sizeof(Oobject), /*tp_basicsize*/
484 0, /*tp_itemsize*/
485 /* methods */
486 (destructor)O_dealloc, /*tp_dealloc*/
487 (printfunc)0, /*tp_print*/
488 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000489 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000490 (cmpfunc)0, /*tp_compare*/
491 (reprfunc)0, /*tp_repr*/
492 0, /*tp_as_number*/
493 0, /*tp_as_sequence*/
494 0, /*tp_as_mapping*/
495 (hashfunc)0, /*tp_hash*/
496 (ternaryfunc)0, /*tp_call*/
497 (reprfunc)0, /*tp_str*/
498
499 /* Space for future expansion */
500 0L,0L,0L,0L,
501 Otype__doc__ /* Documentation string */
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 *
Guido van Rossum154417e1997-04-09 17:35:33 +0000529I_close(Iobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000530
Jim Fultone60de4d2000-10-06 19:24:23 +0000531 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000532
Jim Fultone60de4d2000-10-06 19:24:23 +0000533 Py_XDECREF(self->pbuf);
534 self->pbuf = NULL;
535 self->buf = NULL;
536
537 self->pos = self->string_size = 0;
538
539 Py_INCREF(Py_None);
540 return Py_None;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000541}
542
543static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000544I_seek(Iobject *self, PyObject *args) {
545 int position, mode = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000546
Jim Fultone60de4d2000-10-06 19:24:23 +0000547 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
548 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
549 return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000550
Jim Fultone60de4d2000-10-06 19:24:23 +0000551 if (mode == 2) position += self->string_size;
552 else if (mode == 1) position += self->pos;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000553
Jim Fultone60de4d2000-10-06 19:24:23 +0000554 if (position < 0) position=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000555
Jim Fultone60de4d2000-10-06 19:24:23 +0000556 self->pos=position;
557
558 Py_INCREF(Py_None);
559 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000560}
561
562static struct PyMethodDef I_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000563 /* Common methods: */
564 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
565 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
566 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
567 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
568 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
569 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
570 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
571 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
572 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
573
574 /* Read-only StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000575 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000576 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000577 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000578};
579
Guido van Rossum049cd901996-12-05 23:30:48 +0000580static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000581I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000582 Py_XDECREF(self->pbuf);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000583 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000584}
585
586static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000587I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000588 return Py_FindMethod(I_methods, (PyObject *)self, name);
589}
590
Barry Warsaw3e8be722001-09-22 04:36:49 +0000591static PyObject *
592I_getiter(Iobject *self)
593{
594 PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
595 "readline");
596 PyObject *emptystring = PyString_FromString("");
597 PyObject *iter = NULL;
598 if (!myreadline || !emptystring)
599 goto finally;
600
601 iter = PyCallIter_New(myreadline, emptystring);
602 finally:
603 Py_XDECREF(myreadline);
604 Py_XDECREF(emptystring);
605 return iter;
606}
607
608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000609PyDoc_STRVAR(Itype__doc__,
610"Simple type for treating strings as input file streams");
Guido van Rossum049cd901996-12-05 23:30:48 +0000611
612static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000613 PyObject_HEAD_INIT(NULL)
Barry Warsaw3e8be722001-09-22 04:36:49 +0000614 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000615 "cStringIO.StringI", /*tp_name*/
Barry Warsaw3e8be722001-09-22 04:36:49 +0000616 sizeof(Iobject), /*tp_basicsize*/
617 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000618 /* methods */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000619 (destructor)I_dealloc, /*tp_dealloc*/
620 (printfunc)0, /*tp_print*/
621 (getattrfunc)I_getattr, /*tp_getattr*/
622 (setattrfunc)0, /*tp_setattr*/
623 (cmpfunc)0, /*tp_compare*/
624 (reprfunc)0, /*tp_repr*/
625 0, /*tp_as_number*/
626 0, /*tp_as_sequence*/
627 0, /*tp_as_mapping*/
628 (hashfunc)0, /*tp_hash*/
629 (ternaryfunc)0, /*tp_call*/
630 (reprfunc)0, /*tp_str*/
631 0, /* tp_getattro */
632 0, /* tp_setattro */
633 0, /* tp_as_buffer */
634 Py_TPFLAGS_DEFAULT, /* tp_flags */
635 Itype__doc__, /* tp_doc */
636 0, /* tp_traverse */
637 0, /* tp_clear */
638 0, /* tp_richcompare */
639 0, /* tp_weaklistoffset */
640 (getiterfunc)I_getiter, /* tp_iter */
641 0, /* tp_iternext */
Guido van Rossum049cd901996-12-05 23:30:48 +0000642};
643
Guido van Rossum142eeb81997-08-13 03:14:41 +0000644static PyObject *
645newIobject(PyObject *s) {
646 Iobject *self;
647 char *buf;
648 int size;
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000649
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000650 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
651 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000652 s->ob_type->tp_name);
653 return NULL;
654 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000655 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000656 Py_INCREF(s);
657 self->buf=buf;
658 self->string_size=size;
659 self->pbuf=s;
660 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000661
662 return (PyObject*)self;
663}
664
Guido van Rossum049cd901996-12-05 23:30:48 +0000665/* End of code for StringI objects */
666/* -------------------------------------------------------- */
667
668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(IO_StringIO__doc__,
670"StringIO([s]) -- Return a StringIO-like stream for reading or writing");
Guido van Rossum049cd901996-12-05 23:30:48 +0000671
672static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000673IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000674 PyObject *s=0;
675
Jim Fultone60de4d2000-10-06 19:24:23 +0000676 if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
677
678 if (s) return newIobject(s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000679 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000680}
681
682/* List of methods defined in the module */
683
684static struct PyMethodDef IO_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000685 {"StringIO", (PyCFunction)IO_StringIO,
686 METH_VARARGS, IO_StringIO__doc__},
Guido van Rossum55702f81997-01-06 22:57:52 +0000687 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000688};
689
690
691/* Initialization function for the module (*must* be called initcStringIO) */
692
Guido van Rossum154417e1997-04-09 17:35:33 +0000693static struct PycStringIO_CAPI CAPI = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000694 IO_cread,
695 IO_creadline,
Guido van Rossum154417e1997-04-09 17:35:33 +0000696 O_cwrite,
Jim Fultone60de4d2000-10-06 19:24:23 +0000697 IO_cgetval,
Guido van Rossum154417e1997-04-09 17:35:33 +0000698 newOobject,
699 newIobject,
700 &Itype,
701 &Otype,
702};
703
Guido van Rossum476e49f1998-12-15 21:43:15 +0000704#ifndef DL_EXPORT /* declarations for DLL import/export */
705#define DL_EXPORT(RTYPE) RTYPE
706#endif
Guido van Rossum3886bb61998-12-04 18:50:17 +0000707DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +0000708initcStringIO(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000709 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000710
Guido van Rossum154417e1997-04-09 17:35:33 +0000711
Guido van Rossum049cd901996-12-05 23:30:48 +0000712 /* Create the module and add the functions */
713 m = Py_InitModule4("cStringIO", IO_methods,
714 cStringIO_module_documentation,
715 (PyObject*)NULL,PYTHON_API_VERSION);
716
717 /* Add some symbolic constants to the module */
718 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000719
Guido van Rossum55702f81997-01-06 22:57:52 +0000720 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000721 Itype.ob_type=&PyType_Type;
722 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000723 PyDict_SetItemString(d,"cStringIO_CAPI",
724 v = PyCObject_FromVoidPtr(&CAPI,NULL));
725 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000726
727 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000728 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
729 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000730
731 /* Maybe make certain warnings go away */
Jim Fultone60de4d2000-10-06 19:24:23 +0000732 if (0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000733}