blob: 107f889bf87b27d26ccbcd2350d4b811db9a03c6 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001/*
2
3 $Id$
4
5 A simple fast partial StringIO replacement.
6
7
8
9 Copyright
10
11 Copyright 1996 Digital Creations, L.C., 910 Princess Anne
12 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
13 rights reserved. Copyright in this software is owned by DCLC,
14 unless otherwise indicated. Permission to use, copy and
15 distribute this software is hereby granted, provided that the
16 above copyright notice appear in all copies and that both that
17 copyright notice and this permission notice appear. Note that
18 any product, process or technology described in this software
19 may be the subject of other Intellectual Property rights
20 reserved by Digital Creations, L.C. and are not licensed
21 hereunder.
22
23 Trademarks
24
25 Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
26 All other trademarks are owned by their respective companies.
27
28 No Warranty
29
30 The software is provided "as is" without warranty of any kind,
31 either express or implied, including, but not limited to, the
32 implied warranties of merchantability, fitness for a particular
33 purpose, or non-infringement. This software could include
34 technical inaccuracies or typographical errors. Changes are
35 periodically made to the software; these changes will be
36 incorporated in new editions of the software. DCLC may make
37 improvements and/or changes in this software at any time
38 without notice.
39
40 Limitation Of Liability
41
42 In no event will DCLC be liable for direct, indirect, special,
43 incidental, economic, cover, or consequential damages arising
44 out of the use of or inability to use this software even if
45 advised of the possibility of such damages. Some states do not
46 allow the exclusion or limitation of implied warranties or
47 limitation of liability for incidental or consequential
48 damages, so the above limitation or exclusion may not apply to
49 you.
50
51 If you have questions regarding this software,
52 contact:
53
54 Jim Fulton, jim@digicool.com
55 Digital Creations L.C.
56
57 (540) 371-6909
58
59
60 $Log$
Guido van Rossum154417e1997-04-09 17:35:33 +000061 Revision 2.4 1997/04/09 17:35:33 guido
62 Unknown changes by Jim Fulton.
Barry Warsaw61a63e11997-01-14 17:38:28 +000063
Guido van Rossum154417e1997-04-09 17:35:33 +000064 Revision 1.16 1997/02/17 22:17:43 jim
65 *** empty log message ***
Barry Warsaw61a63e11997-01-14 17:38:28 +000066
Guido van Rossum154417e1997-04-09 17:35:33 +000067 Revision 1.14 1997/01/24 19:56:24 chris
68 undid last change
69
70 Revision 1.13 1997/01/24 19:45:20 chris
71 extra byte in buffer no longer included in buf_size
72
73 Revision 1.12 1997/01/24 19:38:28 chris
74 *** empty log message ***
75
76 Revision 1.11 1997/01/23 20:45:01 jim
77 ANSIfied it.
78 Changed way C API was exported.
Guido van Rossum55702f81997-01-06 22:57:52 +000079
80 Revision 1.10 1997/01/02 15:19:55 chris
81 checked in to be sure repository is up to date.
82
83 Revision 1.9 1996/12/27 21:40:29 jim
84 Took out some lamosities in interface, like returning self from
85 write.
86
87 Revision 1.8 1996/12/23 15:52:49 jim
88 Added ifdef to check for CObject before using it.
89
90 Revision 1.7 1996/12/23 15:22:35 jim
91 Finished implementation, adding full compatibility with StringIO, and
92 then some.
93
94 We still need to take out some cStringIO oddities.
Guido van Rossum049cd901996-12-05 23:30:48 +000095
96 Revision 1.6 1996/10/15 18:42:07 jim
97 Added lots of casts to make warnings go away.
98
99 Revision 1.5 1996/10/11 21:03:42 jim
100 *** empty log message ***
101
102 Revision 1.4 1996/10/11 21:02:15 jim
103 *** empty log message ***
104
105 Revision 1.3 1996/10/07 20:51:38 chris
106 *** empty log message ***
107
108 Revision 1.2 1996/07/18 13:08:34 jfulton
109 *** empty log message ***
110
111 Revision 1.1 1996/07/15 17:06:33 jfulton
112 Initial version.
113
114
115*/
116static char cStringIO_module_documentation[] =
117"A simple fast partial StringIO replacement.\n"
118"\n"
119"This module provides a simple useful replacement for\n"
120"the StringIO module that is written in C. It does not provide the\n"
121"full generality if StringIO, but it provides anough for most\n"
122"applications and is especially useful in conjuction with the\n"
123"pickle module.\n"
124"\n"
125"Usage:\n"
126"\n"
127" from cStringIO import StringIO\n"
128"\n"
129" an_output_stream=StringIO()\n"
130" an_output_stream.write(some_stuff)\n"
131" ...\n"
132" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
133"\n"
134" an_input_stream=StringIO(a_string)\n"
135" spam=an_input_stream.readline()\n"
136" spam=an_input_stream.read(5)\n"
137" an_input_stream.reset() # OK, start over, note no seek yet\n"
138" spam=an_input_stream.read() # and read it all\n"
139" \n"
140"If someone else wants to provide a more complete implementation,\n"
141"go for it. :-) \n"
142;
143
144#include "Python.h"
145#include "import.h"
Guido van Rossum154417e1997-04-09 17:35:33 +0000146#include "cStringIO.h"
Guido van Rossum049cd901996-12-05 23:30:48 +0000147
Guido van Rossum049cd901996-12-05 23:30:48 +0000148#define UNLESS(E) if(!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +0000149
150/* ----------------------------------------------------- */
151
152/* Declarations for objects of type StringO */
153
154typedef struct {
155 PyObject_HEAD
156 char *buf;
157 int pos, string_size, buf_size, closed;
158} Oobject;
159
160staticforward PyTypeObject Otype;
161
162/* ---------------------------------------------------------------- */
163
164/* Declarations for objects of type StringI */
165
166typedef struct {
167 PyObject_HEAD
168 char *buf;
169 int pos, string_size, closed;
170 PyObject *pbuf;
171} Iobject;
172
173staticforward PyTypeObject Itype;
174
Guido van Rossum049cd901996-12-05 23:30:48 +0000175/* ---------------------------------------------------------------- */
176
177static char O_reset__doc__[] =
178"reset() -- Reset the file position to the beginning"
179;
180
181static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000182O_reset(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000183 self->pos = 0;
184
185 Py_INCREF(Py_None);
186 return Py_None;
187}
188
189
190static char O_tell__doc__[] =
191"tell() -- get the current position.";
192
193static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000194O_tell(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000195 return PyInt_FromLong(self->pos);
196}
197
198
199static char O_seek__doc__[] =
200"seek(position) -- set the current position\n"
201"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
202
203static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000204O_seek(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000205 int position, mode = 0;
206
207 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode))
208 {
209 return NULL;
210 }
211
212 if (mode == 2)
213 {
214 position += self->string_size;
215 }
216 else if (mode == 1)
217 {
218 position += self->pos;
219 }
220
221 self->pos = (position > self->string_size ? self->string_size :
222 (position < 0 ? 0 : position));
223
Guido van Rossum55702f81997-01-06 22:57:52 +0000224 Py_INCREF(Py_None);
225 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000226}
227
228static char O_read__doc__[] =
229"read([s]) -- Read s characters, or the rest of the string"
230;
231
232static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000233O_cread(PyObject *self, char **output, int n) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000234 int l;
235
Guido van Rossum154417e1997-04-09 17:35:33 +0000236 l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
Guido van Rossum049cd901996-12-05 23:30:48 +0000237 if (n < 0 || n > l)
238 {
239 n = l;
240 }
241
Guido van Rossum154417e1997-04-09 17:35:33 +0000242 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
243 ((Oobject*)self)->pos += n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000244 return n;
245}
246
247static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000248O_read(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000249 int n = -1;
250 char *output;
251
252 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
253
Guido van Rossum154417e1997-04-09 17:35:33 +0000254 n=O_cread((PyObject*)self,&output,n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000255
256 return PyString_FromStringAndSize(output, n);
257}
258
259
260static char O_readline__doc__[] =
261"readline() -- Read one line"
262;
263
264static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000265O_creadline(PyObject *self, char **output) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000266 char *n, *s;
267 int l;
268
Guido van Rossum154417e1997-04-09 17:35:33 +0000269 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos,
270 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size;
Guido van Rossum049cd901996-12-05 23:30:48 +0000271 n < s && *n != '\n'; n++);
272 if (n < s) n++;
273
Guido van Rossum154417e1997-04-09 17:35:33 +0000274 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
275 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos;
276 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000277 return l;
278}
279
280static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000281O_readline(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000282 int n;
283 char *output;
284
Guido van Rossum154417e1997-04-09 17:35:33 +0000285 n=O_creadline((PyObject*)self,&output);
Guido van Rossum049cd901996-12-05 23:30:48 +0000286 return PyString_FromStringAndSize(output, n);
287}
288
289static char O_write__doc__[] =
290"write(s) -- Write a string to the file"
291"\n\nNote (hack:) writing None resets the buffer"
292;
293
294
295static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000296O_cwrite(PyObject *self, char *c, int l) {
Barry Warsaw61a63e11997-01-14 17:38:28 +0000297 int newl;
Guido van Rossum55702f81997-01-06 22:57:52 +0000298
Guido van Rossum154417e1997-04-09 17:35:33 +0000299 newl=((Oobject*)self)->pos+l;
300 if(newl >= ((Oobject*)self)->buf_size)
Guido van Rossum049cd901996-12-05 23:30:48 +0000301 {
Guido van Rossum154417e1997-04-09 17:35:33 +0000302 ((Oobject*)self)->buf_size*=2;
303 if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
304 UNLESS(((Oobject*)self)->buf=
305 (char*)realloc(((Oobject*)self)->buf,
306 (((Oobject*)self)->buf_size) *sizeof(char)))
Guido van Rossum049cd901996-12-05 23:30:48 +0000307 {
308 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum154417e1997-04-09 17:35:33 +0000309 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000310 return -1;
311 }
312 }
313
Guido van Rossum154417e1997-04-09 17:35:33 +0000314 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000315
Guido van Rossum154417e1997-04-09 17:35:33 +0000316 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000317
Guido van Rossum154417e1997-04-09 17:35:33 +0000318 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos)
Guido van Rossum049cd901996-12-05 23:30:48 +0000319 {
Guido van Rossum154417e1997-04-09 17:35:33 +0000320 ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
Guido van Rossum049cd901996-12-05 23:30:48 +0000321 }
322
323 return l;
324}
325
326static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000327O_write(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000328 PyObject *s;
Barry Warsaw61a63e11997-01-14 17:38:28 +0000329 char *c;
330 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000331
332 UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
Guido van Rossum55702f81997-01-06 22:57:52 +0000333 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
334 UNLESS(c=PyString_AsString(s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000335 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000336
Guido van Rossum55702f81997-01-06 22:57:52 +0000337 Py_INCREF(Py_None);
338 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000339}
340
341static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000342O_getval(Oobject *self, PyObject *args) {
343 return PyString_FromStringAndSize(self->buf, self->pos);
344}
345
346static PyObject *
347O_cgetval(PyObject *self) {
348 return PyString_FromStringAndSize(((Oobject*)self)->buf,
349 ((Oobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000350}
351
352static char O_truncate__doc__[] =
353"truncate(): truncate the file at the current position.";
354
355static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000356O_truncate(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000357 self->string_size = self->pos;
358 Py_INCREF(Py_None);
359 return Py_None;
360}
361
362static char O_isatty__doc__[] = "isatty(): always returns 0";
363
364static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000365O_isatty(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000366 return PyInt_FromLong(0);
367}
368
369static char O_close__doc__[] = "close(): explicitly release resources held.";
370
371static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000372O_close(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000373 free(self->buf);
374
375 self->pos = self->string_size = self->buf_size = 0;
376 self->closed = 1;
377
378 Py_INCREF(Py_None);
379 return Py_None;
380}
381
382static char O_flush__doc__[] = "flush(): does nothing.";
383
384static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000385O_flush(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000386 Py_INCREF(Py_None);
387 return Py_None;
388}
389
390
391static char O_writelines__doc__[] = "blah";
392static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000393O_writelines(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000394 PyObject *string_module = 0;
395 static PyObject *string_joinfields = 0;
396
397 UNLESS(PyArg_Parse(args, "O", args))
398 {
399 return NULL;
400 }
401
402 if (!string_joinfields)
403 {
404 UNLESS(string_module = PyImport_ImportModule("string"))
405 {
406 return NULL;
407 }
408
409 UNLESS(string_joinfields=
410 PyObject_GetAttrString(string_module, "joinfields"))
411 {
412 return NULL;
413 }
414
415 Py_DECREF(string_module);
416 }
417
418 if (PyObject_Length(args) == -1)
419 {
420 return NULL;
421 }
422
423 return O_write(self,
424 PyObject_CallFunction(string_joinfields, "Os", args, ""));
425}
426
427static struct PyMethodDef O_methods[] = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000428 {"write", (PyCFunction)O_write, 0, O_write__doc__},
Guido van Rossum049cd901996-12-05 23:30:48 +0000429 {"read", (PyCFunction)O_read, 1, O_read__doc__},
430 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
Guido van Rossum049cd901996-12-05 23:30:48 +0000431 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
432 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
433 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
434 {"getvalue", (PyCFunction)O_getval, 0, "getvalue() -- Get the string value"},
435 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
436 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
437 {"close", (PyCFunction)O_close, 0, O_close__doc__},
438 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
439 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__},
440 {NULL, NULL} /* sentinel */
441};
442
443/* ---------- */
444
445
Guido van Rossum154417e1997-04-09 17:35:33 +0000446static PyObject *
447newOobject(int size) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000448 Oobject *self;
449
450 self = PyObject_NEW(Oobject, &Otype);
451 if (self == NULL)
452 return NULL;
453 self->pos=0;
454 self->closed = 0;
455 self->string_size = 0;
456
457 UNLESS(self->buf=malloc(size*sizeof(char)))
458 {
459 PyErr_SetString(PyExc_MemoryError,"out of memory");
460 self->buf_size = 0;
461 return NULL;
462 }
463
464 self->buf_size=size;
Guido van Rossum154417e1997-04-09 17:35:33 +0000465 return (PyObject*)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000466}
467
468
469static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000470O_dealloc(Oobject *self) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000471 free(self->buf);
472 PyMem_DEL(self);
473}
474
475static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000476O_getattr(Oobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000477 return Py_FindMethod(O_methods, (PyObject *)self, name);
478}
479
480static char Otype__doc__[] =
481"Simple type for output to strings."
482;
483
484static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000485 PyObject_HEAD_INIT(NULL)
486 0, /*ob_size*/
487 "StringO", /*tp_name*/
488 sizeof(Oobject), /*tp_basicsize*/
489 0, /*tp_itemsize*/
490 /* methods */
491 (destructor)O_dealloc, /*tp_dealloc*/
492 (printfunc)0, /*tp_print*/
493 (getattrfunc)O_getattr, /*tp_getattr*/
494 (setattrfunc)0, /*tp_setattr*/
495 (cmpfunc)0, /*tp_compare*/
496 (reprfunc)0, /*tp_repr*/
497 0, /*tp_as_number*/
498 0, /*tp_as_sequence*/
499 0, /*tp_as_mapping*/
500 (hashfunc)0, /*tp_hash*/
501 (ternaryfunc)0, /*tp_call*/
502 (reprfunc)0, /*tp_str*/
503
504 /* Space for future expansion */
505 0L,0L,0L,0L,
506 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000507};
508
509/* End of code for StringO objects */
510/* -------------------------------------------------------- */
511
512static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000513I_close(Iobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000514 Py_DECREF(self->pbuf);
515
516 self->pos = self->string_size = 0;
517 self->closed = 1;
518
519 Py_INCREF(Py_None);
520 return Py_None;
521}
522
523static struct PyMethodDef I_methods[] = {
524 {"read", (PyCFunction)O_read, 1, O_read__doc__},
525 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
526 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
527 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
528 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
529 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
530 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
531 {"close", (PyCFunction)I_close, 0, O_close__doc__},
532 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
533 {NULL, NULL} /* sentinel */
534};
535
536/* ---------- */
537
538
Guido van Rossum154417e1997-04-09 17:35:33 +0000539static PyObject *
540newIobject(PyObject *s) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000541 Iobject *self;
542 char *buf;
543 int size;
544
545 UNLESS(buf=PyString_AsString(s)) return NULL;
546 UNLESS(-1 != (size=PyString_Size(s))) return NULL;
547 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
548 Py_INCREF(s);
549 self->buf=buf;
550 self->string_size=size;
551 self->pbuf=s;
552 self->pos=0;
553 self->closed = 0;
554
Guido van Rossum154417e1997-04-09 17:35:33 +0000555 return (PyObject*)self;
Guido van Rossum049cd901996-12-05 23:30:48 +0000556}
557
558
559static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000560I_dealloc(Iobject *self) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000561 Py_DECREF(self->pbuf);
562 PyMem_DEL(self);
563}
564
565static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000566I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000567 return Py_FindMethod(I_methods, (PyObject *)self, name);
568}
569
570static char Itype__doc__[] =
571"Simple type for treating strings as input file streams"
572;
573
574static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000575 PyObject_HEAD_INIT(NULL)
576 0, /*ob_size*/
577 "StringI", /*tp_name*/
578 sizeof(Iobject), /*tp_basicsize*/
579 0, /*tp_itemsize*/
580 /* methods */
581 (destructor)I_dealloc, /*tp_dealloc*/
582 (printfunc)0, /*tp_print*/
583 (getattrfunc)I_getattr, /*tp_getattr*/
584 (setattrfunc)0, /*tp_setattr*/
585 (cmpfunc)0, /*tp_compare*/
586 (reprfunc)0, /*tp_repr*/
587 0, /*tp_as_number*/
588 0, /*tp_as_sequence*/
589 0, /*tp_as_mapping*/
590 (hashfunc)0, /*tp_hash*/
591 (ternaryfunc)0, /*tp_call*/
592 (reprfunc)0, /*tp_str*/
593
594 /* Space for future expansion */
595 0L,0L,0L,0L,
596 Itype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000597};
598
599/* End of code for StringI objects */
600/* -------------------------------------------------------- */
601
602
603static char IO_StringIO__doc__[] =
604"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
605;
606
607static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000608IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000609 PyObject *s=0;
610
611 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000612 if(s) return newIobject(s);
613 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000614}
615
616/* List of methods defined in the module */
617
618static struct PyMethodDef IO_methods[] = {
Guido van Rossum55702f81997-01-06 22:57:52 +0000619 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
620 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000621};
622
623
624/* Initialization function for the module (*must* be called initcStringIO) */
625
Guido van Rossum154417e1997-04-09 17:35:33 +0000626static struct PycStringIO_CAPI CAPI = {
627 O_cread,
628 O_creadline,
629 O_cwrite,
630 O_cgetval,
631 newOobject,
632 newIobject,
633 &Itype,
634 &Otype,
635};
636
Guido van Rossum049cd901996-12-05 23:30:48 +0000637void
Guido van Rossum154417e1997-04-09 17:35:33 +0000638initcStringIO() {
Guido van Rossum049cd901996-12-05 23:30:48 +0000639 PyObject *m, *d;
640
Guido van Rossum154417e1997-04-09 17:35:33 +0000641
Guido van Rossum049cd901996-12-05 23:30:48 +0000642 /* Create the module and add the functions */
643 m = Py_InitModule4("cStringIO", IO_methods,
644 cStringIO_module_documentation,
645 (PyObject*)NULL,PYTHON_API_VERSION);
646
647 /* Add some symbolic constants to the module */
648 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000649
Guido van Rossum55702f81997-01-06 22:57:52 +0000650 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000651 Itype.ob_type=&PyType_Type;
652 Otype.ob_type=&PyType_Type;
653 PyDict_SetItemString(d,"cStringIO_CAPI", PyCObject_FromVoidPtr(&CAPI,NULL));
654
655 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000656 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
657 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
658
Guido van Rossum049cd901996-12-05 23:30:48 +0000659 /* Check for errors */
Guido van Rossum154417e1997-04-09 17:35:33 +0000660 if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO");
Guido van Rossum049cd901996-12-05 23:30:48 +0000661}
Guido van Rossum55702f81997-01-06 22:57:52 +0000662