blob: 72c7a4cb23aa72fbe496a747804e8207b70a5408 [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$
Barry Warsaw61a63e11997-01-14 17:38:28 +000061 Revision 2.3 1997/01/14 17:38:28 bwarsaw
62 Quieted gcc -Wall by removing unused local variables.
63
64 Suppressing my urge to reformat according to Python coding standards!
65 :-)
66
Guido van Rossum55702f81997-01-06 22:57:52 +000067 Revision 2.2 1997/01/06 22:57:52 guido
68 Jim's latest version.
69
70 Revision 1.10 1997/01/02 15:19:55 chris
71 checked in to be sure repository is up to date.
72
73 Revision 1.9 1996/12/27 21:40:29 jim
74 Took out some lamosities in interface, like returning self from
75 write.
76
77 Revision 1.8 1996/12/23 15:52:49 jim
78 Added ifdef to check for CObject before using it.
79
80 Revision 1.7 1996/12/23 15:22:35 jim
81 Finished implementation, adding full compatibility with StringIO, and
82 then some.
83
84 We still need to take out some cStringIO oddities.
Guido van Rossum049cd901996-12-05 23:30:48 +000085
86 Revision 1.6 1996/10/15 18:42:07 jim
87 Added lots of casts to make warnings go away.
88
89 Revision 1.5 1996/10/11 21:03:42 jim
90 *** empty log message ***
91
92 Revision 1.4 1996/10/11 21:02:15 jim
93 *** empty log message ***
94
95 Revision 1.3 1996/10/07 20:51:38 chris
96 *** empty log message ***
97
98 Revision 1.2 1996/07/18 13:08:34 jfulton
99 *** empty log message ***
100
101 Revision 1.1 1996/07/15 17:06:33 jfulton
102 Initial version.
103
104
105*/
106static char cStringIO_module_documentation[] =
107"A simple fast partial StringIO replacement.\n"
108"\n"
109"This module provides a simple useful replacement for\n"
110"the StringIO module that is written in C. It does not provide the\n"
111"full generality if StringIO, but it provides anough for most\n"
112"applications and is especially useful in conjuction with the\n"
113"pickle module.\n"
114"\n"
115"Usage:\n"
116"\n"
117" from cStringIO import StringIO\n"
118"\n"
119" an_output_stream=StringIO()\n"
120" an_output_stream.write(some_stuff)\n"
121" ...\n"
122" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
123"\n"
124" an_input_stream=StringIO(a_string)\n"
125" spam=an_input_stream.readline()\n"
126" spam=an_input_stream.read(5)\n"
127" an_input_stream.reset() # OK, start over, note no seek yet\n"
128" spam=an_input_stream.read() # and read it all\n"
129" \n"
130"If someone else wants to provide a more complete implementation,\n"
131"go for it. :-) \n"
132;
133
134#include "Python.h"
135#include "import.h"
136
137static PyObject *ErrorObject;
138
Guido van Rossum55702f81997-01-06 22:57:52 +0000139#ifdef __cplusplus
140#define ARG(T,N) T N
141#define ARGDECL(T,N)
142#else
143#define ARG(T,N) N
144#define ARGDECL(T,N) T N;
145#endif
146
Guido van Rossum049cd901996-12-05 23:30:48 +0000147#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
148#define UNLESS(E) if(!(E))
149#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
150#define Py_ASSIGN(P,E) if(!PyObject_AssignExpression(&(P),(E))) return NULL
151
152/* ----------------------------------------------------- */
153
154/* Declarations for objects of type StringO */
155
156typedef struct {
157 PyObject_HEAD
158 char *buf;
159 int pos, string_size, buf_size, closed;
160} Oobject;
161
162staticforward PyTypeObject Otype;
163
164/* ---------------------------------------------------------------- */
165
166/* Declarations for objects of type StringI */
167
168typedef struct {
169 PyObject_HEAD
170 char *buf;
171 int pos, string_size, closed;
172 PyObject *pbuf;
173} Iobject;
174
175staticforward PyTypeObject Itype;
176
177
178
179/* ---------------------------------------------------------------- */
180
181static char O_reset__doc__[] =
182"reset() -- Reset the file position to the beginning"
183;
184
185static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000186O_reset(ARG(Oobject*, self), ARG(PyObject*, args))
187 ARGDECL(Oobject*, self)
188 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000189{
190 self->pos = 0;
191
192 Py_INCREF(Py_None);
193 return Py_None;
194}
195
196
197static char O_tell__doc__[] =
198"tell() -- get the current position.";
199
200static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000201O_tell(ARG(Oobject*, self), ARG(PyObject*, args))
202 ARGDECL(Oobject*, self)
203 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000204{
205 return PyInt_FromLong(self->pos);
206}
207
208
209static char O_seek__doc__[] =
210"seek(position) -- set the current position\n"
211"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
212
213static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000214O_seek(ARG(Oobject*, self), ARG(PyObject*, args))
215 ARGDECL(Oobject*, self)
216 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000217{
218 int position, mode = 0;
219
220 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode))
221 {
222 return NULL;
223 }
224
225 if (mode == 2)
226 {
227 position += self->string_size;
228 }
229 else if (mode == 1)
230 {
231 position += self->pos;
232 }
233
234 self->pos = (position > self->string_size ? self->string_size :
235 (position < 0 ? 0 : position));
236
Guido van Rossum55702f81997-01-06 22:57:52 +0000237 Py_INCREF(Py_None);
238 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000239}
240
241static char O_read__doc__[] =
242"read([s]) -- Read s characters, or the rest of the string"
243;
244
245static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000246O_cread(ARG(Oobject*, self), ARG(char**, output), ARG(int, n))
247 ARGDECL(Oobject*, self)
248 ARGDECL(char**, output)
249 ARGDECL(int, n)
Guido van Rossum049cd901996-12-05 23:30:48 +0000250{
251 int l;
252
253 l = self->string_size - self->pos;
254 if (n < 0 || n > l)
255 {
256 n = l;
257 }
258
259 *output=self->buf + self->pos;
260 self->pos += n;
261 return n;
262}
263
264static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000265O_read(ARG(Oobject*, self), ARG(PyObject*, args))
266 ARGDECL(Oobject*, self)
267 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000268{
269 int n = -1;
270 char *output;
271
272 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
273
274 n=O_cread(self,&output,n);
275
276 return PyString_FromStringAndSize(output, n);
277}
278
279
280static char O_readline__doc__[] =
281"readline() -- Read one line"
282;
283
284static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000285O_creadline(ARG(Oobject*, self), ARG(char**, output))
286 ARGDECL(Oobject*, self)
287 ARGDECL(char**, output)
Guido van Rossum049cd901996-12-05 23:30:48 +0000288{
289 char *n, *s;
290 int l;
291
292 for (n = self->buf + self->pos, s = self->buf + self->string_size;
293 n < s && *n != '\n'; n++);
294 if (n < s) n++;
295
296 *output=self->buf + self->pos;
297 l = n - self->buf - self->pos;
298 self->pos += l;
299 return l;
300}
301
302static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000303O_readline(ARG(Oobject*, self), ARG(PyObject*, args))
304 ARGDECL(Oobject*, self)
305 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000306{
307 int n;
308 char *output;
309
310 n=O_creadline(self,&output);
311 return PyString_FromStringAndSize(output, n);
312}
313
314static char O_write__doc__[] =
315"write(s) -- Write a string to the file"
316"\n\nNote (hack:) writing None resets the buffer"
317;
318
319
320static int
Guido van Rossum55702f81997-01-06 22:57:52 +0000321O_cwrite(ARG(Oobject*, self), ARG(char*, c), ARG(int, l))
322 ARGDECL(Oobject*, self)
323 ARGDECL(char*, c)
324 ARGDECL(int, l)
Guido van Rossum049cd901996-12-05 23:30:48 +0000325{
Barry Warsaw61a63e11997-01-14 17:38:28 +0000326 int newl;
Guido van Rossum55702f81997-01-06 22:57:52 +0000327
Guido van Rossum049cd901996-12-05 23:30:48 +0000328 newl=self->pos+l;
329 if(newl > self->buf_size)
330 {
331 self->buf_size*=2;
332 if(self->buf_size < newl) self->buf_size=newl;
333 UNLESS(self->buf=(char*)realloc(self->buf, self->buf_size*sizeof(char)))
334 {
335 PyErr_SetString(PyExc_MemoryError,"out of memory");
336 self->buf_size=self->pos=0;
337 return -1;
338 }
339 }
340
341 memcpy(self->buf+self->pos,c,l);
342
343 self->pos += l;
344
345 if (self->string_size < self->pos)
346 {
347 self->string_size = self->pos;
348 }
349
350 return l;
351}
352
353static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000354O_write(ARG(Oobject*, self), ARG(PyObject*, args))
355 ARGDECL(Oobject*, self)
356 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000357{
358 PyObject *s;
Barry Warsaw61a63e11997-01-14 17:38:28 +0000359 char *c;
360 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000361
362 UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
Guido van Rossum55702f81997-01-06 22:57:52 +0000363 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
364 UNLESS(c=PyString_AsString(s)) return NULL;
365 UNLESS(-1 != O_cwrite(self,c,l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000366
Guido van Rossum55702f81997-01-06 22:57:52 +0000367 Py_INCREF(Py_None);
368 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000369}
370
371static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000372O_getval(ARG(Oobject*, self), ARG(PyObject*, args))
373 ARGDECL(Oobject*, self)
374 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000375{
376 return PyString_FromStringAndSize(self->buf,self->pos);
377}
378
379static char O_truncate__doc__[] =
380"truncate(): truncate the file at the current position.";
381
382static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000383O_truncate(ARG(Oobject*, self), ARG(PyObject*, args))
384 ARGDECL(Oobject*, self)
385 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000386{
387 self->string_size = self->pos;
388 Py_INCREF(Py_None);
389 return Py_None;
390}
391
392static char O_isatty__doc__[] = "isatty(): always returns 0";
393
394static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000395O_isatty(ARG(Oobject*, self), ARG(PyObject*, args))
396 ARGDECL(Oobject*, self)
397 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000398{
399 return PyInt_FromLong(0);
400}
401
402static char O_close__doc__[] = "close(): explicitly release resources held.";
403
404static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000405O_close(ARG(Oobject*, self), ARG(PyObject*, args))
406 ARGDECL(Oobject*, self)
407 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000408{
409 free(self->buf);
410
411 self->pos = self->string_size = self->buf_size = 0;
412 self->closed = 1;
413
414 Py_INCREF(Py_None);
415 return Py_None;
416}
417
418static char O_flush__doc__[] = "flush(): does nothing.";
419
420static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000421O_flush(ARG(Oobject*, self), ARG(PyObject*, args))
422 ARGDECL(Oobject*, self)
423 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000424{
425 Py_INCREF(Py_None);
426 return Py_None;
427}
428
429
430static char O_writelines__doc__[] = "blah";
431static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000432O_writelines(ARG(Oobject*, self), ARG(PyObject*, args))
433 ARGDECL(Oobject*, self)
434 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000435{
436 PyObject *string_module = 0;
437 static PyObject *string_joinfields = 0;
438
439 UNLESS(PyArg_Parse(args, "O", args))
440 {
441 return NULL;
442 }
443
444 if (!string_joinfields)
445 {
446 UNLESS(string_module = PyImport_ImportModule("string"))
447 {
448 return NULL;
449 }
450
451 UNLESS(string_joinfields=
452 PyObject_GetAttrString(string_module, "joinfields"))
453 {
454 return NULL;
455 }
456
457 Py_DECREF(string_module);
458 }
459
460 if (PyObject_Length(args) == -1)
461 {
462 return NULL;
463 }
464
465 return O_write(self,
466 PyObject_CallFunction(string_joinfields, "Os", args, ""));
467}
468
469static struct PyMethodDef O_methods[] = {
470 {"read", (PyCFunction)O_read, 1, O_read__doc__},
471 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
472 {"write", (PyCFunction)O_write, 0, O_write__doc__},
473 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
474 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
475 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
476 {"getvalue", (PyCFunction)O_getval, 0, "getvalue() -- Get the string value"},
477 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
478 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
479 {"close", (PyCFunction)O_close, 0, O_close__doc__},
480 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
481 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__},
482 {NULL, NULL} /* sentinel */
483};
484
485/* ---------- */
486
487
488static Oobject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000489newOobject(ARG(int, size))
490 ARGDECL(int, size)
Guido van Rossum049cd901996-12-05 23:30:48 +0000491{
492 Oobject *self;
493
494 self = PyObject_NEW(Oobject, &Otype);
495 if (self == NULL)
496 return NULL;
497 self->pos=0;
498 self->closed = 0;
499 self->string_size = 0;
500
501 UNLESS(self->buf=malloc(size*sizeof(char)))
502 {
503 PyErr_SetString(PyExc_MemoryError,"out of memory");
504 self->buf_size = 0;
505 return NULL;
506 }
507
508 self->buf_size=size;
509 return self;
510}
511
512
513static void
Guido van Rossum55702f81997-01-06 22:57:52 +0000514O_dealloc(ARG(Oobject*, self))
515 ARGDECL(Oobject*, self)
Guido van Rossum049cd901996-12-05 23:30:48 +0000516{
517 free(self->buf);
518 PyMem_DEL(self);
519}
520
521static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000522O_getattr(ARG(Oobject*, self), ARG(char*, name))
523 ARGDECL(Oobject*, self)
524 ARGDECL(char*, name)
Guido van Rossum049cd901996-12-05 23:30:48 +0000525{
526 return Py_FindMethod(O_methods, (PyObject *)self, name);
527}
528
529static char Otype__doc__[] =
530"Simple type for output to strings."
531;
532
533static PyTypeObject Otype = {
534 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum55702f81997-01-06 22:57:52 +0000535 0, /*ob_size*/
536 "StringO", /*tp_name*/
537 sizeof(Oobject), /*tp_basicsize*/
538 0, /*tp_itemsize*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000539 /* methods */
540 (destructor)O_dealloc, /*tp_dealloc*/
541 (printfunc)0, /*tp_print*/
542 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000543 (setattrfunc)0, /*tp_setattr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000544 (cmpfunc)0, /*tp_compare*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000545 (reprfunc)0, /*tp_repr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000546 0, /*tp_as_number*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000547 0, /*tp_as_sequence*/
548 0, /*tp_as_mapping*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000549 (hashfunc)0, /*tp_hash*/
550 (ternaryfunc)0, /*tp_call*/
551 (reprfunc)0, /*tp_str*/
552
553 /* Space for future expansion */
554 0L,0L,0L,0L,
Guido van Rossum55702f81997-01-06 22:57:52 +0000555 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000556};
557
558/* End of code for StringO objects */
559/* -------------------------------------------------------- */
560
561static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000562I_close(ARG(Iobject*, self), ARG(PyObject*, args))
563 ARGDECL(Iobject*, self)
564 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000565{
566 Py_DECREF(self->pbuf);
567
568 self->pos = self->string_size = 0;
569 self->closed = 1;
570
571 Py_INCREF(Py_None);
572 return Py_None;
573}
574
575static struct PyMethodDef I_methods[] = {
576 {"read", (PyCFunction)O_read, 1, O_read__doc__},
577 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
578 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
579 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
580 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
581 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
582 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
583 {"close", (PyCFunction)I_close, 0, O_close__doc__},
584 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
585 {NULL, NULL} /* sentinel */
586};
587
588/* ---------- */
589
590
591static Iobject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000592newIobject(ARG(PyObject*, s))
593 ARGDECL(PyObject*, s)
Guido van Rossum049cd901996-12-05 23:30:48 +0000594{
595 Iobject *self;
596 char *buf;
597 int size;
598
599 UNLESS(buf=PyString_AsString(s)) return NULL;
600 UNLESS(-1 != (size=PyString_Size(s))) return NULL;
601 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
602 Py_INCREF(s);
603 self->buf=buf;
604 self->string_size=size;
605 self->pbuf=s;
606 self->pos=0;
607 self->closed = 0;
608
609 return self;
610}
611
612
613static void
Guido van Rossum55702f81997-01-06 22:57:52 +0000614I_dealloc(ARG(Iobject*, self))
615 ARGDECL(Iobject*, self)
Guido van Rossum049cd901996-12-05 23:30:48 +0000616{
617 Py_DECREF(self->pbuf);
618 PyMem_DEL(self);
619}
620
621static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000622I_getattr(ARG(Iobject*, self), ARG(char*, name))
623 ARGDECL(Iobject*, self)
624 ARGDECL(char*, name)
Guido van Rossum049cd901996-12-05 23:30:48 +0000625{
626 return Py_FindMethod(I_methods, (PyObject *)self, name);
627}
628
629static char Itype__doc__[] =
630"Simple type for treating strings as input file streams"
631;
632
633static PyTypeObject Itype = {
634 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum55702f81997-01-06 22:57:52 +0000635 0, /*ob_size*/
636 "StringI", /*tp_name*/
637 sizeof(Iobject), /*tp_basicsize*/
638 0, /*tp_itemsize*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000639 /* methods */
640 (destructor)I_dealloc, /*tp_dealloc*/
641 (printfunc)0, /*tp_print*/
642 (getattrfunc)I_getattr, /*tp_getattr*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000643 (setattrfunc)0, /*tp_setattr*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000644 (cmpfunc)0, /*tp_compare*/
645 (reprfunc)0, /*tp_repr*/
646 0, /*tp_as_number*/
Guido van Rossum55702f81997-01-06 22:57:52 +0000647 0, /*tp_as_sequence*/
648 0, /*tp_as_mapping*/
Guido van Rossum049cd901996-12-05 23:30:48 +0000649 (hashfunc)0, /*tp_hash*/
650 (ternaryfunc)0, /*tp_call*/
651 (reprfunc)0, /*tp_str*/
652
653 /* Space for future expansion */
654 0L,0L,0L,0L,
Guido van Rossum55702f81997-01-06 22:57:52 +0000655 Itype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000656};
657
658/* End of code for StringI objects */
659/* -------------------------------------------------------- */
660
661
662static char IO_StringIO__doc__[] =
663"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
664;
665
666static PyObject *
Guido van Rossum55702f81997-01-06 22:57:52 +0000667IO_StringIO(ARG(PyObject*, self), ARG(PyObject*, args))
668 ARGDECL(PyObject*, self)
669 ARGDECL(PyObject*, args)
Guido van Rossum049cd901996-12-05 23:30:48 +0000670{
671 PyObject *s=0;
672
673 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL;
674 if(s) return (PyObject *)newIobject(s);
675 return (PyObject *)newOobject(128);
676}
677
678/* List of methods defined in the module */
679
680static struct PyMethodDef IO_methods[] = {
Guido van Rossum55702f81997-01-06 22:57:52 +0000681 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
682 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000683};
684
685
686/* Initialization function for the module (*must* be called initcStringIO) */
687
688void
689initcStringIO()
690{
691 PyObject *m, *d;
692
693 /* Create the module and add the functions */
694 m = Py_InitModule4("cStringIO", IO_methods,
695 cStringIO_module_documentation,
696 (PyObject*)NULL,PYTHON_API_VERSION);
697
698 /* Add some symbolic constants to the module */
699 d = PyModule_GetDict(m);
700 ErrorObject = PyString_FromString("cStringIO.error");
701 PyDict_SetItemString(d, "error", ErrorObject);
702
Guido van Rossum55702f81997-01-06 22:57:52 +0000703#ifdef Py_COBJECT_H
704 /* Export C API */
Guido van Rossum049cd901996-12-05 23:30:48 +0000705 PyDict_SetItemString(d,"cread", PyCObject_FromVoidPtr(O_cread,NULL));
706 PyDict_SetItemString(d,"creadline", PyCObject_FromVoidPtr(O_creadline,NULL));
707 PyDict_SetItemString(d,"cwrite", PyCObject_FromVoidPtr(O_cwrite,NULL));
708 PyDict_SetItemString(d,"cgetvalue", PyCObject_FromVoidPtr(O_getval,NULL));
709 PyDict_SetItemString(d,"NewInput", PyCObject_FromVoidPtr(newIobject,NULL));
710 PyDict_SetItemString(d,"NewOutput", PyCObject_FromVoidPtr(newOobject,NULL));
Guido van Rossum55702f81997-01-06 22:57:52 +0000711#endif
Guido van Rossum049cd901996-12-05 23:30:48 +0000712
713 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
714 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
715
716
717 /* Check for errors */
718 if (PyErr_Occurred())
719 Py_FatalError("can't initialize module cStringIO");
720}
Guido van Rossum55702f81997-01-06 22:57:52 +0000721