5 #include "structmember.h"
7 PyDoc_STRVAR(cStringIO_module_documentation
,
8 "A simple fast partial StringIO replacement.\n"
10 "This module provides a simple useful replacement for\n"
11 "the StringIO module that is written in C. It does not provide the\n"
12 "full generality of StringIO, but it provides enough for most\n"
13 "applications and is especially useful in conjunction with the\n"
18 " from cStringIO import StringIO\n"
20 " an_output_stream=StringIO()\n"
21 " an_output_stream.write(some_stuff)\n"
23 " value=an_output_stream.getvalue()\n"
25 " an_input_stream=StringIO(a_string)\n"
26 " spam=an_input_stream.readline()\n"
27 " spam=an_input_stream.read(5)\n"
28 " an_input_stream.seek(0) # OK, start over\n"
29 " spam=an_input_stream.read() # and read it all\n"
31 "If someone else wants to provide a more complete implementation,\n"
34 "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
36 /* Declaration for file-like objects that manage data as strings
38 The IOobject type should be though of as a common base type for
39 Iobjects, which provide input (read-only) StringIO objects and
40 Oobjects, which provide read-write objects. Most of the methods
41 depend only on common data.
47 Py_ssize_t pos
, string_size
;
50 #define IOOOBJECT(O) ((IOobject*)(O))
52 /* Declarations for objects of type StringO */
54 typedef struct { /* Subtype of IOobject */
57 Py_ssize_t pos
, string_size
;
63 /* Declarations for objects of type StringI */
65 typedef struct { /* Subtype of IOobject */
68 Py_ssize_t pos
, string_size
;
72 /* IOobject (common) methods */
74 PyDoc_STRVAR(IO_flush__doc__
, "flush(): does nothing.");
77 IO__opencheck(IOobject
*self
) {
79 PyErr_SetString(PyExc_ValueError
,
80 "I/O operation on closed file");
87 IO_get_closed(IOobject
*self
, void *closure
)
89 PyObject
*result
= Py_False
;
91 if (self
->buf
== NULL
)
97 static PyGetSetDef file_getsetlist
[] = {
98 {"closed", (getter
)IO_get_closed
, NULL
, "True if the file is closed"},
103 IO_flush(IOobject
*self
, PyObject
*unused
) {
105 if (!IO__opencheck(self
)) return NULL
;
111 PyDoc_STRVAR(IO_getval__doc__
,
112 "getvalue([use_pos]) -- Get the string value."
114 "If use_pos is specified and is a true value, then the string returned\n"
115 "will include only the text up to the current file position.\n");
118 IO_cgetval(PyObject
*self
) {
119 if (!IO__opencheck(IOOOBJECT(self
))) return NULL
;
120 assert(IOOOBJECT(self
)->pos
>= 0);
121 return PyString_FromStringAndSize(((IOobject
*)self
)->buf
,
122 ((IOobject
*)self
)->pos
);
126 IO_getval(IOobject
*self
, PyObject
*args
) {
127 PyObject
*use_pos
=Py_None
;
131 if (!IO__opencheck(self
)) return NULL
;
132 if (!PyArg_UnpackTuple(args
,"getval", 0, 1,&use_pos
)) return NULL
;
134 b
= PyObject_IsTrue(use_pos
);
139 if (s
> self
->string_size
) s
=self
->string_size
;
143 assert(self
->pos
>= 0);
144 return PyString_FromStringAndSize(self
->buf
, s
);
147 PyDoc_STRVAR(IO_isatty__doc__
, "isatty(): always returns 0");
150 IO_isatty(IOobject
*self
, PyObject
*unused
) {
151 if (!IO__opencheck(self
)) return NULL
;
156 PyDoc_STRVAR(IO_read__doc__
,
157 "read([s]) -- Read s characters, or the rest of the string");
160 IO_cread(PyObject
*self
, char **output
, Py_ssize_t n
) {
163 if (!IO__opencheck(IOOOBJECT(self
))) return -1;
164 assert(IOOOBJECT(self
)->pos
>= 0);
165 assert(IOOOBJECT(self
)->string_size
>= 0);
166 l
= ((IOobject
*)self
)->string_size
- ((IOobject
*)self
)->pos
;
167 if (n
< 0 || n
> l
) {
172 PyErr_SetString(PyExc_OverflowError
,
177 *output
=((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->pos
;
178 ((IOobject
*)self
)->pos
+= n
;
183 IO_read(IOobject
*self
, PyObject
*args
) {
187 if (!PyArg_ParseTuple(args
, "|n:read", &n
)) return NULL
;
189 if ( (n
=IO_cread((PyObject
*)self
,&output
,n
)) < 0) return NULL
;
191 return PyString_FromStringAndSize(output
, n
);
194 PyDoc_STRVAR(IO_readline__doc__
, "readline() -- Read one line");
197 IO_creadline(PyObject
*self
, char **output
) {
198 char *n
, *start
, *end
;
201 if (!IO__opencheck(IOOOBJECT(self
))) return -1;
203 n
= start
= ((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->pos
;
204 end
= ((IOobject
*)self
)->buf
+ ((IOobject
*)self
)->string_size
;
205 while (n
< end
&& *n
!= '\n')
216 assert(IOOOBJECT(self
)->pos
<= PY_SSIZE_T_MAX
- len
);
217 assert(IOOOBJECT(self
)->pos
>= 0);
218 assert(IOOOBJECT(self
)->string_size
>= 0);
220 ((IOobject
*)self
)->pos
+= len
;
225 IO_readline(IOobject
*self
, PyObject
*args
) {
230 if (!PyArg_ParseTuple(args
, "|i:readline", &m
)) return NULL
;
232 if( (n
=IO_creadline((PyObject
*)self
,&output
)) < 0) return NULL
;
233 if (m
>= 0 && m
< n
) {
238 assert(IOOOBJECT(self
)->pos
>= 0);
239 return PyString_FromStringAndSize(output
, n
);
242 PyDoc_STRVAR(IO_readlines__doc__
, "readlines() -- Read all lines");
245 IO_readlines(IOobject
*self
, PyObject
*args
) {
248 PyObject
*result
, *line
;
249 Py_ssize_t hint
= 0, length
= 0;
251 if (!PyArg_ParseTuple(args
, "|n:readlines", &hint
)) return NULL
;
253 result
= PyList_New(0);
258 if ( (n
= IO_creadline((PyObject
*)self
,&output
)) < 0)
262 line
= PyString_FromStringAndSize (output
, n
);
265 if (PyList_Append (result
, line
) == -1) {
271 if (hint
> 0 && length
>= hint
)
280 PyDoc_STRVAR(IO_reset__doc__
,
281 "reset() -- Reset the file position to the beginning");
284 IO_reset(IOobject
*self
, PyObject
*unused
) {
286 if (!IO__opencheck(self
)) return NULL
;
294 PyDoc_STRVAR(IO_tell__doc__
, "tell() -- get the current position.");
297 IO_tell(IOobject
*self
, PyObject
*unused
) {
299 if (!IO__opencheck(self
)) return NULL
;
301 assert(self
->pos
>= 0);
302 return PyInt_FromSsize_t(self
->pos
);
305 PyDoc_STRVAR(IO_truncate__doc__
,
306 "truncate(): truncate the file at the current position.");
309 IO_truncate(IOobject
*self
, PyObject
*args
) {
312 if (!IO__opencheck(self
)) return NULL
;
313 if (!PyArg_ParseTuple(args
, "|n:truncate", &pos
)) return NULL
;
315 if (PyTuple_Size(args
) == 0) {
316 /* No argument passed, truncate to current position */
322 PyErr_SetFromErrno(PyExc_IOError
);
326 if (self
->string_size
> pos
) self
->string_size
= pos
;
327 self
->pos
= self
->string_size
;
334 IO_iternext(Iobject
*self
)
337 next
= IO_readline((IOobject
*)self
, NULL
);
340 if (!PyString_GET_SIZE(next
)) {
342 PyErr_SetNone(PyExc_StopIteration
);
351 /* Read-write object methods */
353 PyDoc_STRVAR(IO_seek__doc__
,
354 "seek(position) -- set the current position\n"
355 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
358 IO_seek(Iobject
*self
, PyObject
*args
) {
362 if (!IO__opencheck(IOOOBJECT(self
))) return NULL
;
363 if (!PyArg_ParseTuple(args
, "n|i:seek", &position
, &mode
))
367 position
+= self
->string_size
;
369 else if (mode
== 1) {
370 position
+= self
->pos
;
373 if (position
< 0) position
=0;
381 PyDoc_STRVAR(O_write__doc__
,
382 "write(s) -- Write a string to the file"
383 "\n\nNote (hack:) writing None resets the buffer");
387 O_cwrite(PyObject
*self
, const char *c
, Py_ssize_t len
) {
392 if (!IO__opencheck(IOOOBJECT(self
))) return -1;
393 oself
= (Oobject
*)self
;
396 PyErr_SetString(PyExc_OverflowError
,
401 if (oself
->pos
>= PY_SSIZE_T_MAX
- len
) {
402 PyErr_SetString(PyExc_OverflowError
,
403 "new position too large");
406 newpos
= oself
->pos
+ len
;
407 if (newpos
>= oself
->buf_size
) {
408 size_t newsize
= oself
->buf_size
;
410 if (newsize
<= (size_t)newpos
|| newsize
> PY_SSIZE_T_MAX
) {
411 assert(newpos
< PY_SSIZE_T_MAX
- 1);
412 newsize
= newpos
+ 1;
414 newbuf
= (char*)realloc(oself
->buf
, newsize
);
416 PyErr_SetString(PyExc_MemoryError
,"out of memory");
419 oself
->buf_size
= (Py_ssize_t
)newsize
;
423 if (oself
->string_size
< oself
->pos
) {
424 /* In case of overseek, pad with null bytes the buffer region between
425 the end of stream and the current position.
428 | |<---used--->|<----------available----------->|
429 | | <--to pad-->|<---to write---> |
432 memset(oself
->buf
+ oself
->string_size
, '\0',
433 (oself
->pos
- oself
->string_size
) * sizeof(char));
436 memcpy(oself
->buf
+ oself
->pos
, c
, len
);
440 if (oself
->string_size
< oself
->pos
) {
441 oself
->string_size
= oself
->pos
;
448 O_write(Oobject
*self
, PyObject
*args
) {
452 if (!PyArg_ParseTuple(args
, "s*:write", &buf
)) return NULL
;
454 result
= O_cwrite((PyObject
*)self
, buf
.buf
, buf
.len
);
455 PyBuffer_Release(&buf
);
456 if (result
< 0) return NULL
;
462 PyDoc_STRVAR(O_close__doc__
, "close(): explicitly release resources held.");
465 O_close(Oobject
*self
, PyObject
*unused
) {
466 if (self
->buf
!= NULL
) free(self
->buf
);
469 self
->pos
= self
->string_size
= self
->buf_size
= 0;
475 PyDoc_STRVAR(O_writelines__doc__
,
476 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
478 "Note that newlines are not added. The sequence can be any iterable object\n"
479 "producing strings. This is equivalent to calling write() for each string.");
481 O_writelines(Oobject
*self
, PyObject
*args
) {
484 it
= PyObject_GetIter(args
);
487 while ((s
= PyIter_Next(it
)) != NULL
) {
490 if (PyString_AsStringAndSize(s
, &c
, &n
) == -1) {
495 if (O_cwrite((PyObject
*)self
, c
, n
) == -1) {
505 /* See if PyIter_Next failed */
506 if (PyErr_Occurred())
511 static struct PyMethodDef O_methods
[] = {
512 /* Common methods: */
513 {"flush", (PyCFunction
)IO_flush
, METH_NOARGS
, IO_flush__doc__
},
514 {"getvalue", (PyCFunction
)IO_getval
, METH_VARARGS
, IO_getval__doc__
},
515 {"isatty", (PyCFunction
)IO_isatty
, METH_NOARGS
, IO_isatty__doc__
},
516 {"read", (PyCFunction
)IO_read
, METH_VARARGS
, IO_read__doc__
},
517 {"readline", (PyCFunction
)IO_readline
, METH_VARARGS
, IO_readline__doc__
},
518 {"readlines", (PyCFunction
)IO_readlines
,METH_VARARGS
, IO_readlines__doc__
},
519 {"reset", (PyCFunction
)IO_reset
, METH_NOARGS
, IO_reset__doc__
},
520 {"seek", (PyCFunction
)IO_seek
, METH_VARARGS
, IO_seek__doc__
},
521 {"tell", (PyCFunction
)IO_tell
, METH_NOARGS
, IO_tell__doc__
},
522 {"truncate", (PyCFunction
)IO_truncate
, METH_VARARGS
, IO_truncate__doc__
},
524 /* Read-write StringIO specific methods: */
525 {"close", (PyCFunction
)O_close
, METH_NOARGS
, O_close__doc__
},
526 {"write", (PyCFunction
)O_write
, METH_VARARGS
, O_write__doc__
},
527 {"writelines", (PyCFunction
)O_writelines
, METH_O
, O_writelines__doc__
},
528 {NULL
, NULL
} /* sentinel */
531 static PyMemberDef O_memberlist
[] = {
532 {"softspace", T_INT
, offsetof(Oobject
, softspace
), 0,
533 "flag indicating that a space needs to be printed; used by print"},
534 /* getattr(f, "closed") is implemented without this table */
535 {NULL
} /* Sentinel */
539 O_dealloc(Oobject
*self
) {
540 if (self
->buf
!= NULL
)
545 PyDoc_STRVAR(Otype__doc__
, "Simple type for output to strings.");
547 static PyTypeObject Otype
= {
548 PyVarObject_HEAD_INIT(NULL
, 0)
549 "cStringIO.StringO", /*tp_name*/
550 sizeof(Oobject
), /*tp_basicsize*/
553 (destructor
)O_dealloc
, /*tp_dealloc*/
560 0, /*tp_as_sequence*/
568 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
569 Otype__doc__
, /*tp_doc */
572 0, /*tp_richcompare */
573 0, /*tp_weaklistoffset */
574 PyObject_SelfIter
, /*tp_iter */
575 (iternextfunc
)IO_iternext
, /*tp_iternext */
576 O_methods
, /*tp_methods */
577 O_memberlist
, /*tp_members */
578 file_getsetlist
, /*tp_getset */
582 newOobject(int size
) {
585 self
= PyObject_New(Oobject
, &Otype
);
589 self
->string_size
= 0;
592 self
->buf
= (char *)malloc(size
);
594 PyErr_SetString(PyExc_MemoryError
,"out of memory");
601 return (PyObject
*)self
;
604 /* End of code for StringO objects */
605 /* -------------------------------------------------------- */
608 I_close(Iobject
*self
, PyObject
*unused
) {
609 PyBuffer_Release(&self
->pbuf
);
612 self
->pos
= self
->string_size
= 0;
618 static struct PyMethodDef I_methods
[] = {
619 /* Common methods: */
620 {"flush", (PyCFunction
)IO_flush
, METH_NOARGS
, IO_flush__doc__
},
621 {"getvalue", (PyCFunction
)IO_getval
, METH_VARARGS
, IO_getval__doc__
},
622 {"isatty", (PyCFunction
)IO_isatty
, METH_NOARGS
, IO_isatty__doc__
},
623 {"read", (PyCFunction
)IO_read
, METH_VARARGS
, IO_read__doc__
},
624 {"readline", (PyCFunction
)IO_readline
, METH_VARARGS
, IO_readline__doc__
},
625 {"readlines", (PyCFunction
)IO_readlines
,METH_VARARGS
, IO_readlines__doc__
},
626 {"reset", (PyCFunction
)IO_reset
, METH_NOARGS
, IO_reset__doc__
},
627 {"seek", (PyCFunction
)IO_seek
, METH_VARARGS
, IO_seek__doc__
},
628 {"tell", (PyCFunction
)IO_tell
, METH_NOARGS
, IO_tell__doc__
},
629 {"truncate", (PyCFunction
)IO_truncate
, METH_VARARGS
, IO_truncate__doc__
},
631 /* Read-only StringIO specific methods: */
632 {"close", (PyCFunction
)I_close
, METH_NOARGS
, O_close__doc__
},
637 I_dealloc(Iobject
*self
) {
638 PyBuffer_Release(&self
->pbuf
);
643 PyDoc_STRVAR(Itype__doc__
,
644 "Simple type for treating strings as input file streams");
646 static PyTypeObject Itype
= {
647 PyVarObject_HEAD_INIT(NULL
, 0)
648 "cStringIO.StringI", /*tp_name*/
649 sizeof(Iobject
), /*tp_basicsize*/
652 (destructor
)I_dealloc
, /*tp_dealloc*/
659 0, /*tp_as_sequence*/
666 0, /* tp_as_buffer */
667 Py_TPFLAGS_DEFAULT
, /* tp_flags */
668 Itype__doc__
, /* tp_doc */
671 0, /* tp_richcompare */
672 0, /* tp_weaklistoffset */
673 PyObject_SelfIter
, /* tp_iter */
674 (iternextfunc
)IO_iternext
, /* tp_iternext */
675 I_methods
, /* tp_methods */
677 file_getsetlist
, /* tp_getset */
681 newIobject(PyObject
*s
) {
687 args
= Py_BuildValue("(O)", s
);
690 result
= PyArg_ParseTuple(args
, "s*:StringIO", &buf
);
695 self
= PyObject_New(Iobject
, &Itype
);
697 PyBuffer_Release(&buf
);
701 self
->string_size
=buf
.len
;
705 return (PyObject
*)self
;
708 /* End of code for StringI objects */
709 /* -------------------------------------------------------- */
712 PyDoc_STRVAR(IO_StringIO__doc__
,
713 "StringIO([s]) -- Return a StringIO-like stream for reading or writing");
716 IO_StringIO(PyObject
*self
, PyObject
*args
) {
719 if (!PyArg_UnpackTuple(args
, "StringIO", 0, 1, &s
)) return NULL
;
721 if (s
) return newIobject(s
);
722 return newOobject(128);
725 /* List of methods defined in the module */
727 static struct PyMethodDef IO_methods
[] = {
728 {"StringIO", (PyCFunction
)IO_StringIO
,
729 METH_VARARGS
, IO_StringIO__doc__
},
730 {NULL
, NULL
} /* sentinel */
734 /* Initialization function for the module (*must* be called initcStringIO) */
736 static struct PycStringIO_CAPI CAPI
= {
747 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
748 #define PyMODINIT_FUNC void
751 initcStringIO(void) {
755 /* Create the module and add the functions */
756 m
= Py_InitModule4("cStringIO", IO_methods
,
757 cStringIO_module_documentation
,
758 (PyObject
*)NULL
,PYTHON_API_VERSION
);
759 if (m
== NULL
) return;
761 /* Add some symbolic constants to the module */
762 d
= PyModule_GetDict(m
);
765 Py_TYPE(&Itype
)=&PyType_Type
;
766 Py_TYPE(&Otype
)=&PyType_Type
;
767 if (PyType_Ready(&Otype
) < 0) return;
768 if (PyType_Ready(&Itype
) < 0) return;
769 v
= PyCapsule_New(&CAPI
, PycStringIO_CAPSULE_NAME
, NULL
);
770 PyDict_SetItemString(d
,"cStringIO_CAPI", v
);
774 PyDict_SetItemString(d
,"InputType", (PyObject
*)&Itype
);
775 PyDict_SetItemString(d
,"OutputType", (PyObject
*)&Otype
);
777 /* Maybe make certain warnings go away */
778 if (0) PycString_IMPORT
;