blob: f2750cc2b7af9dbf44b462a21549585471f94287 [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 Rossum55702f81997-01-06 22:57:52 +000061 Revision 2.2 1997/01/06 22:57:52 guido
62 Jim's latest version.
63
64 Revision 1.10 1997/01/02 15:19:55 chris
65 checked in to be sure repository is up to date.
66
67 Revision 1.9 1996/12/27 21:40:29 jim
68 Took out some lamosities in interface, like returning self from
69 write.
70
71 Revision 1.8 1996/12/23 15:52:49 jim
72 Added ifdef to check for CObject before using it.
73
74 Revision 1.7 1996/12/23 15:22:35 jim
75 Finished implementation, adding full compatibility with StringIO, and
76 then some.
77
78 We still need to take out some cStringIO oddities.
Guido van Rossum049cd901996-12-05 23:30:48 +000079
80 Revision 1.6 1996/10/15 18:42:07 jim
81 Added lots of casts to make warnings go away.
82
83 Revision 1.5 1996/10/11 21:03:42 jim
84 *** empty log message ***
85
86 Revision 1.4 1996/10/11 21:02:15 jim
87 *** empty log message ***
88
89 Revision 1.3 1996/10/07 20:51:38 chris
90 *** empty log message ***
91
92 Revision 1.2 1996/07/18 13:08:34 jfulton
93 *** empty log message ***
94
95 Revision 1.1 1996/07/15 17:06:33 jfulton
96 Initial version.
97
98
99*/
100static char cStringIO_module_documentation[] =
101"A simple fast partial StringIO replacement.\n"
102"\n"
103"This module provides a simple useful replacement for\n"
104"the StringIO module that is written in C. It does not provide the\n"
105"full generality if StringIO, but it provides anough for most\n"
106"applications and is especially useful in conjuction with the\n"
107"pickle module.\n"
108"\n"
109"Usage:\n"
110"\n"
111" from cStringIO import StringIO\n"
112"\n"
113" an_output_stream=StringIO()\n"
114" an_output_stream.write(some_stuff)\n"
115" ...\n"
116" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
117"\n"
118" an_input_stream=StringIO(a_string)\n"
119" spam=an_input_stream.readline()\n"
120" spam=an_input_stream.read(5)\n"
121" an_input_stream.reset() # OK, start over, note no seek yet\n"
122" spam=an_input_stream.read() # and read it all\n"
123" \n"
124"If someone else wants to provide a more complete implementation,\n"
125"go for it. :-) \n"
126;
127
128#include "Python.h"
129#include "import.h"
130
131static PyObject *ErrorObject;
132
Guido van Rossum55702f81997-01-06 22:57:52 +0000133#ifdef __cplusplus
134#define ARG(T,N) T N
135#define ARGDECL(T,N)
136#else
137#define ARG(T,N) N
138#define ARGDECL(T,N) T N;
139#endif
140
Guido van Rossum049cd901996-12-05 23:30:48 +0000141#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
142#define UNLESS(E) if(!(E))
143#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
144#define Py_ASSIGN(P,E) if(!PyObject_AssignExpression(&(P),(E))) return NULL
145
146/* ----------------------------------------------------- */
147
148/* Declarations for objects of type StringO */
149
150typedef struct {
151 PyObject_HEAD
152 char *buf;
153 int pos, string_size, buf_size, closed;
154} Oobject;
155
156staticforward PyTypeObject Otype;
157
158/* ---------------------------------------------------------------- */
159
160/* Declarations for objects of type StringI */
161
162typedef struct {
163 PyObject_HEAD
164 char *buf;
165 int pos, string_size, closed;
166 PyObject *pbuf;
167} Iobject;
168
169staticforward PyTypeObject Itype;
170
171
172
173/* ---------------------------------------------------------------- */
174
175static char O_reset__doc__[] =
176"reset() -- Reset the file position to the beginning"
177;
178
179static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000180O_reset(ARG(Oobject*, self), ARG(PyObject*, args))
181 ARGDECL(Oobject*, self)
182 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000183{
184 self->pos = 0;
185
186 Py_INCREF(Py_None);
187 return Py_None;
188}
189
190
191static char O_tell__doc__[] =
192"tell() -- get the current position.";
193
194static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000195O_tell(ARG(Oobject*, self), ARG(PyObject*, args))
196 ARGDECL(Oobject*, self)
197 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000198{
199 return PyInt_FromLong(self->pos);
200}
201
202
203static char O_seek__doc__[] =
204"seek(position) -- set the current position\n"
205"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
206
207static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000208O_seek(ARG(Oobject*, self), ARG(PyObject*, args))
209 ARGDECL(Oobject*, self)
210 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000211{
212 int position, mode = 0;
213
214 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode))
215 {
216 return NULL;
217 }
218
219 if (mode == 2)
220 {
221 position += self->string_size;
222 }
223 else if (mode == 1)
224 {
225 position += self->pos;
226 }
227
228 self->pos = (position > self->string_size ? self->string_size :
229 (position < 0 ? 0 : position));
230
Guido van Rossum55702f81997-01-06 22:57:52 +0000231 Py_INCREF(Py_None);
232 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000233}
234
235static char O_read__doc__[] =
236"read([s]) -- Read s characters, or the rest of the string"
237;
238
239static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000240O_cread(ARG(Oobject*, self), ARG(char**, output), ARG(int, n))
241 ARGDECL(Oobject*, self)
242 ARGDECL(char**, output)
243 ARGDECL(int, n)
Guido van Rossum049cd901996-12-05 23:30:48 +0000244{
245 int l;
246
247 l = self->string_size - self->pos;
248 if (n < 0 || n > l)
249 {
250 n = l;
251 }
252
253 *output=self->buf + self->pos;
254 self->pos += n;
255 return n;
256}
257
258static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000259O_read(ARG(Oobject*, self), ARG(PyObject*, args))
260 ARGDECL(Oobject*, self)
261 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000262{
263 int n = -1;
264 char *output;
265
266 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
267
268 n=O_cread(self,&output,n);
269
270 return PyString_FromStringAndSize(output, n);
271}
272
273
274static char O_readline__doc__[] =
275"readline() -- Read one line"
276;
277
278static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000279O_creadline(ARG(Oobject*, self), ARG(char**, output))
280 ARGDECL(Oobject*, self)
281 ARGDECL(char**, output)
Guido van Rossum049cd901996-12-05 23:30:48 +0000282{
283 char *n, *s;
284 int l;
285
286 for (n = self->buf + self->pos, s = self->buf + self->string_size;
287 n < s && *n != '\n'; n++);
288 if (n < s) n++;
289
290 *output=self->buf + self->pos;
291 l = n - self->buf - self->pos;
292 self->pos += l;
293 return l;
294}
295
296static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000297O_readline(ARG(Oobject*, self), ARG(PyObject*, args))
298 ARGDECL(Oobject*, self)
299 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000300{
301 int n;
302 char *output;
303
304 n=O_creadline(self,&output);
305 return PyString_FromStringAndSize(output, n);
306}
307
308static char O_write__doc__[] =
309"write(s) -- Write a string to the file"
310"\n\nNote (hack:) writing None resets the buffer"
311;
312
313
314static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000315O_cwrite(ARG(Oobject*, self), ARG(char*, c), ARG(int, l))
316 ARGDECL(Oobject*, self)
317 ARGDECL(char*, c)
318 ARGDECL(int, l)
Guido van Rossum049cd901996-12-05 23:30:48 +0000319{
Guido van Rossum55702f81997-01-06 22:57:52 +0000320 PyObject *s;
321 char *b;
322 int newl, space_needed;
323
Guido van Rossum049cd901996-12-05 23:30:48 +0000324 newl=self->pos+l;
325 if(newl > self->buf_size)
326 {
327 self->buf_size*=2;
328 if(self->buf_size < newl) self->buf_size=newl;
329 UNLESS(self->buf=(char*)realloc(self->buf, self->buf_size*sizeof(char)))
330 {
331 PyErr_SetString(PyExc_MemoryError,"out of memory");
332 self->buf_size=self->pos=0;
333 return -1;
334 }
335 }
336
337 memcpy(self->buf+self->pos,c,l);
338
339 self->pos += l;
340
341 if (self->string_size < self->pos)
342 {
343 self->string_size = self->pos;
344 }
345
346 return l;
347}
348
349static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000350O_write(ARG(Oobject*, self), ARG(PyObject*, args))
351 ARGDECL(Oobject*, self)
352 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000353{
354 PyObject *s;
Guido van Rossum55702f81997-01-06 22:57:52 +0000355 char *c, *b;
356 int l, newl, space_needed;
Guido van Rossum049cd901996-12-05 23:30:48 +0000357
358 UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
Guido van Rossum55702f81997-01-06 22:57:52 +0000359 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
360 UNLESS(c=PyString_AsString(s)) return NULL;
361 UNLESS(-1 != O_cwrite(self,c,l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000362
Guido van Rossum55702f81997-01-06 22:57:52 +0000363 Py_INCREF(Py_None);
364 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000365}
366
367static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000368O_getval(ARG(Oobject*, self), ARG(PyObject*, args))
369 ARGDECL(Oobject*, self)
370 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000371{
372 return PyString_FromStringAndSize(self->buf,self->pos);
373}
374
375static char O_truncate__doc__[] =
376"truncate(): truncate the file at the current position.";
377
378static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000379O_truncate(ARG(Oobject*, self), ARG(PyObject*, args))
380 ARGDECL(Oobject*, self)
381 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000382{
383 self->string_size = self->pos;
384 Py_INCREF(Py_None);
385 return Py_None;
386}
387
388static char O_isatty__doc__[] = "isatty(): always returns 0";
389
390static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000391O_isatty(ARG(Oobject*, self), ARG(PyObject*, args))
392 ARGDECL(Oobject*, self)
393 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000394{
395 return PyInt_FromLong(0);
396}
397
398static char O_close__doc__[] = "close(): explicitly release resources held.";
399
400static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000401O_close(ARG(Oobject*, self), ARG(PyObject*, args))
402 ARGDECL(Oobject*, self)
403 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000404{
405 free(self->buf);
406
407 self->pos = self->string_size = self->buf_size = 0;
408 self->closed = 1;
409
410 Py_INCREF(Py_None);
411 return Py_None;
412}
413
414static char O_flush__doc__[] = "flush(): does nothing.";
415
416static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000417O_flush(ARG(Oobject*, self), ARG(PyObject*, args))
418 ARGDECL(Oobject*, self)
419 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000420{
421 Py_INCREF(Py_None);
422 return Py_None;
423}
424
425
426static char O_writelines__doc__[] = "blah";
427static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000428O_writelines(ARG(Oobject*, self), ARG(PyObject*, args))
429 ARGDECL(Oobject*, self)
430 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000431{
432 PyObject *string_module = 0;
433 static PyObject *string_joinfields = 0;
434
435 UNLESS(PyArg_Parse(args, "O", args))
436 {
437 return NULL;
438 }
439
440 if (!string_joinfields)
441 {
442 UNLESS(string_module = PyImport_ImportModule("string"))
443 {
444 return NULL;
445 }
446
447 UNLESS(string_joinfields=
448 PyObject_GetAttrString(string_module, "joinfields"))
449 {
450 return NULL;
451 }
452
453 Py_DECREF(string_module);
454 }
455
456 if (PyObject_Length(args) == -1)
457 {
458 return NULL;
459 }
460
461 return O_write(self,
462 PyObject_CallFunction(string_joinfields, "Os", args, ""));
463}
464
465static struct PyMethodDef O_methods[] = {
466 {"read", (PyCFunction)O_read, 1, O_read__doc__},
467 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
468 {"write", (PyCFunction)O_write, 0, O_write__doc__},
469 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
470 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
471 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
472 {"getvalue", (PyCFunction)O_getval, 0, "getvalue() -- Get the string value"},
473 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
474 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
475 {"close", (PyCFunction)O_close, 0, O_close__doc__},
476 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
477 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__},
478 {NULL, NULL} /* sentinel */
479};
480
481/* ---------- */
482
483
484static Oobject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000485newOobject(ARG(int, size))
486 ARGDECL(int, size)
Guido van Rossum049cd901996-12-05 23:30:48 +0000487{
488 Oobject *self;
489
490 self = PyObject_NEW(Oobject, &Otype);
491 if (self == NULL)
492 return NULL;
493 self->pos=0;
494 self->closed = 0;
495 self->string_size = 0;
496
497 UNLESS(self->buf=malloc(size*sizeof(char)))
498 {
499 PyErr_SetString(PyExc_MemoryError,"out of memory");
500 self->buf_size = 0;
501 return NULL;
502 }
503
504 self->buf_size=size;
505 return self;
506}
507
508
509static void
Guido van Rossum55702f81997-01-06 22:57:52 +0000510O_dealloc(ARG(Oobject*, self))
511 ARGDECL(Oobject*, self)
Guido van Rossum049cd901996-12-05 23:30:48 +0000512{
513 free(self->buf);
514 PyMem_DEL(self);
515}
516
517static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000518O_getattr(ARG(Oobject*, self), ARG(char*, name))
519 ARGDECL(Oobject*, self)
520 ARGDECL(char*, name)
Guido van Rossum049cd901996-12-05 23:30:48 +0000521{
522 return Py_FindMethod(O_methods, (PyObject *)self, name);
523}
524
525static char Otype__doc__[] =
526"Simple type for output to strings."
527;
528
529static PyTypeObject Otype = {
530 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum55702f81997-01-06 22:57:52 +0000531 0, /*ob_size*/
532 "StringO", /*tp_name*/
533 sizeof(Oobject), /*tp_basicsize*/
534 0, /*tp_itemsize*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000535 /* methods */
536 (destructor)O_dealloc, /*tp_dealloc*/
537 (printfunc)0, /*tp_print*/
538 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000539 (setattrfunc)0, /*tp_setattr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000540 (cmpfunc)0, /*tp_compare*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000541 (reprfunc)0, /*tp_repr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000542 0, /*tp_as_number*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000543 0, /*tp_as_sequence*/
544 0, /*tp_as_mapping*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000545 (hashfunc)0, /*tp_hash*/
546 (ternaryfunc)0, /*tp_call*/
547 (reprfunc)0, /*tp_str*/
548
549 /* Space for future expansion */
550 0L,0L,0L,0L,
Guido van Rossum55702f81997-01-06 22:57:52 +0000551 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000552};
553
554/* End of code for StringO objects */
555/* -------------------------------------------------------- */
556
557static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000558I_close(ARG(Iobject*, self), ARG(PyObject*, args))
559 ARGDECL(Iobject*, self)
560 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000561{
562 Py_DECREF(self->pbuf);
563
564 self->pos = self->string_size = 0;
565 self->closed = 1;
566
567 Py_INCREF(Py_None);
568 return Py_None;
569}
570
571static struct PyMethodDef I_methods[] = {
572 {"read", (PyCFunction)O_read, 1, O_read__doc__},
573 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
574 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
575 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
576 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
577 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
578 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
579 {"close", (PyCFunction)I_close, 0, O_close__doc__},
580 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
581 {NULL, NULL} /* sentinel */
582};
583
584/* ---------- */
585
586
587static Iobject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000588newIobject(ARG(PyObject*, s))
589 ARGDECL(PyObject*, s)
Guido van Rossum049cd901996-12-05 23:30:48 +0000590{
591 Iobject *self;
592 char *buf;
593 int size;
594
595 UNLESS(buf=PyString_AsString(s)) return NULL;
596 UNLESS(-1 != (size=PyString_Size(s))) return NULL;
597 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
598 Py_INCREF(s);
599 self->buf=buf;
600 self->string_size=size;
601 self->pbuf=s;
602 self->pos=0;
603 self->closed = 0;
604
605 return self;
606}
607
608
609static void
Guido van Rossum55702f81997-01-06 22:57:52 +0000610I_dealloc(ARG(Iobject*, self))
611 ARGDECL(Iobject*, self)
Guido van Rossum049cd901996-12-05 23:30:48 +0000612{
613 Py_DECREF(self->pbuf);
614 PyMem_DEL(self);
615}
616
617static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000618I_getattr(ARG(Iobject*, self), ARG(char*, name))
619 ARGDECL(Iobject*, self)
620 ARGDECL(char*, name)
Guido van Rossum049cd901996-12-05 23:30:48 +0000621{
622 return Py_FindMethod(I_methods, (PyObject *)self, name);
623}
624
625static char Itype__doc__[] =
626"Simple type for treating strings as input file streams"
627;
628
629static PyTypeObject Itype = {
630 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum55702f81997-01-06 22:57:52 +0000631 0, /*ob_size*/
632 "StringI", /*tp_name*/
633 sizeof(Iobject), /*tp_basicsize*/
634 0, /*tp_itemsize*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000635 /* methods */
636 (destructor)I_dealloc, /*tp_dealloc*/
637 (printfunc)0, /*tp_print*/
638 (getattrfunc)I_getattr, /*tp_getattr*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000639 (setattrfunc)0, /*tp_setattr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000640 (cmpfunc)0, /*tp_compare*/
641 (reprfunc)0, /*tp_repr*/
642 0, /*tp_as_number*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000643 0, /*tp_as_sequence*/
644 0, /*tp_as_mapping*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000645 (hashfunc)0, /*tp_hash*/
646 (ternaryfunc)0, /*tp_call*/
647 (reprfunc)0, /*tp_str*/
648
649 /* Space for future expansion */
650 0L,0L,0L,0L,
Guido van Rossum55702f81997-01-06 22:57:52 +0000651 Itype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000652};
653
654/* End of code for StringI objects */
655/* -------------------------------------------------------- */
656
657
658static char IO_StringIO__doc__[] =
659"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
660;
661
662static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000663IO_StringIO(ARG(PyObject*, self), ARG(PyObject*, args))
664 ARGDECL(PyObject*, self)
665 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000666{
667 PyObject *s=0;
668
669 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL;
670 if(s) return (PyObject *)newIobject(s);
671 return (PyObject *)newOobject(128);
672}
673
674/* List of methods defined in the module */
675
676static struct PyMethodDef IO_methods[] = {
Guido van Rossum55702f81997-01-06 22:57:52 +0000677 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
678 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000679};
680
681
682/* Initialization function for the module (*must* be called initcStringIO) */
683
684void
685initcStringIO()
686{
687 PyObject *m, *d;
688
689 /* Create the module and add the functions */
690 m = Py_InitModule4("cStringIO", IO_methods,
691 cStringIO_module_documentation,
692 (PyObject*)NULL,PYTHON_API_VERSION);
693
694 /* Add some symbolic constants to the module */
695 d = PyModule_GetDict(m);
696 ErrorObject = PyString_FromString("cStringIO.error");
697 PyDict_SetItemString(d, "error", ErrorObject);
698
Guido van Rossum55702f81997-01-06 22:57:52 +0000699#ifdef Py_COBJECT_H
700 /* Export C API */
Guido van Rossum049cd901996-12-05 23:30:48 +0000701 PyDict_SetItemString(d,"cread", PyCObject_FromVoidPtr(O_cread,NULL));
702 PyDict_SetItemString(d,"creadline", PyCObject_FromVoidPtr(O_creadline,NULL));
703 PyDict_SetItemString(d,"cwrite", PyCObject_FromVoidPtr(O_cwrite,NULL));
704 PyDict_SetItemString(d,"cgetvalue", PyCObject_FromVoidPtr(O_getval,NULL));
705 PyDict_SetItemString(d,"NewInput", PyCObject_FromVoidPtr(newIobject,NULL));
706 PyDict_SetItemString(d,"NewOutput", PyCObject_FromVoidPtr(newOobject,NULL));
Guido van Rossum55702f81997-01-06 22:57:52 +0000707#endif
Guido van Rossum049cd901996-12-05 23:30:48 +0000708
709 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
710 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
711
712
713 /* Check for errors */
714 if (PyErr_Occurred())
715 Py_FatalError("can't initialize module cStringIO");
716}
Guido van Rossum55702f81997-01-06 22:57:52 +0000717