Mercurial > cpython-lru_cache-weakref
annotate Modules/cStringIO.c @ 4963:5ded2bc1908b legacy-trunk
"ported" Guido's embedding demo to the mac, and wrote some minimal
notes on embedding on the mac.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Wed, 15 Jan 1997 16:53:37 +0000 |
| parents | 9d58d0e9877e |
| children | e8f8cf358054 |
| rev | line source |
|---|---|
| 4598 | 1 /* |
| 2 | |
| 3 $Id$ | |
| 4 | |
| 5 A simple fast partial StringIO replacement. | |
| 6 | |
| 7 | |
| 8 | |
| 9 Copyright | |
| 10 | |
| 11 Copyright 1996 Digital Creations, L.C., 910 Princess Anne | |
| 12 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All | |
| 13 rights reserved. Copyright in this software is owned by DCLC, | |
| 14 unless otherwise indicated. Permission to use, copy and | |
| 15 distribute this software is hereby granted, provided that the | |
| 16 above copyright notice appear in all copies and that both that | |
| 17 copyright notice and this permission notice appear. Note that | |
| 18 any product, process or technology described in this software | |
| 19 may be the subject of other Intellectual Property rights | |
| 20 reserved by Digital Creations, L.C. and are not licensed | |
| 21 hereunder. | |
| 22 | |
| 23 Trademarks | |
| 24 | |
| 25 Digital Creations & DCLC, are trademarks of Digital Creations, L.C.. | |
| 26 All other trademarks are owned by their respective companies. | |
| 27 | |
| 28 No Warranty | |
| 29 | |
| 30 The software is provided "as is" without warranty of any kind, | |
| 31 either express or implied, including, but not limited to, the | |
| 32 implied warranties of merchantability, fitness for a particular | |
| 33 purpose, or non-infringement. This software could include | |
| 34 technical inaccuracies or typographical errors. Changes are | |
| 35 periodically made to the software; these changes will be | |
| 36 incorporated in new editions of the software. DCLC may make | |
| 37 improvements and/or changes in this software at any time | |
| 38 without notice. | |
| 39 | |
| 40 Limitation Of Liability | |
| 41 | |
| 42 In no event will DCLC be liable for direct, indirect, special, | |
| 43 incidental, economic, cover, or consequential damages arising | |
| 44 out of the use of or inability to use this software even if | |
| 45 advised of the possibility of such damages. Some states do not | |
| 46 allow the exclusion or limitation of implied warranties or | |
| 47 limitation of liability for incidental or consequential | |
| 48 damages, so the above limitation or exclusion may not apply to | |
| 49 you. | |
| 50 | |
| 51 If you have questions regarding this software, | |
| 52 contact: | |
| 53 | |
| 54 Jim Fulton, jim@digicool.com | |
| 55 Digital Creations L.C. | |
| 56 | |
| 57 (540) 371-6909 | |
| 58 | |
| 59 | |
| 60 $Log$ | |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
61 Revision 2.3 1997/01/14 17:38:28 bwarsaw |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
62 Quieted gcc -Wall by removing unused local variables. |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
63 |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
64 Suppressing my urge to reformat according to Python coding standards! |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
65 :-) |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
66 |
| 4906 | 67 Revision 2.2 1997/01/06 22:57:52 guido |
| 68 Jim's latest version. | |
| 69 | |
| 70 Revision 1.10 1997/01/02 15:19:55 chris | |
| 71 checked in to be sure repository is up to date. | |
| 72 | |
| 73 Revision 1.9 1996/12/27 21:40:29 jim | |
| 74 Took out some lamosities in interface, like returning self from | |
| 75 write. | |
| 76 | |
| 77 Revision 1.8 1996/12/23 15:52:49 jim | |
| 78 Added ifdef to check for CObject before using it. | |
| 79 | |
| 80 Revision 1.7 1996/12/23 15:22:35 jim | |
| 81 Finished implementation, adding full compatibility with StringIO, and | |
| 82 then some. | |
| 83 | |
| 84 We still need to take out some cStringIO oddities. | |
| 4598 | 85 |
| 86 Revision 1.6 1996/10/15 18:42:07 jim | |
| 87 Added lots of casts to make warnings go away. | |
| 88 | |
| 89 Revision 1.5 1996/10/11 21:03:42 jim | |
| 90 *** empty log message *** | |
| 91 | |
| 92 Revision 1.4 1996/10/11 21:02:15 jim | |
| 93 *** empty log message *** | |
| 94 | |
| 95 Revision 1.3 1996/10/07 20:51:38 chris | |
| 96 *** empty log message *** | |
| 97 | |
| 98 Revision 1.2 1996/07/18 13:08:34 jfulton | |
| 99 *** empty log message *** | |
| 100 | |
| 101 Revision 1.1 1996/07/15 17:06:33 jfulton | |
| 102 Initial version. | |
| 103 | |
| 104 | |
| 105 */ | |
| 106 static char cStringIO_module_documentation[] = | |
| 107 "A simple fast partial StringIO replacement.\n" | |
| 108 "\n" | |
| 109 "This module provides a simple useful replacement for\n" | |
| 110 "the StringIO module that is written in C. It does not provide the\n" | |
| 111 "full generality if StringIO, but it provides anough for most\n" | |
| 112 "applications and is especially useful in conjuction with the\n" | |
| 113 "pickle module.\n" | |
| 114 "\n" | |
| 115 "Usage:\n" | |
| 116 "\n" | |
| 117 " from cStringIO import StringIO\n" | |
| 118 "\n" | |
| 119 " an_output_stream=StringIO()\n" | |
| 120 " an_output_stream.write(some_stuff)\n" | |
| 121 " ...\n" | |
| 122 " value=an_output_stream.getvalue() # str(an_output_stream) works too!\n" | |
| 123 "\n" | |
| 124 " an_input_stream=StringIO(a_string)\n" | |
| 125 " spam=an_input_stream.readline()\n" | |
| 126 " spam=an_input_stream.read(5)\n" | |
| 127 " an_input_stream.reset() # OK, start over, note no seek yet\n" | |
| 128 " spam=an_input_stream.read() # and read it all\n" | |
| 129 " \n" | |
| 130 "If someone else wants to provide a more complete implementation,\n" | |
| 131 "go for it. :-) \n" | |
| 132 ; | |
| 133 | |
| 134 #include "Python.h" | |
| 135 #include "import.h" | |
| 136 | |
| 137 static PyObject *ErrorObject; | |
| 138 | |
| 4906 | 139 #ifdef __cplusplus |
| 140 #define ARG(T,N) T N | |
| 141 #define ARGDECL(T,N) | |
| 142 #else | |
| 143 #define ARG(T,N) N | |
| 144 #define ARGDECL(T,N) T N; | |
| 145 #endif | |
| 146 | |
| 4598 | 147 #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} |
| 148 #define UNLESS(E) if(!(E)) | |
| 149 #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V) | |
| 150 #define Py_ASSIGN(P,E) if(!PyObject_AssignExpression(&(P),(E))) return NULL | |
| 151 | |
| 152 /* ----------------------------------------------------- */ | |
| 153 | |
| 154 /* Declarations for objects of type StringO */ | |
| 155 | |
| 156 typedef struct { | |
| 157 PyObject_HEAD | |
| 158 char *buf; | |
| 159 int pos, string_size, buf_size, closed; | |
| 160 } Oobject; | |
| 161 | |
| 162 staticforward PyTypeObject Otype; | |
| 163 | |
| 164 /* ---------------------------------------------------------------- */ | |
| 165 | |
| 166 /* Declarations for objects of type StringI */ | |
| 167 | |
| 168 typedef struct { | |
| 169 PyObject_HEAD | |
| 170 char *buf; | |
| 171 int pos, string_size, closed; | |
| 172 PyObject *pbuf; | |
| 173 } Iobject; | |
| 174 | |
| 175 staticforward PyTypeObject Itype; | |
| 176 | |
| 177 | |
| 178 | |
| 179 /* ---------------------------------------------------------------- */ | |
| 180 | |
| 181 static char O_reset__doc__[] = | |
| 182 "reset() -- Reset the file position to the beginning" | |
| 183 ; | |
| 184 | |
| 185 static PyObject * | |
| 4906 | 186 O_reset(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 187 ARGDECL(Oobject*, self) | |
| 188 ARGDECL(PyObject*, args) | |
| 4598 | 189 { |
| 190 self->pos = 0; | |
| 191 | |
| 192 Py_INCREF(Py_None); | |
| 193 return Py_None; | |
| 194 } | |
| 195 | |
| 196 | |
| 197 static char O_tell__doc__[] = | |
| 198 "tell() -- get the current position."; | |
| 199 | |
| 200 static PyObject * | |
| 4906 | 201 O_tell(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 202 ARGDECL(Oobject*, self) | |
| 203 ARGDECL(PyObject*, args) | |
| 4598 | 204 { |
| 205 return PyInt_FromLong(self->pos); | |
| 206 } | |
| 207 | |
| 208 | |
| 209 static char O_seek__doc__[] = | |
| 210 "seek(position) -- set the current position\n" | |
| 211 "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; | |
| 212 | |
| 213 static PyObject * | |
| 4906 | 214 O_seek(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 215 ARGDECL(Oobject*, self) | |
| 216 ARGDECL(PyObject*, args) | |
| 4598 | 217 { |
| 218 int position, mode = 0; | |
| 219 | |
| 220 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) | |
| 221 { | |
| 222 return NULL; | |
| 223 } | |
| 224 | |
| 225 if (mode == 2) | |
| 226 { | |
| 227 position += self->string_size; | |
| 228 } | |
| 229 else if (mode == 1) | |
| 230 { | |
| 231 position += self->pos; | |
| 232 } | |
| 233 | |
| 234 self->pos = (position > self->string_size ? self->string_size : | |
| 235 (position < 0 ? 0 : position)); | |
| 236 | |
| 4906 | 237 Py_INCREF(Py_None); |
| 238 return Py_None; | |
| 4598 | 239 } |
| 240 | |
| 241 static char O_read__doc__[] = | |
| 242 "read([s]) -- Read s characters, or the rest of the string" | |
| 243 ; | |
| 244 | |
| 245 static int | |
| 4906 | 246 O_cread(ARG(Oobject*, self), ARG(char**, output), ARG(int, n)) |
| 247 ARGDECL(Oobject*, self) | |
| 248 ARGDECL(char**, output) | |
| 249 ARGDECL(int, n) | |
| 4598 | 250 { |
| 251 int l; | |
| 252 | |
| 253 l = self->string_size - self->pos; | |
| 254 if (n < 0 || n > l) | |
| 255 { | |
| 256 n = l; | |
| 257 } | |
| 258 | |
| 259 *output=self->buf + self->pos; | |
| 260 self->pos += n; | |
| 261 return n; | |
| 262 } | |
| 263 | |
| 264 static PyObject * | |
| 4906 | 265 O_read(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 266 ARGDECL(Oobject*, self) | |
| 267 ARGDECL(PyObject*, args) | |
| 4598 | 268 { |
| 269 int n = -1; | |
| 270 char *output; | |
| 271 | |
| 272 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; | |
| 273 | |
| 274 n=O_cread(self,&output,n); | |
| 275 | |
| 276 return PyString_FromStringAndSize(output, n); | |
| 277 } | |
| 278 | |
| 279 | |
| 280 static char O_readline__doc__[] = | |
| 281 "readline() -- Read one line" | |
| 282 ; | |
| 283 | |
| 284 static int | |
| 4906 | 285 O_creadline(ARG(Oobject*, self), ARG(char**, output)) |
| 286 ARGDECL(Oobject*, self) | |
| 287 ARGDECL(char**, output) | |
| 4598 | 288 { |
| 289 char *n, *s; | |
| 290 int l; | |
| 291 | |
| 292 for (n = self->buf + self->pos, s = self->buf + self->string_size; | |
| 293 n < s && *n != '\n'; n++); | |
| 294 if (n < s) n++; | |
| 295 | |
| 296 *output=self->buf + self->pos; | |
| 297 l = n - self->buf - self->pos; | |
| 298 self->pos += l; | |
| 299 return l; | |
| 300 } | |
| 301 | |
| 302 static PyObject * | |
| 4906 | 303 O_readline(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 304 ARGDECL(Oobject*, self) | |
| 305 ARGDECL(PyObject*, args) | |
| 4598 | 306 { |
| 307 int n; | |
| 308 char *output; | |
| 309 | |
| 310 n=O_creadline(self,&output); | |
| 311 return PyString_FromStringAndSize(output, n); | |
| 312 } | |
| 313 | |
| 314 static char O_write__doc__[] = | |
| 315 "write(s) -- Write a string to the file" | |
| 316 "\n\nNote (hack:) writing None resets the buffer" | |
| 317 ; | |
| 318 | |
| 319 | |
| 320 static int | |
| 4906 | 321 O_cwrite(ARG(Oobject*, self), ARG(char*, c), ARG(int, l)) |
| 322 ARGDECL(Oobject*, self) | |
| 323 ARGDECL(char*, c) | |
| 324 ARGDECL(int, l) | |
| 4598 | 325 { |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
326 int newl; |
| 4906 | 327 |
| 4598 | 328 newl=self->pos+l; |
| 329 if(newl > self->buf_size) | |
| 330 { | |
| 331 self->buf_size*=2; | |
| 332 if(self->buf_size < newl) self->buf_size=newl; | |
| 333 UNLESS(self->buf=(char*)realloc(self->buf, self->buf_size*sizeof(char))) | |
| 334 { | |
| 335 PyErr_SetString(PyExc_MemoryError,"out of memory"); | |
| 336 self->buf_size=self->pos=0; | |
| 337 return -1; | |
| 338 } | |
| 339 } | |
| 340 | |
| 341 memcpy(self->buf+self->pos,c,l); | |
| 342 | |
| 343 self->pos += l; | |
| 344 | |
| 345 if (self->string_size < self->pos) | |
| 346 { | |
| 347 self->string_size = self->pos; | |
| 348 } | |
| 349 | |
| 350 return l; | |
| 351 } | |
| 352 | |
| 353 static PyObject * | |
| 4906 | 354 O_write(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 355 ARGDECL(Oobject*, self) | |
| 356 ARGDECL(PyObject*, args) | |
| 4598 | 357 { |
| 358 PyObject *s; | |
|
4957
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
359 char *c; |
|
9d58d0e9877e
Quieted gcc -Wall by removing unused local variables.
Barry Warsaw <barry@python.org>
parents:
4906
diff
changeset
|
360 int l; |
| 4598 | 361 |
| 362 UNLESS(PyArg_Parse(args, "O", &s)) return NULL; | |
| 4906 | 363 UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 364 UNLESS(c=PyString_AsString(s)) return NULL; | |
| 365 UNLESS(-1 != O_cwrite(self,c,l)) return NULL; | |
| 4598 | 366 |
| 4906 | 367 Py_INCREF(Py_None); |
| 368 return Py_None; | |
| 4598 | 369 } |
| 370 | |
| 371 static PyObject * | |
| 4906 | 372 O_getval(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 373 ARGDECL(Oobject*, self) | |
| 374 ARGDECL(PyObject*, args) | |
| 4598 | 375 { |
| 376 return PyString_FromStringAndSize(self->buf,self->pos); | |
| 377 } | |
| 378 | |
| 379 static char O_truncate__doc__[] = | |
| 380 "truncate(): truncate the file at the current position."; | |
| 381 | |
| 382 static PyObject * | |
| 4906 | 383 O_truncate(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 384 ARGDECL(Oobject*, self) | |
| 385 ARGDECL(PyObject*, args) | |
| 4598 | 386 { |
| 387 self->string_size = self->pos; | |
| 388 Py_INCREF(Py_None); | |
| 389 return Py_None; | |
| 390 } | |
| 391 | |
| 392 static char O_isatty__doc__[] = "isatty(): always returns 0"; | |
| 393 | |
| 394 static PyObject * | |
| 4906 | 395 O_isatty(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 396 ARGDECL(Oobject*, self) | |
| 397 ARGDECL(PyObject*, args) | |
| 4598 | 398 { |
| 399 return PyInt_FromLong(0); | |
| 400 } | |
| 401 | |
| 402 static char O_close__doc__[] = "close(): explicitly release resources held."; | |
| 403 | |
| 404 static PyObject * | |
| 4906 | 405 O_close(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 406 ARGDECL(Oobject*, self) | |
| 407 ARGDECL(PyObject*, args) | |
| 4598 | 408 { |
| 409 free(self->buf); | |
| 410 | |
| 411 self->pos = self->string_size = self->buf_size = 0; | |
| 412 self->closed = 1; | |
| 413 | |
| 414 Py_INCREF(Py_None); | |
| 415 return Py_None; | |
| 416 } | |
| 417 | |
| 418 static char O_flush__doc__[] = "flush(): does nothing."; | |
| 419 | |
| 420 static PyObject * | |
| 4906 | 421 O_flush(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 422 ARGDECL(Oobject*, self) | |
| 423 ARGDECL(PyObject*, args) | |
| 4598 | 424 { |
| 425 Py_INCREF(Py_None); | |
| 426 return Py_None; | |
| 427 } | |
| 428 | |
| 429 | |
| 430 static char O_writelines__doc__[] = "blah"; | |
| 431 static PyObject * | |
| 4906 | 432 O_writelines(ARG(Oobject*, self), ARG(PyObject*, args)) |
| 433 ARGDECL(Oobject*, self) | |
| 434 ARGDECL(PyObject*, args) | |
| 4598 | 435 { |
| 436 PyObject *string_module = 0; | |
| 437 static PyObject *string_joinfields = 0; | |
| 438 | |
| 439 UNLESS(PyArg_Parse(args, "O", args)) | |
| 440 { | |
| 441 return NULL; | |
| 442 } | |
| 443 | |
| 444 if (!string_joinfields) | |
| 445 { | |
| 446 UNLESS(string_module = PyImport_ImportModule("string")) | |
| 447 { | |
| 448 return NULL; | |
| 449 } | |
| 450 | |
| 451 UNLESS(string_joinfields= | |
| 452 PyObject_GetAttrString(string_module, "joinfields")) | |
| 453 { | |
| 454 return NULL; | |
| 455 } | |
| 456 | |
| 457 Py_DECREF(string_module); | |
| 458 } | |
| 459 | |
| 460 if (PyObject_Length(args) == -1) | |
| 461 { | |
| 462 return NULL; | |
| 463 } | |
| 464 | |
| 465 return O_write(self, | |
| 466 PyObject_CallFunction(string_joinfields, "Os", args, "")); | |
| 467 } | |
| 468 | |
| 469 static struct PyMethodDef O_methods[] = { | |
| 470 {"read", (PyCFunction)O_read, 1, O_read__doc__}, | |
| 471 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, | |
| 472 {"write", (PyCFunction)O_write, 0, O_write__doc__}, | |
| 473 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, | |
| 474 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, | |
| 475 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, | |
| 476 {"getvalue", (PyCFunction)O_getval, 0, "getvalue() -- Get the string value"}, | |
| 477 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, | |
| 478 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, | |
| 479 {"close", (PyCFunction)O_close, 0, O_close__doc__}, | |
| 480 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, | |
| 481 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__}, | |
| 482 {NULL, NULL} /* sentinel */ | |
| 483 }; | |
| 484 | |
| 485 /* ---------- */ | |
| 486 | |
| 487 | |
| 488 static Oobject * | |
| 4906 | 489 newOobject(ARG(int, size)) |
| 490 ARGDECL(int, size) | |
| 4598 | 491 { |
| 492 Oobject *self; | |
| 493 | |
| 494 self = PyObject_NEW(Oobject, &Otype); | |
| 495 if (self == NULL) | |
| 496 return NULL; | |
| 497 self->pos=0; | |
| 498 self->closed = 0; | |
| 499 self->string_size = 0; | |
| 500 | |
| 501 UNLESS(self->buf=malloc(size*sizeof(char))) | |
| 502 { | |
| 503 PyErr_SetString(PyExc_MemoryError,"out of memory"); | |
| 504 self->buf_size = 0; | |
| 505 return NULL; | |
| 506 } | |
| 507 | |
| 508 self->buf_size=size; | |
| 509 return self; | |
| 510 } | |
| 511 | |
| 512 | |
| 513 static void | |
| 4906 | 514 O_dealloc(ARG(Oobject*, self)) |
| 515 ARGDECL(Oobject*, self) | |
| 4598 | 516 { |
| 517 free(self->buf); | |
| 518 PyMem_DEL(self); | |
| 519 } | |
| 520 | |
| 521 static PyObject * | |
| 4906 | 522 O_getattr(ARG(Oobject*, self), ARG(char*, name)) |
| 523 ARGDECL(Oobject*, self) | |
| 524 ARGDECL(char*, name) | |
| 4598 | 525 { |
| 526 return Py_FindMethod(O_methods, (PyObject *)self, name); | |
| 527 } | |
| 528 | |
| 529 static char Otype__doc__[] = | |
| 530 "Simple type for output to strings." | |
| 531 ; | |
| 532 | |
| 533 static PyTypeObject Otype = { | |
| 534 PyObject_HEAD_INIT(&PyType_Type) | |
| 4906 | 535 0, /*ob_size*/ |
| 536 "StringO", /*tp_name*/ | |
| 537 sizeof(Oobject), /*tp_basicsize*/ | |
| 538 0, /*tp_itemsize*/ | |
| 4598 | 539 /* methods */ |
| 540 (destructor)O_dealloc, /*tp_dealloc*/ | |
| 541 (printfunc)0, /*tp_print*/ | |
| 542 (getattrfunc)O_getattr, /*tp_getattr*/ | |
| 4906 | 543 (setattrfunc)0, /*tp_setattr*/ |
| 4598 | 544 (cmpfunc)0, /*tp_compare*/ |
| 4906 | 545 (reprfunc)0, /*tp_repr*/ |
| 4598 | 546 0, /*tp_as_number*/ |
| 4906 | 547 0, /*tp_as_sequence*/ |
| 548 0, /*tp_as_mapping*/ | |
| 4598 | 549 (hashfunc)0, /*tp_hash*/ |
| 550 (ternaryfunc)0, /*tp_call*/ | |
| 551 (reprfunc)0, /*tp_str*/ | |
| 552 | |
| 553 /* Space for future expansion */ | |
| 554 0L,0L,0L,0L, | |
| 4906 | 555 Otype__doc__ /* Documentation string */ |
| 4598 | 556 }; |
| 557 | |
| 558 /* End of code for StringO objects */ | |
| 559 /* -------------------------------------------------------- */ | |
| 560 | |
| 561 static PyObject * | |
| 4906 | 562 I_close(ARG(Iobject*, self), ARG(PyObject*, args)) |
| 563 ARGDECL(Iobject*, self) | |
| 564 ARGDECL(PyObject*, args) | |
| 4598 | 565 { |
| 566 Py_DECREF(self->pbuf); | |
| 567 | |
| 568 self->pos = self->string_size = 0; | |
| 569 self->closed = 1; | |
| 570 | |
| 571 Py_INCREF(Py_None); | |
| 572 return Py_None; | |
| 573 } | |
| 574 | |
| 575 static struct PyMethodDef I_methods[] = { | |
| 576 {"read", (PyCFunction)O_read, 1, O_read__doc__}, | |
| 577 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, | |
| 578 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, | |
| 579 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, | |
| 580 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, | |
| 581 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, | |
| 582 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, | |
| 583 {"close", (PyCFunction)I_close, 0, O_close__doc__}, | |
| 584 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, | |
| 585 {NULL, NULL} /* sentinel */ | |
| 586 }; | |
| 587 | |
| 588 /* ---------- */ | |
| 589 | |
| 590 | |
| 591 static Iobject * | |
| 4906 | 592 newIobject(ARG(PyObject*, s)) |
| 593 ARGDECL(PyObject*, s) | |
| 4598 | 594 { |
| 595 Iobject *self; | |
| 596 char *buf; | |
| 597 int size; | |
| 598 | |
| 599 UNLESS(buf=PyString_AsString(s)) return NULL; | |
| 600 UNLESS(-1 != (size=PyString_Size(s))) return NULL; | |
| 601 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; | |
| 602 Py_INCREF(s); | |
| 603 self->buf=buf; | |
| 604 self->string_size=size; | |
| 605 self->pbuf=s; | |
| 606 self->pos=0; | |
| 607 self->closed = 0; | |
| 608 | |
| 609 return self; | |
| 610 } | |
| 611 | |
| 612 | |
| 613 static void | |
| 4906 | 614 I_dealloc(ARG(Iobject*, self)) |
| 615 ARGDECL(Iobject*, self) | |
| 4598 | 616 { |
| 617 Py_DECREF(self->pbuf); | |
| 618 PyMem_DEL(self); | |
| 619 } | |
| 620 | |
| 621 static PyObject * | |
| 4906 | 622 I_getattr(ARG(Iobject*, self), ARG(char*, name)) |
| 623 ARGDECL(Iobject*, self) | |
| 624 ARGDECL(char*, name) | |
| 4598 | 625 { |
| 626 return Py_FindMethod(I_methods, (PyObject *)self, name); | |
| 627 } | |
| 628 | |
| 629 static char Itype__doc__[] = | |
| 630 "Simple type for treating strings as input file streams" | |
| 631 ; | |
| 632 | |
| 633 static PyTypeObject Itype = { | |
| 634 PyObject_HEAD_INIT(&PyType_Type) | |
| 4906 | 635 0, /*ob_size*/ |
| 636 "StringI", /*tp_name*/ | |
| 637 sizeof(Iobject), /*tp_basicsize*/ | |
| 638 0, /*tp_itemsize*/ | |
| 4598 | 639 /* methods */ |
| 640 (destructor)I_dealloc, /*tp_dealloc*/ | |
| 641 (printfunc)0, /*tp_print*/ | |
| 642 (getattrfunc)I_getattr, /*tp_getattr*/ | |
| 4906 | 643 (setattrfunc)0, /*tp_setattr*/ |
| 4598 | 644 (cmpfunc)0, /*tp_compare*/ |
| 645 (reprfunc)0, /*tp_repr*/ | |
| 646 0, /*tp_as_number*/ | |
| 4906 | 647 0, /*tp_as_sequence*/ |
| 648 0, /*tp_as_mapping*/ | |
| 4598 | 649 (hashfunc)0, /*tp_hash*/ |
| 650 (ternaryfunc)0, /*tp_call*/ | |
| 651 (reprfunc)0, /*tp_str*/ | |
| 652 | |
| 653 /* Space for future expansion */ | |
| 654 0L,0L,0L,0L, | |
| 4906 | 655 Itype__doc__ /* Documentation string */ |
| 4598 | 656 }; |
| 657 | |
| 658 /* End of code for StringI objects */ | |
| 659 /* -------------------------------------------------------- */ | |
| 660 | |
| 661 | |
| 662 static char IO_StringIO__doc__[] = | |
| 663 "StringIO([s]) -- Return a StringIO-like stream for reading or writing" | |
| 664 ; | |
| 665 | |
| 666 static PyObject * | |
| 4906 | 667 IO_StringIO(ARG(PyObject*, self), ARG(PyObject*, args)) |
| 668 ARGDECL(PyObject*, self) | |
| 669 ARGDECL(PyObject*, args) | |
| 4598 | 670 { |
| 671 PyObject *s=0; | |
| 672 | |
| 673 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL; | |
| 674 if(s) return (PyObject *)newIobject(s); | |
| 675 return (PyObject *)newOobject(128); | |
| 676 } | |
| 677 | |
| 678 /* List of methods defined in the module */ | |
| 679 | |
| 680 static struct PyMethodDef IO_methods[] = { | |
| 4906 | 681 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 682 {NULL, NULL} /* sentinel */ | |
| 4598 | 683 }; |
| 684 | |
| 685 | |
| 686 /* Initialization function for the module (*must* be called initcStringIO) */ | |
| 687 | |
| 688 void | |
| 689 initcStringIO() | |
| 690 { | |
| 691 PyObject *m, *d; | |
| 692 | |
| 693 /* Create the module and add the functions */ | |
| 694 m = Py_InitModule4("cStringIO", IO_methods, | |
| 695 cStringIO_module_documentation, | |
| 696 (PyObject*)NULL,PYTHON_API_VERSION); | |
| 697 | |
| 698 /* Add some symbolic constants to the module */ | |
| 699 d = PyModule_GetDict(m); | |
| 700 ErrorObject = PyString_FromString("cStringIO.error"); | |
| 701 PyDict_SetItemString(d, "error", ErrorObject); | |
| 702 | |
| 4906 | 703 #ifdef Py_COBJECT_H |
| 704 /* Export C API */ | |
| 4598 | 705 PyDict_SetItemString(d,"cread", PyCObject_FromVoidPtr(O_cread,NULL)); |
| 706 PyDict_SetItemString(d,"creadline", PyCObject_FromVoidPtr(O_creadline,NULL)); | |
| 707 PyDict_SetItemString(d,"cwrite", PyCObject_FromVoidPtr(O_cwrite,NULL)); | |
| 708 PyDict_SetItemString(d,"cgetvalue", PyCObject_FromVoidPtr(O_getval,NULL)); | |
| 709 PyDict_SetItemString(d,"NewInput", PyCObject_FromVoidPtr(newIobject,NULL)); | |
| 710 PyDict_SetItemString(d,"NewOutput", PyCObject_FromVoidPtr(newOobject,NULL)); | |
| 4906 | 711 #endif |
| 4598 | 712 |
| 713 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); | |
| 714 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); | |
| 715 | |
| 716 | |
| 717 /* Check for errors */ | |
| 718 if (PyErr_Occurred()) | |
| 719 Py_FatalError("can't initialize module cStringIO"); | |
| 720 } | |
| 4906 | 721 |
