Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | #include "import.h" |
| 4 | #include "cStringIO.h" |
| 5 | |
| 6 | PyDoc_STRVAR(cStringIO_module_documentation, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 7 | "A simple fast partial StringIO replacement.\n" |
| 8 | "\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" |
Fred Drake | aef1000 | 2000-06-19 13:17:41 +0000 | [diff] [blame] | 11 | "full generality of StringIO, but it provides enough for most\n" |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 12 | "applications and is especially useful in conjunction with the\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 13 | "pickle module.\n" |
| 14 | "\n" |
| 15 | "Usage:\n" |
| 16 | "\n" |
| 17 | " from cStringIO import StringIO\n" |
| 18 | "\n" |
| 19 | " an_output_stream=StringIO()\n" |
| 20 | " an_output_stream.write(some_stuff)\n" |
| 21 | " ...\n" |
Jeremy Hylton | b189b07 | 2002-03-08 17:17:33 +0000 | [diff] [blame] | 22 | " value=an_output_stream.getvalue()\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 23 | "\n" |
| 24 | " an_input_stream=StringIO(a_string)\n" |
| 25 | " spam=an_input_stream.readline()\n" |
| 26 | " spam=an_input_stream.read(5)\n" |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 27 | " an_input_stream.seek(0) # OK, start over\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 28 | " spam=an_input_stream.read() # and read it all\n" |
| 29 | " \n" |
| 30 | "If someone else wants to provide a more complete implementation,\n" |
| 31 | "go for it. :-) \n" |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 32 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 33 | "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 34 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 35 | #define UNLESS(E) if (!(E)) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 37 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 38 | /* Declaration for file-like objects that manage data as strings |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 39 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 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. |
| 44 | */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 45 | |
| 46 | typedef struct { |
| 47 | PyObject_HEAD |
| 48 | char *buf; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 49 | int pos, string_size; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 50 | } IOobject; |
| 51 | |
| 52 | #define IOOOBJECT(O) ((IOobject*)(O)) |
| 53 | |
| 54 | /* Declarations for objects of type StringO */ |
| 55 | |
| 56 | typedef struct { /* Subtype of IOobject */ |
| 57 | PyObject_HEAD |
| 58 | char *buf; |
| 59 | int pos, string_size; |
| 60 | |
| 61 | int buf_size, softspace; |
| 62 | } Oobject; |
| 63 | |
| 64 | /* Declarations for objects of type StringI */ |
| 65 | |
| 66 | typedef struct { /* Subtype of IOobject */ |
| 67 | PyObject_HEAD |
| 68 | char *buf; |
| 69 | int pos, string_size; |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 70 | /* We store a reference to the object here in order to keep |
| 71 | the buffer alive during the lifetime of the Iobject. */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 72 | PyObject *pbuf; |
| 73 | } Iobject; |
| 74 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 75 | /* IOobject (common) methods */ |
| 76 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 77 | PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing."); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 78 | |
| 79 | static int |
| 80 | IO__opencheck(IOobject *self) { |
| 81 | UNLESS (self->buf) { |
| 82 | PyErr_SetString(PyExc_ValueError, |
| 83 | "I/O operation on closed file"); |
| 84 | return 0; |
| 85 | } |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 90 | IO_flush(IOobject *self, PyObject *unused) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 91 | |
| 92 | UNLESS (IO__opencheck(self)) return NULL; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 93 | |
| 94 | Py_INCREF(Py_None); |
| 95 | return Py_None; |
| 96 | } |
| 97 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 98 | PyDoc_STRVAR(IO_getval__doc__, |
| 99 | "getvalue([use_pos]) -- Get the string value." |
| 100 | "\n" |
| 101 | "If use_pos is specified and is a true value, then the string returned\n" |
| 102 | "will include only the text up to the current file position.\n"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 103 | |
| 104 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 105 | IO_cgetval(PyObject *self) { |
| 106 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 107 | return PyString_FromStringAndSize(((IOobject*)self)->buf, |
| 108 | ((IOobject*)self)->pos); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 111 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 112 | IO_getval(IOobject *self, PyObject *args) { |
| 113 | PyObject *use_pos=Py_None; |
| 114 | int s; |
| 115 | |
| 116 | UNLESS (IO__opencheck(self)) return NULL; |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 117 | UNLESS (PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 118 | |
| 119 | if (PyObject_IsTrue(use_pos)) { |
| 120 | s=self->pos; |
| 121 | if (s > self->string_size) s=self->string_size; |
| 122 | } |
| 123 | else |
| 124 | s=self->string_size; |
| 125 | return PyString_FromStringAndSize(self->buf, s); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 128 | PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 129 | |
| 130 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 131 | IO_isatty(IOobject *self, PyObject *unused) { |
Guido van Rossum | 674deb2 | 2002-09-01 15:06:28 +0000 | [diff] [blame] | 132 | Py_INCREF(Py_False); |
| 133 | return Py_False; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 136 | PyDoc_STRVAR(IO_read__doc__, |
| 137 | "read([s]) -- Read s characters, or the rest of the string"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 138 | |
| 139 | static int |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 140 | IO_cread(PyObject *self, char **output, int n) { |
| 141 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 142 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 143 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
| 144 | l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos; |
| 145 | if (n < 0 || n > l) { |
| 146 | n = l; |
| 147 | if (n < 0) n=0; |
| 148 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 149 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 150 | *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
| 151 | ((IOobject*)self)->pos += n; |
| 152 | return n; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 156 | IO_read(IOobject *self, PyObject *args) { |
| 157 | int n = -1; |
| 158 | char *output; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 159 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 160 | UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 161 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 162 | if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 163 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 164 | return PyString_FromStringAndSize(output, n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 168 | |
| 169 | static int |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 170 | IO_creadline(PyObject *self, char **output) { |
| 171 | char *n, *s; |
| 172 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 173 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 174 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
| 175 | |
| 176 | for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos, |
| 177 | s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size; |
| 178 | n < s && *n != '\n'; n++); |
| 179 | if (n < s) n++; |
| 180 | |
| 181 | *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
| 182 | l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos; |
| 183 | ((IOobject*)self)->pos += l; |
| 184 | return l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 188 | IO_readline(IOobject *self, PyObject *args) { |
| 189 | int n, m=-1; |
| 190 | char *output; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 191 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 192 | UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL; |
| 193 | |
| 194 | if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL; |
| 195 | if (m >= 0 && m < n) { |
| 196 | m = n - m; |
| 197 | n -= m; |
| 198 | self->pos -= m; |
| 199 | } |
| 200 | return PyString_FromStringAndSize(output, n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 203 | PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines"); |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 204 | |
| 205 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 206 | IO_readlines(IOobject *self, PyObject *args) { |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 207 | int n; |
| 208 | char *output; |
| 209 | PyObject *result, *line; |
| 210 | int hint = 0, length = 0; |
| 211 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 212 | UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL; |
| 213 | |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 214 | result = PyList_New(0); |
| 215 | if (!result) |
| 216 | return NULL; |
| 217 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 218 | while (1){ |
| 219 | if ( (n = IO_creadline((PyObject*)self,&output)) < 0) |
| 220 | goto err; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 221 | if (n == 0) |
| 222 | break; |
| 223 | line = PyString_FromStringAndSize (output, n); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 224 | if (!line) |
| 225 | goto err; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 226 | PyList_Append (result, line); |
| 227 | Py_DECREF (line); |
| 228 | length += n; |
| 229 | if (hint > 0 && length >= hint) |
| 230 | break; |
| 231 | } |
| 232 | return result; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 233 | err: |
| 234 | Py_DECREF(result); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 238 | PyDoc_STRVAR(IO_reset__doc__, |
| 239 | "reset() -- Reset the file position to the beginning"); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 240 | |
| 241 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 242 | IO_reset(IOobject *self, PyObject *unused) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 243 | |
| 244 | UNLESS (IO__opencheck(self)) return NULL; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 245 | |
| 246 | self->pos = 0; |
| 247 | |
| 248 | Py_INCREF(Py_None); |
| 249 | return Py_None; |
| 250 | } |
| 251 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 252 | PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position."); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 253 | |
| 254 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 255 | IO_tell(IOobject *self, PyObject *unused) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 256 | |
| 257 | UNLESS (IO__opencheck(self)) return NULL; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 258 | |
| 259 | return PyInt_FromLong(self->pos); |
| 260 | } |
| 261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 262 | PyDoc_STRVAR(IO_truncate__doc__, |
| 263 | "truncate(): truncate the file at the current position."); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 264 | |
| 265 | static PyObject * |
| 266 | IO_truncate(IOobject *self, PyObject *args) { |
| 267 | int pos = -1; |
| 268 | |
| 269 | UNLESS (IO__opencheck(self)) return NULL; |
| 270 | UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL; |
| 271 | if (pos < 0) pos = self->pos; |
| 272 | |
| 273 | if (self->string_size > pos) self->string_size = pos; |
| 274 | |
| 275 | Py_INCREF(Py_None); |
| 276 | return Py_None; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | /* Read-write object methods */ |
| 283 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 284 | PyDoc_STRVAR(O_seek__doc__, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 285 | "seek(position) -- set the current position\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 286 | "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 287 | |
| 288 | static PyObject * |
| 289 | O_seek(Oobject *self, PyObject *args) { |
| 290 | int position, mode = 0; |
| 291 | |
| 292 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 293 | UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
| 294 | return NULL; |
| 295 | |
| 296 | if (mode == 2) { |
| 297 | position += self->string_size; |
| 298 | } |
| 299 | else if (mode == 1) { |
| 300 | position += self->pos; |
| 301 | } |
| 302 | |
| 303 | if (position > self->buf_size) { |
| 304 | self->buf_size*=2; |
| 305 | if (self->buf_size <= position) self->buf_size=position+1; |
| 306 | UNLESS (self->buf=(char*) |
| 307 | realloc(self->buf,self->buf_size*sizeof(char))) { |
| 308 | self->buf_size=self->pos=0; |
| 309 | return PyErr_NoMemory(); |
| 310 | } |
| 311 | } |
| 312 | else if (position < 0) position=0; |
| 313 | |
| 314 | self->pos=position; |
| 315 | |
| 316 | while (--position >= self->string_size) self->buf[position]=0; |
| 317 | |
| 318 | Py_INCREF(Py_None); |
| 319 | return Py_None; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 322 | PyDoc_STRVAR(O_write__doc__, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 323 | "write(s) -- Write a string to the file" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 324 | "\n\nNote (hack:) writing None resets the buffer"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 328 | O_cwrite(PyObject *self, char *c, int l) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 329 | int newl; |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 330 | Oobject *oself; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 331 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 332 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 333 | oself = (Oobject *)self; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 335 | newl = oself->pos+l; |
| 336 | if (newl >= oself->buf_size) { |
| 337 | oself->buf_size *= 2; |
| 338 | if (oself->buf_size <= newl) |
| 339 | oself->buf_size = newl+1; |
| 340 | UNLESS (oself->buf = |
| 341 | (char*)realloc(oself->buf, |
| 342 | (oself->buf_size) * sizeof(char))) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 343 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 344 | oself->buf_size = oself->pos = 0; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 349 | memcpy(oself->buf+oself->pos,c,l); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 351 | oself->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | 2f09812 | 2001-12-07 20:20:28 +0000 | [diff] [blame] | 353 | if (oself->string_size < oself->pos) { |
| 354 | oself->string_size = oself->pos; |
| 355 | } |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 356 | |
| 357 | return l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 361 | O_write(Oobject *self, PyObject *args) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 362 | char *c; |
| 363 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | a883a3d | 2002-04-29 13:54:48 +0000 | [diff] [blame] | 365 | UNLESS (PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 366 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 367 | if (O_cwrite((PyObject*)self,c,l) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 368 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 369 | Py_INCREF(Py_None); |
| 370 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 373 | PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held."); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 374 | |
| 375 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 376 | O_close(Oobject *self, PyObject *unused) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 377 | if (self->buf != NULL) free(self->buf); |
| 378 | self->buf = NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 379 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 380 | self->pos = self->string_size = self->buf_size = 0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 381 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 382 | Py_INCREF(Py_None); |
| 383 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 387 | PyDoc_STRVAR(O_writelines__doc__, |
| 388 | "writelines(sequence_of_strings): write each string"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 389 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 390 | O_writelines(Oobject *self, PyObject *args) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 391 | PyObject *tmp = 0; |
Jeremy Hylton | cafd495 | 2001-02-09 23:44:22 +0000 | [diff] [blame] | 392 | static PyObject *joiner = NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 393 | |
Jeremy Hylton | cafd495 | 2001-02-09 23:44:22 +0000 | [diff] [blame] | 394 | if (!joiner) { |
| 395 | PyObject *empty_string = PyString_FromString(""); |
| 396 | if (empty_string == NULL) |
| 397 | return NULL; |
| 398 | joiner = PyObject_GetAttrString(empty_string, "join"); |
| 399 | Py_DECREF(empty_string); |
| 400 | if (joiner == NULL) |
| 401 | return NULL; |
| 402 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 403 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 404 | if (PyObject_Size(args) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 405 | |
Jeremy Hylton | cafd495 | 2001-02-09 23:44:22 +0000 | [diff] [blame] | 406 | tmp = PyObject_CallFunction(joiner, "O", args); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 407 | UNLESS (tmp) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 408 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 409 | args = Py_BuildValue("(O)", tmp); |
| 410 | Py_DECREF(tmp); |
| 411 | UNLESS (args) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 412 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 413 | tmp = O_write(self, args); |
| 414 | Py_DECREF(args); |
| 415 | return tmp; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static struct PyMethodDef O_methods[] = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 419 | /* Common methods: */ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 420 | {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 421 | {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 422 | {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 423 | {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
| 424 | {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
| 425 | {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 426 | {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__}, |
| 427 | {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 428 | {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
| 429 | |
| 430 | /* Read-write StringIO specific methods: */ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 431 | {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 432 | {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__}, |
| 433 | {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 434 | {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__}, |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 435 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 438 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 439 | O_dealloc(Oobject *self) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 440 | if (self->buf != NULL) |
| 441 | free(self->buf); |
| 442 | PyObject_Del(self); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 446 | O_getattr(Oobject *self, char *name) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 447 | if (strcmp(name, "softspace") == 0) { |
| 448 | return PyInt_FromLong(self->softspace); |
| 449 | } |
| 450 | return Py_FindMethod(O_methods, (PyObject *)self, name); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 453 | static int |
| 454 | O_setattr(Oobject *self, char *name, PyObject *value) { |
| 455 | long x; |
| 456 | if (strcmp(name, "softspace") != 0) { |
| 457 | PyErr_SetString(PyExc_AttributeError, name); |
| 458 | return -1; |
| 459 | } |
| 460 | x = PyInt_AsLong(value); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 461 | if (x < 0 && PyErr_Occurred()) |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 462 | return -1; |
| 463 | self->softspace = x; |
| 464 | return 0; |
| 465 | } |
| 466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 467 | PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings."); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 468 | |
| 469 | static PyTypeObject Otype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 470 | PyObject_HEAD_INIT(NULL) |
| 471 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 472 | "cStringIO.StringO", /*tp_name*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 473 | sizeof(Oobject), /*tp_basicsize*/ |
| 474 | 0, /*tp_itemsize*/ |
| 475 | /* methods */ |
| 476 | (destructor)O_dealloc, /*tp_dealloc*/ |
| 477 | (printfunc)0, /*tp_print*/ |
| 478 | (getattrfunc)O_getattr, /*tp_getattr*/ |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 479 | (setattrfunc)O_setattr, /*tp_setattr*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 480 | (cmpfunc)0, /*tp_compare*/ |
| 481 | (reprfunc)0, /*tp_repr*/ |
| 482 | 0, /*tp_as_number*/ |
| 483 | 0, /*tp_as_sequence*/ |
| 484 | 0, /*tp_as_mapping*/ |
| 485 | (hashfunc)0, /*tp_hash*/ |
| 486 | (ternaryfunc)0, /*tp_call*/ |
| 487 | (reprfunc)0, /*tp_str*/ |
| 488 | |
| 489 | /* Space for future expansion */ |
| 490 | 0L,0L,0L,0L, |
| 491 | Otype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 492 | }; |
| 493 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 494 | static PyObject * |
| 495 | newOobject(int size) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 496 | Oobject *self; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 497 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 498 | self = PyObject_New(Oobject, &Otype); |
| 499 | if (self == NULL) |
| 500 | return NULL; |
| 501 | self->pos=0; |
| 502 | self->string_size = 0; |
| 503 | self->softspace = 0; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 504 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 505 | UNLESS (self->buf=malloc(size*sizeof(char))) { |
| 506 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 507 | self->buf_size = 0; |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | self->buf_size=size; |
| 512 | return (PyObject*)self; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 515 | /* End of code for StringO objects */ |
| 516 | /* -------------------------------------------------------- */ |
| 517 | |
| 518 | static PyObject * |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 519 | I_close(Iobject *self, PyObject *unused) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 520 | Py_XDECREF(self->pbuf); |
| 521 | self->pbuf = NULL; |
| 522 | self->buf = NULL; |
| 523 | |
| 524 | self->pos = self->string_size = 0; |
| 525 | |
| 526 | Py_INCREF(Py_None); |
| 527 | return Py_None; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 531 | I_seek(Iobject *self, PyObject *args) { |
| 532 | int position, mode = 0; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 533 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 534 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 535 | UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
| 536 | return NULL; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 537 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 538 | if (mode == 2) position += self->string_size; |
| 539 | else if (mode == 1) position += self->pos; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 540 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 541 | if (position < 0) position=0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 542 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 543 | self->pos=position; |
| 544 | |
| 545 | Py_INCREF(Py_None); |
| 546 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static struct PyMethodDef I_methods[] = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 550 | /* Common methods: */ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 551 | {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 552 | {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 553 | {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 554 | {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
| 555 | {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
| 556 | {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 557 | {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__}, |
| 558 | {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 559 | {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
| 560 | |
| 561 | /* Read-only StringIO specific methods: */ |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 562 | {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 563 | {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__}, |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 564 | {NULL, NULL} |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 567 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 568 | I_dealloc(Iobject *self) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 569 | Py_XDECREF(self->pbuf); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 570 | PyObject_Del(self); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 574 | I_getattr(Iobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 575 | return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 576 | } |
| 577 | |
Barry Warsaw | 3e8be72 | 2001-09-22 04:36:49 +0000 | [diff] [blame] | 578 | static PyObject * |
| 579 | I_getiter(Iobject *self) |
| 580 | { |
| 581 | PyObject *myreadline = PyObject_GetAttrString((PyObject*)self, |
| 582 | "readline"); |
| 583 | PyObject *emptystring = PyString_FromString(""); |
| 584 | PyObject *iter = NULL; |
| 585 | if (!myreadline || !emptystring) |
| 586 | goto finally; |
| 587 | |
| 588 | iter = PyCallIter_New(myreadline, emptystring); |
| 589 | finally: |
| 590 | Py_XDECREF(myreadline); |
| 591 | Py_XDECREF(emptystring); |
| 592 | return iter; |
| 593 | } |
| 594 | |
| 595 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 596 | PyDoc_STRVAR(Itype__doc__, |
| 597 | "Simple type for treating strings as input file streams"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 598 | |
| 599 | static PyTypeObject Itype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 600 | PyObject_HEAD_INIT(NULL) |
Barry Warsaw | 3e8be72 | 2001-09-22 04:36:49 +0000 | [diff] [blame] | 601 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 602 | "cStringIO.StringI", /*tp_name*/ |
Barry Warsaw | 3e8be72 | 2001-09-22 04:36:49 +0000 | [diff] [blame] | 603 | sizeof(Iobject), /*tp_basicsize*/ |
| 604 | 0, /*tp_itemsize*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 605 | /* methods */ |
Barry Warsaw | 3e8be72 | 2001-09-22 04:36:49 +0000 | [diff] [blame] | 606 | (destructor)I_dealloc, /*tp_dealloc*/ |
| 607 | (printfunc)0, /*tp_print*/ |
| 608 | (getattrfunc)I_getattr, /*tp_getattr*/ |
| 609 | (setattrfunc)0, /*tp_setattr*/ |
| 610 | (cmpfunc)0, /*tp_compare*/ |
| 611 | (reprfunc)0, /*tp_repr*/ |
| 612 | 0, /*tp_as_number*/ |
| 613 | 0, /*tp_as_sequence*/ |
| 614 | 0, /*tp_as_mapping*/ |
| 615 | (hashfunc)0, /*tp_hash*/ |
| 616 | (ternaryfunc)0, /*tp_call*/ |
| 617 | (reprfunc)0, /*tp_str*/ |
| 618 | 0, /* tp_getattro */ |
| 619 | 0, /* tp_setattro */ |
| 620 | 0, /* tp_as_buffer */ |
| 621 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 622 | Itype__doc__, /* tp_doc */ |
| 623 | 0, /* tp_traverse */ |
| 624 | 0, /* tp_clear */ |
| 625 | 0, /* tp_richcompare */ |
| 626 | 0, /* tp_weaklistoffset */ |
| 627 | (getiterfunc)I_getiter, /* tp_iter */ |
| 628 | 0, /* tp_iternext */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 629 | }; |
| 630 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 631 | static PyObject * |
| 632 | newIobject(PyObject *s) { |
| 633 | Iobject *self; |
| 634 | char *buf; |
| 635 | int size; |
Jeremy Hylton | 127b2ef | 2000-04-12 22:04:01 +0000 | [diff] [blame] | 636 | |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 637 | if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) { |
| 638 | PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found", |
Jeremy Hylton | 127b2ef | 2000-04-12 22:04:01 +0000 | [diff] [blame] | 639 | s->ob_type->tp_name); |
| 640 | return NULL; |
| 641 | } |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 642 | UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 643 | Py_INCREF(s); |
| 644 | self->buf=buf; |
| 645 | self->string_size=size; |
| 646 | self->pbuf=s; |
| 647 | self->pos=0; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 648 | |
| 649 | return (PyObject*)self; |
| 650 | } |
| 651 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 652 | /* End of code for StringI objects */ |
| 653 | /* -------------------------------------------------------- */ |
| 654 | |
| 655 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 656 | PyDoc_STRVAR(IO_StringIO__doc__, |
| 657 | "StringIO([s]) -- Return a StringIO-like stream for reading or writing"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 658 | |
| 659 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 660 | IO_StringIO(PyObject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 661 | PyObject *s=0; |
| 662 | |
Raymond Hettinger | 36cd2bf | 2003-01-03 08:24:58 +0000 | [diff] [blame] | 663 | if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 664 | |
| 665 | if (s) return newIobject(s); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 666 | return newOobject(128); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* List of methods defined in the module */ |
| 670 | |
| 671 | static struct PyMethodDef IO_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 672 | {"StringIO", (PyCFunction)IO_StringIO, |
| 673 | METH_VARARGS, IO_StringIO__doc__}, |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 674 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | |
| 678 | /* Initialization function for the module (*must* be called initcStringIO) */ |
| 679 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 680 | static struct PycStringIO_CAPI CAPI = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 681 | IO_cread, |
| 682 | IO_creadline, |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 683 | O_cwrite, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 684 | IO_cgetval, |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 685 | newOobject, |
| 686 | newIobject, |
| 687 | &Itype, |
| 688 | &Otype, |
| 689 | }; |
| 690 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 691 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 692 | #define PyMODINIT_FUNC void |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 693 | #endif |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 694 | PyMODINIT_FUNC |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 695 | initcStringIO(void) { |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 696 | PyObject *m, *d, *v; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 697 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 698 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 699 | /* Create the module and add the functions */ |
| 700 | m = Py_InitModule4("cStringIO", IO_methods, |
| 701 | cStringIO_module_documentation, |
| 702 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 703 | |
| 704 | /* Add some symbolic constants to the module */ |
| 705 | d = PyModule_GetDict(m); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 706 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 707 | /* Export C API */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 708 | Itype.ob_type=&PyType_Type; |
| 709 | Otype.ob_type=&PyType_Type; |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 710 | PyDict_SetItemString(d,"cStringIO_CAPI", |
| 711 | v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
| 712 | Py_XDECREF(v); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 713 | |
| 714 | /* Export Types */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 715 | PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 716 | PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 717 | |
| 718 | /* Maybe make certain warnings go away */ |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 719 | if (0) PycString_IMPORT; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 720 | } |