| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "import.h"
 | 
					
						
							|  |  |  | #include "cStringIO.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(cStringIO_module_documentation, | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | "A simple fast partial StringIO replacement.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "This module provides a simple useful replacement for\n" | 
					
						
							|  |  |  | "the StringIO module that is written in C.  It does not provide the\n" | 
					
						
							| 
									
										
										
										
											2000-06-19 13:17:41 +00:00
										 |  |  | "full generality of StringIO, but it provides enough for most\n" | 
					
						
							| 
									
										
										
										
											2000-07-16 12:04:32 +00:00
										 |  |  | "applications and is especially useful in conjunction with the\n" | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | "pickle module.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "Usage:\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "  from cStringIO import StringIO\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "  an_output_stream=StringIO()\n" | 
					
						
							|  |  |  | "  an_output_stream.write(some_stuff)\n" | 
					
						
							|  |  |  | "  ...\n" | 
					
						
							| 
									
										
										
										
											2002-03-08 17:17:33 +00:00
										 |  |  | "  value=an_output_stream.getvalue()\n" | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "  an_input_stream=StringIO(a_string)\n" | 
					
						
							|  |  |  | "  spam=an_input_stream.readline()\n" | 
					
						
							|  |  |  | "  spam=an_input_stream.read(5)\n" | 
					
						
							| 
									
										
										
										
											1998-11-25 16:17:32 +00:00
										 |  |  | "  an_input_stream.seek(0)           # OK, start over\n" | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | "  spam=an_input_stream.read()       # and read it all\n" | 
					
						
							|  |  |  | "  \n" | 
					
						
							|  |  |  | "If someone else wants to provide a more complete implementation,\n" | 
					
						
							|  |  |  | "go for it. :-)  \n" | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | #define UNLESS(E) if (!(E))
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Declaration for file-like objects that manage data as strings 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The IOobject type should be though of as a common base type for | 
					
						
							|  |  |  |    Iobjects, which provide input (read-only) StringIO objects and | 
					
						
							|  |  |  |    Oobjects, which provide read-write objects.  Most of the methods | 
					
						
							|  |  |  |    depend only on common data. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   PyObject_HEAD | 
					
						
							|  |  |  |   char *buf; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   int pos, string_size; | 
					
						
							|  |  |  | } IOobject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define IOOOBJECT(O) ((IOobject*)(O))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Declarations for objects of type StringO */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { /* Subtype of IOobject */ | 
					
						
							|  |  |  |   PyObject_HEAD | 
					
						
							|  |  |  |   char *buf; | 
					
						
							|  |  |  |   int pos, string_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int buf_size, softspace; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } Oobject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Declarations for objects of type StringI */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | typedef struct { /* Subtype of IOobject */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   PyObject_HEAD | 
					
						
							|  |  |  |   char *buf; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  |   int pos, string_size; | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |   /* We store a reference to the object here in order to keep
 | 
					
						
							|  |  |  |      the buffer alive during the lifetime of the Iobject. */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   PyObject *pbuf; | 
					
						
							|  |  |  | } Iobject; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | /* IOobject (common) methods */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing."); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | IO__opencheck(IOobject *self) { | 
					
						
							|  |  |  |         UNLESS (self->buf) { | 
					
						
							|  |  |  |                 PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  |                                 "I/O operation on closed file"); | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | IO_flush(IOobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (IO__opencheck(self)) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_getval__doc__, | 
					
						
							|  |  |  | "getvalue([use_pos]) -- Get the string value." | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "If use_pos is specified and is a true value, then the string returned\n" | 
					
						
							|  |  |  | "will include only the text up to the current file position.\n"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_cgetval(PyObject *self) { | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; | 
					
						
							|  |  |  |         return PyString_FromStringAndSize(((IOobject*)self)->buf, | 
					
						
							|  |  |  |                                           ((IOobject*)self)->pos); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | IO_getval(IOobject *self, PyObject *args) { | 
					
						
							|  |  |  |         PyObject *use_pos=Py_None; | 
					
						
							|  |  |  |         int s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(self)) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (PyObject_IsTrue(use_pos)) { | 
					
						
							|  |  |  |                   s=self->pos; | 
					
						
							|  |  |  |                   if (s > self->string_size) s=self->string_size; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |                   s=self->string_size; | 
					
						
							|  |  |  |         return PyString_FromStringAndSize(self->buf, s); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | IO_isatty(IOobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1998-11-25 16:17:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-01 15:06:28 +00:00
										 |  |  | 	Py_INCREF(Py_False); | 
					
						
							|  |  |  |         return Py_False; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_read__doc__, | 
					
						
							|  |  |  | "read([s]) -- Read s characters, or the rest of the string"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_cread(PyObject *self, char **output, int  n) { | 
					
						
							|  |  |  |         int l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; | 
					
						
							|  |  |  |         l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;   | 
					
						
							|  |  |  |         if (n < 0 || n > l) { | 
					
						
							|  |  |  |                 n = l; | 
					
						
							|  |  |  |                 if (n < 0) n=0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; | 
					
						
							|  |  |  |         ((IOobject*)self)->pos += n; | 
					
						
							|  |  |  |         return n; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_read(IOobject *self, PyObject *args) { | 
					
						
							|  |  |  |         int n = -1; | 
					
						
							|  |  |  |         char *output; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         return PyString_FromStringAndSize(output, n); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_creadline(PyObject *self, char **output) { | 
					
						
							|  |  |  |         char *n, *s; | 
					
						
							|  |  |  |         int l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos, | 
					
						
							|  |  |  |                s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;  | 
					
						
							|  |  |  |              n < s && *n != '\n'; n++); | 
					
						
							|  |  |  |         if (n < s) n++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; | 
					
						
							|  |  |  |         l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos; | 
					
						
							|  |  |  |         ((IOobject*)self)->pos += l; | 
					
						
							|  |  |  |         return l; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_readline(IOobject *self, PyObject *args) { | 
					
						
							|  |  |  |         int n, m=-1; | 
					
						
							|  |  |  |         char *output; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL; | 
					
						
							|  |  |  |         if (m >= 0 && m < n) { | 
					
						
							|  |  |  |                 m = n - m; | 
					
						
							|  |  |  |                 n -= m; | 
					
						
							|  |  |  |                 self->pos -= m; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return PyString_FromStringAndSize(output, n); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines"); | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_readlines(IOobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | 	int n; | 
					
						
							|  |  |  | 	char *output; | 
					
						
							|  |  |  | 	PyObject *result, *line; | 
					
						
							|  |  |  |         int hint = 0, length = 0; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | 	result = PyList_New(0); | 
					
						
							|  |  |  | 	if (!result) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 	while (1){ | 
					
						
							|  |  |  | 		if ( (n = IO_creadline((PyObject*)self,&output)) < 0) | 
					
						
							|  |  |  |                         goto err; | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | 		if (n == 0) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		line = PyString_FromStringAndSize (output, n); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 		if (!line)  | 
					
						
							|  |  |  |                         goto err; | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | 		PyList_Append (result, line); | 
					
						
							|  |  |  | 		Py_DECREF (line); | 
					
						
							|  |  |  |                 length += n; | 
					
						
							|  |  |  |                 if (hint > 0 && length >= hint) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |  err: | 
					
						
							|  |  |  |         Py_DECREF(result); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2000-09-19 11:06:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_reset__doc__, | 
					
						
							|  |  |  | "reset() -- Reset the file position to the beginning"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | IO_reset(IOobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (IO__opencheck(self)) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         self->pos = 0; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position."); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_tell(IOobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (IO__opencheck(self)) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         return PyInt_FromLong(self->pos); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_truncate__doc__, | 
					
						
							|  |  |  | "truncate(): truncate the file at the current position."); | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | IO_truncate(IOobject *self, PyObject *args) { | 
					
						
							|  |  |  |         int pos = -1; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(self)) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL; | 
					
						
							|  |  |  |         if (pos < 0) pos = self->pos; | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (self->string_size > pos) self->string_size = pos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Read-write object methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(O_seek__doc__, | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | "seek(position)       -- set the current position\n" | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | O_seek(Oobject *self, PyObject *args) { | 
					
						
							|  |  |  |         int position, mode = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))  | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (mode == 2) { | 
					
						
							|  |  |  |                 position += self->string_size; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (mode == 1) { | 
					
						
							|  |  |  |                 position += self->pos; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (position > self->buf_size) { | 
					
						
							|  |  |  |                   self->buf_size*=2; | 
					
						
							|  |  |  |                   if (self->buf_size <= position) self->buf_size=position+1; | 
					
						
							|  |  |  |                   UNLESS (self->buf=(char*) | 
					
						
							|  |  |  |                           realloc(self->buf,self->buf_size*sizeof(char))) { | 
					
						
							|  |  |  |                       self->buf_size=self->pos=0; | 
					
						
							|  |  |  |                       return PyErr_NoMemory(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         else if (position < 0) position=0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self->pos=position; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (--position >= self->string_size) self->buf[position]=0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(O_write__doc__, | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | "write(s) -- Write a string to the file" | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | "\n\nNote (hack:) writing None resets the buffer"); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | O_cwrite(PyObject *self, char *c, int  l) { | 
					
						
							|  |  |  |         int newl; | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |         Oobject *oself; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |         oself = (Oobject *)self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         newl = oself->pos+l; | 
					
						
							|  |  |  |         if (newl >= oself->buf_size) { | 
					
						
							|  |  |  |             oself->buf_size *= 2; | 
					
						
							|  |  |  |             if (oself->buf_size <= newl)  | 
					
						
							|  |  |  |                     oself->buf_size = newl+1; | 
					
						
							|  |  |  |             UNLESS (oself->buf =  | 
					
						
							|  |  |  |                     (char*)realloc(oself->buf, | 
					
						
							|  |  |  |                                    (oself->buf_size) * sizeof(char))) { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |                     PyErr_SetString(PyExc_MemoryError,"out of memory"); | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |                     oself->buf_size = oself->pos = 0; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |                     return -1; | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |         memcpy(oself->buf+oself->pos,c,l); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |         oself->pos += l; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-07 20:20:28 +00:00
										 |  |  |         if (oself->string_size < oself->pos) { | 
					
						
							|  |  |  |             oself->string_size = oself->pos; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return l; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | O_write(Oobject *self, PyObject *args) { | 
					
						
							|  |  |  |         char *c; | 
					
						
							|  |  |  |         int l; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-29 13:54:48 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL; | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (O_cwrite((PyObject*)self,c,l) < 0) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held."); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | O_close(Oobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, ":close")) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (self->buf != NULL) free(self->buf); | 
					
						
							|  |  |  |         self->buf = NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         self->pos = self->string_size = self->buf_size = 0; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(O_writelines__doc__, | 
					
						
							|  |  |  | "writelines(sequence_of_strings): write each string"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | O_writelines(Oobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         PyObject *tmp = 0; | 
					
						
							| 
									
										
										
										
											2001-02-09 23:44:22 +00:00
										 |  |  | 	static PyObject *joiner = NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-09 23:44:22 +00:00
										 |  |  | 	if (!joiner) { | 
					
						
							|  |  |  | 		PyObject *empty_string = PyString_FromString(""); | 
					
						
							|  |  |  | 		if (empty_string == NULL) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		joiner = PyObject_GetAttrString(empty_string, "join"); | 
					
						
							|  |  |  | 		Py_DECREF(empty_string); | 
					
						
							|  |  |  | 		if (joiner == NULL) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (PyObject_Size(args) < 0) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-09 23:44:22 +00:00
										 |  |  |         tmp = PyObject_CallFunction(joiner, "O", args); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (tmp) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         args = Py_BuildValue("(O)", tmp); | 
					
						
							|  |  |  |         Py_DECREF(tmp); | 
					
						
							|  |  |  |         UNLESS (args) return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         tmp = O_write(self, args); | 
					
						
							|  |  |  |         Py_DECREF(args); | 
					
						
							|  |  |  |         return tmp; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef O_methods[] = { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   /* Common methods: */ | 
					
						
							|  |  |  |   {"flush",     (PyCFunction)IO_flush,    METH_VARARGS, IO_flush__doc__}, | 
					
						
							|  |  |  |   {"getvalue",  (PyCFunction)IO_getval,   METH_VARARGS, IO_getval__doc__}, | 
					
						
							|  |  |  |   {"isatty",    (PyCFunction)IO_isatty,   METH_VARARGS, IO_isatty__doc__}, | 
					
						
							|  |  |  |   {"read",	(PyCFunction)IO_read,     METH_VARARGS, IO_read__doc__}, | 
					
						
							|  |  |  |   {"readline",	(PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, | 
					
						
							|  |  |  |   {"readlines",	(PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, | 
					
						
							|  |  |  |   {"reset",	(PyCFunction)IO_reset,	  METH_VARARGS, IO_reset__doc__}, | 
					
						
							|  |  |  |   {"tell",      (PyCFunction)IO_tell,     METH_VARARGS, IO_tell__doc__}, | 
					
						
							|  |  |  |   {"truncate",  (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Read-write StringIO specific  methods: */ | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  |   {"close",      (PyCFunction)O_close,      METH_VARARGS, O_close__doc__}, | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   {"seek",       (PyCFunction)O_seek,       METH_VARARGS, O_seek__doc__}, | 
					
						
							|  |  |  |   {"write",	 (PyCFunction)O_write,      METH_VARARGS, O_write__doc__}, | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  |   {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__}, | 
					
						
							|  |  |  |   {NULL,	 NULL}		/* sentinel */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | O_dealloc(Oobject *self) { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (self->buf != NULL) | 
					
						
							|  |  |  |                 free(self->buf); | 
					
						
							|  |  |  |         PyObject_Del(self); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | O_getattr(Oobject *self, char *name) { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (strcmp(name, "softspace") == 0) { | 
					
						
							|  |  |  |                 return PyInt_FromLong(self->softspace); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return Py_FindMethod(O_methods, (PyObject *)self, name); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-11 19:56:06 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | O_setattr(Oobject *self, char *name, PyObject *value) { | 
					
						
							|  |  |  | 	long x; | 
					
						
							|  |  |  | 	if (strcmp(name, "softspace") != 0) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_AttributeError, name); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	x = PyInt_AsLong(value); | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | 	if (x < 0 && PyErr_Occurred()) | 
					
						
							| 
									
										
										
										
											1997-04-11 19:56:06 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	self->softspace = x; | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings."); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject Otype = { | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   PyObject_HEAD_INIT(NULL) | 
					
						
							|  |  |  |   0,	       		/*ob_size*/ | 
					
						
							| 
									
										
										
										
											2001-12-08 18:02:58 +00:00
										 |  |  |   "cStringIO.StringO",   		/*tp_name*/ | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   sizeof(Oobject),       	/*tp_basicsize*/ | 
					
						
							|  |  |  |   0,	       		/*tp_itemsize*/ | 
					
						
							|  |  |  |   /* methods */ | 
					
						
							|  |  |  |   (destructor)O_dealloc,	/*tp_dealloc*/ | 
					
						
							|  |  |  |   (printfunc)0,		/*tp_print*/ | 
					
						
							|  |  |  |   (getattrfunc)O_getattr,	/*tp_getattr*/ | 
					
						
							| 
									
										
										
										
											1997-04-11 19:56:06 +00:00
										 |  |  |   (setattrfunc)O_setattr,	/*tp_setattr*/ | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   (cmpfunc)0,		/*tp_compare*/ | 
					
						
							|  |  |  |   (reprfunc)0,		/*tp_repr*/ | 
					
						
							|  |  |  |   0,			/*tp_as_number*/ | 
					
						
							|  |  |  |   0,			/*tp_as_sequence*/ | 
					
						
							|  |  |  |   0,			/*tp_as_mapping*/ | 
					
						
							|  |  |  |   (hashfunc)0,		/*tp_hash*/ | 
					
						
							|  |  |  |   (ternaryfunc)0,		/*tp_call*/ | 
					
						
							|  |  |  |   (reprfunc)0,		/*tp_str*/ | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   /* Space for future expansion */ | 
					
						
							|  |  |  |   0L,0L,0L,0L, | 
					
						
							|  |  |  |   Otype__doc__ 		/* Documentation string */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | newOobject(int  size) { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         Oobject *self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self = PyObject_New(Oobject, &Otype); | 
					
						
							|  |  |  |         if (self == NULL) | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |         self->pos=0; | 
					
						
							|  |  |  |         self->string_size = 0; | 
					
						
							|  |  |  |         self->softspace = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         UNLESS (self->buf=malloc(size*sizeof(char))) { | 
					
						
							|  |  |  |                   PyErr_SetString(PyExc_MemoryError,"out of memory"); | 
					
						
							|  |  |  |                   self->buf_size = 0; | 
					
						
							|  |  |  |                   return NULL; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self->buf_size=size; | 
					
						
							|  |  |  |         return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | /* End of code for StringO objects */ | 
					
						
							|  |  |  | /* -------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | I_close(Iobject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (PyArg_ParseTuple(args, ":close")) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_XDECREF(self->pbuf); | 
					
						
							|  |  |  |         self->pbuf = NULL; | 
					
						
							|  |  |  |         self->buf = NULL; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         self->pos = self->string_size = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  | I_seek(Iobject *self, PyObject *args) { | 
					
						
							|  |  |  |         int position, mode = 0; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; | 
					
						
							|  |  |  |         UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))  | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (mode == 2) position += self->string_size; | 
					
						
							|  |  |  |         else if (mode == 1) position += self->pos; | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         if (position < 0) position=0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self->pos=position; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |         Py_INCREF(Py_None); | 
					
						
							|  |  |  |         return Py_None; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef I_methods[] = { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   /* Common methods: */ | 
					
						
							|  |  |  |   {"flush",     (PyCFunction)IO_flush,    METH_VARARGS, IO_flush__doc__}, | 
					
						
							|  |  |  |   {"getvalue",  (PyCFunction)IO_getval,   METH_VARARGS, IO_getval__doc__}, | 
					
						
							|  |  |  |   {"isatty",    (PyCFunction)IO_isatty,   METH_VARARGS, IO_isatty__doc__}, | 
					
						
							|  |  |  |   {"read",	(PyCFunction)IO_read,     METH_VARARGS, IO_read__doc__}, | 
					
						
							|  |  |  |   {"readline",	(PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, | 
					
						
							|  |  |  |   {"readlines",	(PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, | 
					
						
							|  |  |  |   {"reset",	(PyCFunction)IO_reset,	  METH_VARARGS, IO_reset__doc__}, | 
					
						
							|  |  |  |   {"tell",      (PyCFunction)IO_tell,     METH_VARARGS, IO_tell__doc__}, | 
					
						
							|  |  |  |   {"truncate",  (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Read-only StringIO specific  methods: */ | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  |   {"close",     (PyCFunction)I_close,    METH_VARARGS, O_close__doc__}, | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   {"seek",      (PyCFunction)I_seek,     METH_VARARGS, O_seek__doc__},   | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  |   {NULL,	NULL} | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | I_dealloc(Iobject *self) { | 
					
						
							| 
									
										
										
										
											1997-09-03 00:09:26 +00:00
										 |  |  |   Py_XDECREF(self->pbuf); | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  |   PyObject_Del(self); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | I_getattr(Iobject *self, char *name) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   return Py_FindMethod(I_methods, (PyObject *)self, name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-22 04:36:49 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | I_getiter(Iobject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *myreadline = PyObject_GetAttrString((PyObject*)self, | 
					
						
							|  |  |  | 						      "readline"); | 
					
						
							|  |  |  | 	PyObject *emptystring = PyString_FromString(""); | 
					
						
							|  |  |  | 	PyObject *iter = NULL; | 
					
						
							|  |  |  | 	if (!myreadline || !emptystring) | 
					
						
							|  |  |  | 		goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	iter = PyCallIter_New(myreadline, emptystring); | 
					
						
							|  |  |  |   finally: | 
					
						
							|  |  |  | 	Py_XDECREF(myreadline); | 
					
						
							|  |  |  | 	Py_XDECREF(emptystring); | 
					
						
							|  |  |  | 	return iter; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(Itype__doc__, | 
					
						
							|  |  |  | "Simple type for treating strings as input file streams"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyTypeObject Itype = { | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   PyObject_HEAD_INIT(NULL) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:36:49 +00:00
										 |  |  |   0,					/*ob_size*/ | 
					
						
							| 
									
										
										
										
											2001-12-08 18:02:58 +00:00
										 |  |  |   "cStringIO.StringI",			/*tp_name*/ | 
					
						
							| 
									
										
										
										
											2001-09-22 04:36:49 +00:00
										 |  |  |   sizeof(Iobject),			/*tp_basicsize*/ | 
					
						
							|  |  |  |   0,					/*tp_itemsize*/ | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   /* methods */ | 
					
						
							| 
									
										
										
										
											2001-09-22 04:36:49 +00:00
										 |  |  |   (destructor)I_dealloc,		/*tp_dealloc*/ | 
					
						
							|  |  |  |   (printfunc)0,				/*tp_print*/ | 
					
						
							|  |  |  |   (getattrfunc)I_getattr,		/*tp_getattr*/ | 
					
						
							|  |  |  |   (setattrfunc)0,			/*tp_setattr*/ | 
					
						
							|  |  |  |   (cmpfunc)0,				/*tp_compare*/ | 
					
						
							|  |  |  |   (reprfunc)0,				/*tp_repr*/ | 
					
						
							|  |  |  |   0,					/*tp_as_number*/ | 
					
						
							|  |  |  |   0,					/*tp_as_sequence*/ | 
					
						
							|  |  |  |   0,					/*tp_as_mapping*/ | 
					
						
							|  |  |  |   (hashfunc)0,				/*tp_hash*/ | 
					
						
							|  |  |  |   (ternaryfunc)0,			/*tp_call*/ | 
					
						
							|  |  |  |   (reprfunc)0,				/*tp_str*/ | 
					
						
							|  |  |  |   0,					/* tp_getattro */ | 
					
						
							|  |  |  |   0,					/* tp_setattro */ | 
					
						
							|  |  |  |   0,					/* tp_as_buffer */ | 
					
						
							|  |  |  |   Py_TPFLAGS_DEFAULT,			/* tp_flags */ | 
					
						
							|  |  |  |   Itype__doc__,				/* tp_doc */ | 
					
						
							|  |  |  |   0,					/* tp_traverse */ | 
					
						
							|  |  |  |   0,					/* tp_clear */ | 
					
						
							|  |  |  |   0,					/* tp_richcompare */ | 
					
						
							|  |  |  |   0,					/* tp_weaklistoffset */ | 
					
						
							|  |  |  |   (getiterfunc)I_getiter,		/* tp_iter */ | 
					
						
							|  |  |  |   0,					/* tp_iternext */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | newIobject(PyObject *s) { | 
					
						
							|  |  |  |   Iobject *self; | 
					
						
							|  |  |  |   char *buf; | 
					
						
							|  |  |  |   int size; | 
					
						
							| 
									
										
										
										
											2000-04-12 22:04:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |   if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) { | 
					
						
							|  |  |  |       PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found", | 
					
						
							| 
									
										
										
										
											2000-04-12 22:04:01 +00:00
										 |  |  | 		   s->ob_type->tp_name); | 
					
						
							|  |  |  |       return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL; | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  |   Py_INCREF(s); | 
					
						
							|  |  |  |   self->buf=buf; | 
					
						
							|  |  |  |   self->string_size=size; | 
					
						
							|  |  |  |   self->pbuf=s; | 
					
						
							|  |  |  |   self->pos=0; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   return (PyObject*)self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | /* End of code for StringI objects */ | 
					
						
							|  |  |  | /* -------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(IO_StringIO__doc__, | 
					
						
							|  |  |  | "StringIO([s]) -- Return a StringIO-like stream for reading or writing"); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | IO_StringIO(PyObject *self, PyObject *args) { | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   PyObject *s=0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (s) return newIobject(s); | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   return newOobject(128); | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* List of methods defined in the module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct PyMethodDef IO_methods[] = { | 
					
						
							| 
									
										
										
										
											2000-08-03 02:06:16 +00:00
										 |  |  |   {"StringIO",	(PyCFunction)IO_StringIO,	 | 
					
						
							|  |  |  |    METH_VARARGS,	IO_StringIO__doc__}, | 
					
						
							| 
									
										
										
										
											1997-01-06 22:57:52 +00:00
										 |  |  |   {NULL,		NULL}		/* sentinel */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Initialization function for the module (*must* be called initcStringIO) */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | static struct PycStringIO_CAPI CAPI = { | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   IO_cread, | 
					
						
							|  |  |  |   IO_creadline, | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   O_cwrite, | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   IO_cgetval, | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   newOobject, | 
					
						
							|  |  |  |   newIobject, | 
					
						
							|  |  |  |   &Itype, | 
					
						
							|  |  |  |   &Otype, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-02 02:27:13 +00:00
										 |  |  | #ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
 | 
					
						
							|  |  |  | #define PyMODINIT_FUNC void
 | 
					
						
							| 
									
										
										
										
											1998-12-15 21:43:15 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-08-02 02:27:13 +00:00
										 |  |  | PyMODINIT_FUNC | 
					
						
							| 
									
										
										
										
											2000-07-24 14:43:35 +00:00
										 |  |  | initcStringIO(void) { | 
					
						
							| 
									
										
										
										
											1997-09-03 18:19:40 +00:00
										 |  |  |   PyObject *m, *d, *v; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   /* Create the module and add the functions */ | 
					
						
							|  |  |  |   m = Py_InitModule4("cStringIO", IO_methods, | 
					
						
							|  |  |  | 		     cStringIO_module_documentation, | 
					
						
							|  |  |  | 		     (PyObject*)NULL,PYTHON_API_VERSION); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Add some symbolic constants to the module */ | 
					
						
							|  |  |  |   d = PyModule_GetDict(m); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											1997-01-06 22:57:52 +00:00
										 |  |  |   /* Export C API */ | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  |   Itype.ob_type=&PyType_Type; | 
					
						
							|  |  |  |   Otype.ob_type=&PyType_Type; | 
					
						
							| 
									
										
										
										
											1997-09-03 18:19:40 +00:00
										 |  |  |   PyDict_SetItemString(d,"cStringIO_CAPI", | 
					
						
							|  |  |  | 		       v = PyCObject_FromVoidPtr(&CAPI,NULL)); | 
					
						
							|  |  |  |   Py_XDECREF(v); | 
					
						
							| 
									
										
										
										
											1997-04-09 17:35:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Export Types */ | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  |   PyDict_SetItemString(d,"InputType",  (PyObject*)&Itype); | 
					
						
							|  |  |  |   PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); | 
					
						
							| 
									
										
										
										
											1997-08-13 03:14:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Maybe make certain warnings go away */ | 
					
						
							| 
									
										
										
										
											2000-10-06 19:24:23 +00:00
										 |  |  |   if (0) PycString_IMPORT; | 
					
						
							| 
									
										
										
										
											1996-12-05 23:30:48 +00:00
										 |  |  | } |