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