6 PyDoc_STRVAR(cStringIO_module_documentation
,
7 "A simple fast partial StringIO replacement.\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"
11 "full generality of StringIO, but it provides enough for most\n"
12 "applications and is especially useful in conjunction with the\n"
17 " from cStringIO import StringIO\n"
19 " an_output_stream=StringIO()\n"
20 " an_output_stream.write(some_stuff)\n"
22 " value=an_output_stream.getvalue()\n"
24 " an_input_stream=StringIO(a_string)\n"
25 " spam=an_input_stream.readline()\n"
26 " spam=an_input_stream.read(5)\n"
27 " an_input_stream.seek(0) # OK, start over\n"
28 " spam=an_input_stream.read() # and read it all\n"
30 "If someone else wants to provide a more complete implementation,\n"
33 "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
35 #define UNLESS(E) if (!(E))
38 /* Declaration for file-like objects that manage data as strings
40 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.
52 #define IOOOBJECT(O) ((IOobject*)(O))
54 /* Declarations for objects of type StringO */
56 typedef struct { /* Subtype of IOobject */
61 int buf_size
, softspace
;
64 /* Declarations for objects of type StringI */
66 typedef struct { /* Subtype of IOobject */
70 /* We store a reference to the object here in order to keep
71 the buffer alive during the lifetime of the Iobject. */
75 /* IOobject (common) methods */
77 PyDoc_STRVAR(IO_flush__doc__
, "flush(): does nothing.");
80 IO__opencheck(IOobject
*self
) {
82 PyErr_SetString(PyExc_ValueError
,
83 "I/O operation on closed file");
90 IO_flush(IOobject
*self
, PyObject
*args
) {
92 UNLESS (IO__opencheck(self
)) return NULL
;
93 UNLESS (PyArg_ParseTuple(args
, ":flush")) return NULL
;
99 PyDoc_STRVAR(IO_getval__doc__
,
100 "getvalue([use_pos]) -- Get the string value."
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");
106 IO_cgetval(PyObject
*self
) {
107 UNLESS (IO__opencheck(IOOOBJECT(self
))) return NULL
;
108 return PyString_FromStringAndSize(((IOobject
*)self
)->buf
,
109 ((IOobject
*)self
)->pos
);
113 IO_getval(IOobject
*self
, PyObject
*args
) {
114 PyObject
*use_pos
=Py_None
;
117 UNLESS (IO__opencheck(self
)) return NULL
;
118 UNLESS (PyArg_ParseTuple(args
,"|O:getval",&use_pos
)) return NULL
;
120 if (PyObject_IsTrue(use_pos
)) {
122 if (s
> self
->string_size
) s
=self
->string_size
;
126 return PyString_FromStringAndSize(self
->buf
, s
);
129 PyDoc_STRVAR(IO_isatty__doc__
, "isatty(): always returns 0");
132 IO_isatty(IOobject
*self
, PyObject
*args
) {
134 UNLESS (PyArg_ParseTuple(args
, ":isatty")) return NULL
;
136 return PyInt_FromLong(0);
139 PyDoc_STRVAR(IO_read__doc__
,
140 "read([s]) -- Read s characters, or the rest of the string");
143 IO_cread(PyObject
*self
, char **output
, int n
) {
146 UNLESS (IO__opencheck(IOOOBJECT(self
))) return -1;
147 l
= ((IOobject
*)self
)->string_size
- ((IOobject
*)self
)->pos
;
148 if (n
< 0 || n
> l
) {
153 *output
=((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->pos
;
154 ((IOobject
*)self
)->pos
+= n
;
159 IO_read(IOobject
*self
, PyObject
*args
) {
163 UNLESS (PyArg_ParseTuple(args
, "|i:read", &n
)) return NULL
;
165 if ( (n
=IO_cread((PyObject
*)self
,&output
,n
)) < 0) return NULL
;
167 return PyString_FromStringAndSize(output
, n
);
170 PyDoc_STRVAR(IO_readline__doc__
, "readline() -- Read one line");
173 IO_creadline(PyObject
*self
, char **output
) {
177 UNLESS (IO__opencheck(IOOOBJECT(self
))) return -1;
179 for (n
= ((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->pos
,
180 s
= ((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->string_size
;
181 n
< s
&& *n
!= '\n'; n
++);
184 *output
=((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->pos
;
185 l
= n
- ((IOobject
*)self
)->buf
- ((IOobject
*)self
)->pos
;
186 ((IOobject
*)self
)->pos
+= l
;
191 IO_readline(IOobject
*self
, PyObject
*args
) {
195 UNLESS (PyArg_ParseTuple(args
, "|i:readline", &m
)) return NULL
;
197 if( (n
=IO_creadline((PyObject
*)self
,&output
)) < 0) return NULL
;
198 if (m
>= 0 && m
< n
) {
203 return PyString_FromStringAndSize(output
, n
);
206 PyDoc_STRVAR(IO_readlines__doc__
, "readlines() -- Read all lines");
209 IO_readlines(IOobject
*self
, PyObject
*args
) {
212 PyObject
*result
, *line
;
213 int hint
= 0, length
= 0;
215 UNLESS (PyArg_ParseTuple(args
, "|i:readlines", &hint
)) return NULL
;
217 result
= PyList_New(0);
222 if ( (n
= IO_creadline((PyObject
*)self
,&output
)) < 0)
226 line
= PyString_FromStringAndSize (output
, n
);
229 PyList_Append (result
, line
);
232 if (hint
> 0 && length
>= hint
)
241 PyDoc_STRVAR(IO_reset__doc__
,
242 "reset() -- Reset the file position to the beginning");
245 IO_reset(IOobject
*self
, PyObject
*args
) {
247 UNLESS (IO__opencheck(self
)) return NULL
;
248 UNLESS (PyArg_ParseTuple(args
, ":reset")) return NULL
;
256 PyDoc_STRVAR(IO_tell__doc__
, "tell() -- get the current position.");
259 IO_tell(IOobject
*self
, PyObject
*args
) {
261 UNLESS (IO__opencheck(self
)) return NULL
;
262 UNLESS (PyArg_ParseTuple(args
, ":tell")) return NULL
;
264 return PyInt_FromLong(self
->pos
);
267 PyDoc_STRVAR(IO_truncate__doc__
,
268 "truncate(): truncate the file at the current position.");
271 IO_truncate(IOobject
*self
, PyObject
*args
) {
274 UNLESS (IO__opencheck(self
)) return NULL
;
275 UNLESS (PyArg_ParseTuple(args
, "|i:truncate", &pos
)) return NULL
;
276 if (pos
< 0) pos
= self
->pos
;
278 if (self
->string_size
> pos
) self
->string_size
= pos
;
287 /* Read-write object methods */
289 PyDoc_STRVAR(O_seek__doc__
,
290 "seek(position) -- set the current position\n"
291 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
294 O_seek(Oobject
*self
, PyObject
*args
) {
295 int position
, mode
= 0;
297 UNLESS (IO__opencheck(IOOOBJECT(self
))) return NULL
;
298 UNLESS (PyArg_ParseTuple(args
, "i|i:seek", &position
, &mode
))
302 position
+= self
->string_size
;
304 else if (mode
== 1) {
305 position
+= self
->pos
;
308 if (position
> self
->buf_size
) {
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();
317 else if (position
< 0) position
=0;
321 while (--position
>= self
->string_size
) self
->buf
[position
]=0;
327 PyDoc_STRVAR(O_write__doc__
,
328 "write(s) -- Write a string to the file"
329 "\n\nNote (hack:) writing None resets the buffer");
333 O_cwrite(PyObject
*self
, char *c
, int l
) {
337 UNLESS (IO__opencheck(IOOOBJECT(self
))) return -1;
338 oself
= (Oobject
*)self
;
341 if (newl
>= oself
->buf_size
) {
342 oself
->buf_size
*= 2;
343 if (oself
->buf_size
<= newl
)
344 oself
->buf_size
= newl
+1;
346 (char*)realloc(oself
->buf
,
347 (oself
->buf_size
) * sizeof(char))) {
348 PyErr_SetString(PyExc_MemoryError
,"out of memory");
349 oself
->buf_size
= oself
->pos
= 0;
354 memcpy(oself
->buf
+oself
->pos
,c
,l
);
358 if (oself
->string_size
< oself
->pos
) {
359 oself
->string_size
= oself
->pos
;
366 O_write(Oobject
*self
, PyObject
*args
) {
370 UNLESS (PyArg_ParseTuple(args
, "t#:write", &c
, &l
)) return NULL
;
372 if (O_cwrite((PyObject
*)self
,c
,l
) < 0) return NULL
;
378 PyDoc_STRVAR(O_close__doc__
, "close(): explicitly release resources held.");
381 O_close(Oobject
*self
, PyObject
*args
) {
383 UNLESS (PyArg_ParseTuple(args
, ":close")) return NULL
;
385 if (self
->buf
!= NULL
) free(self
->buf
);
388 self
->pos
= self
->string_size
= self
->buf_size
= 0;
395 PyDoc_STRVAR(O_writelines__doc__
,
396 "writelines(sequence_of_strings): write each string");
398 O_writelines(Oobject
*self
, PyObject
*args
) {
400 static PyObject
*joiner
= NULL
;
402 UNLESS (PyArg_ParseTuple(args
, "O:writelines", &args
)) return NULL
;
405 PyObject
*empty_string
= PyString_FromString("");
406 if (empty_string
== NULL
)
408 joiner
= PyObject_GetAttrString(empty_string
, "join");
409 Py_DECREF(empty_string
);
414 if (PyObject_Size(args
) < 0) return NULL
;
416 tmp
= PyObject_CallFunction(joiner
, "O", args
);
417 UNLESS (tmp
) return NULL
;
419 args
= Py_BuildValue("(O)", tmp
);
421 UNLESS (args
) return NULL
;
423 tmp
= O_write(self
, args
);
428 static struct PyMethodDef O_methods
[] = {
429 /* 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__
},
440 /* Read-write StringIO specific methods: */
441 {"close", (PyCFunction
)O_close
, METH_VARARGS
, O_close__doc__
},
442 {"seek", (PyCFunction
)O_seek
, METH_VARARGS
, O_seek__doc__
},
443 {"write", (PyCFunction
)O_write
, METH_VARARGS
, O_write__doc__
},
444 {"writelines", (PyCFunction
)O_writelines
, METH_VARARGS
, O_writelines__doc__
},
445 {NULL
, NULL
} /* sentinel */
449 O_dealloc(Oobject
*self
) {
450 if (self
->buf
!= NULL
)
456 O_getattr(Oobject
*self
, char *name
) {
457 if (strcmp(name
, "softspace") == 0) {
458 return PyInt_FromLong(self
->softspace
);
460 return Py_FindMethod(O_methods
, (PyObject
*)self
, name
);
464 O_setattr(Oobject
*self
, char *name
, PyObject
*value
) {
466 if (strcmp(name
, "softspace") != 0) {
467 PyErr_SetString(PyExc_AttributeError
, name
);
470 x
= PyInt_AsLong(value
);
471 if (x
< 0 && PyErr_Occurred())
477 PyDoc_STRVAR(Otype__doc__
, "Simple type for output to strings.");
479 static PyTypeObject Otype
= {
480 PyObject_HEAD_INIT(NULL
)
482 "cStringIO.StringO", /*tp_name*/
483 sizeof(Oobject
), /*tp_basicsize*/
486 (destructor
)O_dealloc
, /*tp_dealloc*/
487 (printfunc
)0, /*tp_print*/
488 (getattrfunc
)O_getattr
, /*tp_getattr*/
489 (setattrfunc
)O_setattr
, /*tp_setattr*/
490 (cmpfunc
)0, /*tp_compare*/
491 (reprfunc
)0, /*tp_repr*/
493 0, /*tp_as_sequence*/
495 (hashfunc
)0, /*tp_hash*/
496 (ternaryfunc
)0, /*tp_call*/
497 (reprfunc
)0, /*tp_str*/
499 /* Space for future expansion */
501 Otype__doc__
/* Documentation string */
505 newOobject(int size
) {
508 self
= PyObject_New(Oobject
, &Otype
);
512 self
->string_size
= 0;
515 UNLESS (self
->buf
=malloc(size
*sizeof(char))) {
516 PyErr_SetString(PyExc_MemoryError
,"out of memory");
522 return (PyObject
*)self
;
525 /* End of code for StringO objects */
526 /* -------------------------------------------------------- */
529 I_close(Iobject
*self
, PyObject
*args
) {
531 UNLESS (PyArg_ParseTuple(args
, ":close")) return NULL
;
533 Py_XDECREF(self
->pbuf
);
537 self
->pos
= self
->string_size
= 0;
544 I_seek(Iobject
*self
, PyObject
*args
) {
545 int position
, mode
= 0;
547 UNLESS (IO__opencheck(IOOOBJECT(self
))) return NULL
;
548 UNLESS (PyArg_ParseTuple(args
, "i|i:seek", &position
, &mode
))
551 if (mode
== 2) position
+= self
->string_size
;
552 else if (mode
== 1) position
+= self
->pos
;
554 if (position
< 0) position
=0;
562 static struct PyMethodDef I_methods
[] = {
563 /* 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__
},
574 /* Read-only StringIO specific methods: */
575 {"close", (PyCFunction
)I_close
, METH_VARARGS
, O_close__doc__
},
576 {"seek", (PyCFunction
)I_seek
, METH_VARARGS
, O_seek__doc__
},
581 I_dealloc(Iobject
*self
) {
582 Py_XDECREF(self
->pbuf
);
587 I_getattr(Iobject
*self
, char *name
) {
588 return Py_FindMethod(I_methods
, (PyObject
*)self
, name
);
592 I_getiter(Iobject
*self
)
594 PyObject
*myreadline
= PyObject_GetAttrString((PyObject
*)self
,
596 PyObject
*emptystring
= PyString_FromString("");
597 PyObject
*iter
= NULL
;
598 if (!myreadline
|| !emptystring
)
601 iter
= PyCallIter_New(myreadline
, emptystring
);
603 Py_XDECREF(myreadline
);
604 Py_XDECREF(emptystring
);
609 PyDoc_STRVAR(Itype__doc__
,
610 "Simple type for treating strings as input file streams");
612 static PyTypeObject Itype
= {
613 PyObject_HEAD_INIT(NULL
)
615 "cStringIO.StringI", /*tp_name*/
616 sizeof(Iobject
), /*tp_basicsize*/
619 (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*/
626 0, /*tp_as_sequence*/
628 (hashfunc
)0, /*tp_hash*/
629 (ternaryfunc
)0, /*tp_call*/
630 (reprfunc
)0, /*tp_str*/
633 0, /* tp_as_buffer */
634 Py_TPFLAGS_DEFAULT
, /* tp_flags */
635 Itype__doc__
, /* tp_doc */
638 0, /* tp_richcompare */
639 0, /* tp_weaklistoffset */
640 (getiterfunc
)I_getiter
, /* tp_iter */
645 newIobject(PyObject
*s
) {
650 if (PyObject_AsReadBuffer(s
, (const void **)&buf
, &size
)) {
651 PyErr_Format(PyExc_TypeError
, "expected read buffer, %.200s found",
652 s
->ob_type
->tp_name
);
655 UNLESS (self
= PyObject_New(Iobject
, &Itype
)) return NULL
;
658 self
->string_size
=size
;
662 return (PyObject
*)self
;
665 /* End of code for StringI objects */
666 /* -------------------------------------------------------- */
669 PyDoc_STRVAR(IO_StringIO__doc__
,
670 "StringIO([s]) -- Return a StringIO-like stream for reading or writing");
673 IO_StringIO(PyObject
*self
, PyObject
*args
) {
676 if (!PyArg_ParseTuple(args
, "|O:StringIO", &s
)) return NULL
;
678 if (s
) return newIobject(s
);
679 return newOobject(128);
682 /* List of methods defined in the module */
684 static struct PyMethodDef IO_methods
[] = {
685 {"StringIO", (PyCFunction
)IO_StringIO
,
686 METH_VARARGS
, IO_StringIO__doc__
},
687 {NULL
, NULL
} /* sentinel */
691 /* Initialization function for the module (*must* be called initcStringIO) */
693 static struct PycStringIO_CAPI CAPI
= {
704 #ifndef DL_EXPORT /* declarations for DLL import/export */
705 #define DL_EXPORT(RTYPE) RTYPE
708 initcStringIO(void) {
712 /* Create the module and add the functions */
713 m
= Py_InitModule4("cStringIO", IO_methods
,
714 cStringIO_module_documentation
,
715 (PyObject
*)NULL
,PYTHON_API_VERSION
);
717 /* Add some symbolic constants to the module */
718 d
= PyModule_GetDict(m
);
721 Itype
.ob_type
=&PyType_Type
;
722 Otype
.ob_type
=&PyType_Type
;
723 PyDict_SetItemString(d
,"cStringIO_CAPI",
724 v
= PyCObject_FromVoidPtr(&CAPI
,NULL
));
728 PyDict_SetItemString(d
,"InputType", (PyObject
*)&Itype
);
729 PyDict_SetItemString(d
,"OutputType", (PyObject
*)&Otype
);
731 /* Maybe make certain warnings go away */
732 if (0) PycString_IMPORT
;