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