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