/Modules/cStringIO.c

http://unladen-swallow.googlecode.com/ · C · 774 lines · 593 code · 148 blank · 33 comment · 100 complexity · b9fb91c0452b6bcf3d264c72e4d99c63 MD5 · raw file

  1. #include "Python.h"
  2. #include "import.h"
  3. #include "cStringIO.h"
  4. #include "structmember.h"
  5. PyDoc_STRVAR(cStringIO_module_documentation,
  6. "A simple fast partial StringIO replacement.\n"
  7. "\n"
  8. "This module provides a simple useful replacement for\n"
  9. "the StringIO module that is written in C. It does not provide the\n"
  10. "full generality of StringIO, but it provides enough for most\n"
  11. "applications and is especially useful in conjunction with the\n"
  12. "pickle module.\n"
  13. "\n"
  14. "Usage:\n"
  15. "\n"
  16. " from cStringIO import StringIO\n"
  17. "\n"
  18. " an_output_stream=StringIO()\n"
  19. " an_output_stream.write(some_stuff)\n"
  20. " ...\n"
  21. " value=an_output_stream.getvalue()\n"
  22. "\n"
  23. " an_input_stream=StringIO(a_string)\n"
  24. " spam=an_input_stream.readline()\n"
  25. " spam=an_input_stream.read(5)\n"
  26. " an_input_stream.seek(0) # OK, start over\n"
  27. " spam=an_input_stream.read() # and read it all\n"
  28. " \n"
  29. "If someone else wants to provide a more complete implementation,\n"
  30. "go for it. :-) \n"
  31. "\n"
  32. "cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
  33. /* Declaration for file-like objects that manage data as strings
  34. The IOobject type should be though of as a common base type for
  35. Iobjects, which provide input (read-only) StringIO objects and
  36. Oobjects, which provide read-write objects. Most of the methods
  37. depend only on common data.
  38. */
  39. typedef struct {
  40. PyObject_HEAD
  41. char *buf;
  42. Py_ssize_t pos, string_size;
  43. } IOobject;
  44. #define IOOOBJECT(O) ((IOobject*)(O))
  45. /* Declarations for objects of type StringO */
  46. typedef struct { /* Subtype of IOobject */
  47. PyObject_HEAD
  48. char *buf;
  49. Py_ssize_t pos, string_size;
  50. Py_ssize_t buf_size;
  51. int softspace;
  52. } Oobject;
  53. /* Declarations for objects of type StringI */
  54. typedef struct { /* Subtype of IOobject */
  55. PyObject_HEAD
  56. char *buf;
  57. Py_ssize_t pos, string_size;
  58. /* We store a reference to the object here in order to keep
  59. the buffer alive during the lifetime of the Iobject. */
  60. PyObject *pbuf;
  61. } Iobject;
  62. /* IOobject (common) methods */
  63. PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing.");
  64. static int
  65. IO__opencheck(IOobject *self) {
  66. if (!self->buf) {
  67. PyErr_SetString(PyExc_ValueError,
  68. "I/O operation on closed file");
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. static PyObject *
  74. IO_get_closed(IOobject *self, void *closure)
  75. {
  76. PyObject *result = Py_False;
  77. if (self->buf == NULL)
  78. result = Py_True;
  79. Py_INCREF(result);
  80. return result;
  81. }
  82. static PyGetSetDef file_getsetlist[] = {
  83. {"closed", (getter)IO_get_closed, NULL, "True if the file is closed"},
  84. {0},
  85. };
  86. static PyObject *
  87. IO_flush(IOobject *self, PyObject *unused) {
  88. if (!IO__opencheck(self)) return NULL;
  89. Py_INCREF(Py_None);
  90. return Py_None;
  91. }
  92. PyDoc_STRVAR(IO_getval__doc__,
  93. "getvalue([use_pos]) -- Get the string value."
  94. "\n"
  95. "If use_pos is specified and is a true value, then the string returned\n"
  96. "will include only the text up to the current file position.\n");
  97. static PyObject *
  98. IO_cgetval(PyObject *self) {
  99. if (!IO__opencheck(IOOOBJECT(self))) return NULL;
  100. assert(IOOOBJECT(self)->pos >= 0);
  101. return PyString_FromStringAndSize(((IOobject*)self)->buf,
  102. ((IOobject*)self)->pos);
  103. }
  104. static PyObject *
  105. IO_getval(IOobject *self, PyObject *args) {
  106. PyObject *use_pos=Py_None;
  107. Py_ssize_t s;
  108. if (!IO__opencheck(self)) return NULL;
  109. if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
  110. if (PyObject_IsTrue(use_pos)) {
  111. s=self->pos;
  112. if (s > self->string_size) s=self->string_size;
  113. }
  114. else
  115. s=self->string_size;
  116. assert(self->pos >= 0);
  117. return PyString_FromStringAndSize(self->buf, s);
  118. }
  119. PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
  120. static PyObject *
  121. IO_isatty(IOobject *self, PyObject *unused) {
  122. if (!IO__opencheck(self)) return NULL;
  123. Py_INCREF(Py_False);
  124. return Py_False;
  125. }
  126. PyDoc_STRVAR(IO_read__doc__,
  127. "read([s]) -- Read s characters, or the rest of the string");
  128. static int
  129. IO_cread(PyObject *self, char **output, Py_ssize_t n) {
  130. Py_ssize_t l;
  131. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  132. assert(IOOOBJECT(self)->pos >= 0);
  133. assert(IOOOBJECT(self)->string_size >= 0);
  134. l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
  135. if (n < 0 || n > l) {
  136. n = l;
  137. if (n < 0) n=0;
  138. }
  139. *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
  140. ((IOobject*)self)->pos += n;
  141. return n;
  142. }
  143. static PyObject *
  144. IO_read(IOobject *self, PyObject *args) {
  145. Py_ssize_t n = -1;
  146. char *output = NULL;
  147. if (!PyArg_ParseTuple(args, "|n:read", &n)) return NULL;
  148. if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
  149. return PyString_FromStringAndSize(output, n);
  150. }
  151. PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
  152. static int
  153. IO_creadline(PyObject *self, char **output) {
  154. char *n, *s;
  155. Py_ssize_t l;
  156. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  157. for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
  158. s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
  159. n < s && *n != '\n'; n++);
  160. if (n < s) n++;
  161. *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
  162. l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
  163. assert(IOOOBJECT(self)->pos <= PY_SSIZE_T_MAX - l);
  164. assert(IOOOBJECT(self)->pos >= 0);
  165. assert(IOOOBJECT(self)->string_size >= 0);
  166. ((IOobject*)self)->pos += l;
  167. return (int)l;
  168. }
  169. static PyObject *
  170. IO_readline(IOobject *self, PyObject *args) {
  171. int n, m=-1;
  172. char *output;
  173. if (args)
  174. if (!PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
  175. if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
  176. if (m >= 0 && m < n) {
  177. m = n - m;
  178. n -= m;
  179. self->pos -= m;
  180. }
  181. assert(IOOOBJECT(self)->pos >= 0);
  182. return PyString_FromStringAndSize(output, n);
  183. }
  184. PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
  185. static PyObject *
  186. IO_readlines(IOobject *self, PyObject *args) {
  187. int n;
  188. char *output;
  189. PyObject *result, *line;
  190. int hint = 0, length = 0;
  191. if (!PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
  192. result = PyList_New(0);
  193. if (!result)
  194. return NULL;
  195. while (1){
  196. if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
  197. goto err;
  198. if (n == 0)
  199. break;
  200. line = PyString_FromStringAndSize (output, n);
  201. if (!line)
  202. goto err;
  203. if (PyList_Append (result, line) == -1) {
  204. Py_DECREF (line);
  205. goto err;
  206. }
  207. Py_DECREF (line);
  208. length += n;
  209. if (hint > 0 && length >= hint)
  210. break;
  211. }
  212. return result;
  213. err:
  214. Py_DECREF(result);
  215. return NULL;
  216. }
  217. PyDoc_STRVAR(IO_reset__doc__,
  218. "reset() -- Reset the file position to the beginning");
  219. static PyObject *
  220. IO_reset(IOobject *self, PyObject *unused) {
  221. if (!IO__opencheck(self)) return NULL;
  222. self->pos = 0;
  223. Py_INCREF(Py_None);
  224. return Py_None;
  225. }
  226. PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
  227. static PyObject *
  228. IO_tell(IOobject *self, PyObject *unused) {
  229. if (!IO__opencheck(self)) return NULL;
  230. assert(self->pos >= 0);
  231. return PyInt_FromSsize_t(self->pos);
  232. }
  233. PyDoc_STRVAR(IO_truncate__doc__,
  234. "truncate(): truncate the file at the current position.");
  235. static PyObject *
  236. IO_truncate(IOobject *self, PyObject *args) {
  237. Py_ssize_t pos = -1;
  238. if (!IO__opencheck(self)) return NULL;
  239. if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;
  240. if (PyTuple_Size(args) == 0) {
  241. /* No argument passed, truncate to current position */
  242. pos = self->pos;
  243. }
  244. if (pos < 0) {
  245. errno = EINVAL;
  246. PyErr_SetFromErrno(PyExc_IOError);
  247. return NULL;
  248. }
  249. if (self->string_size > pos) self->string_size = pos;
  250. self->pos = self->string_size;
  251. Py_INCREF(Py_None);
  252. return Py_None;
  253. }
  254. static PyObject *
  255. IO_iternext(Iobject *self)
  256. {
  257. PyObject *next;
  258. next = IO_readline((IOobject *)self, NULL);
  259. if (!next)
  260. return NULL;
  261. if (!PyString_GET_SIZE(next)) {
  262. Py_DECREF(next);
  263. PyErr_SetNone(PyExc_StopIteration);
  264. return NULL;
  265. }
  266. return next;
  267. }
  268. /* Read-write object methods */
  269. PyDoc_STRVAR(O_seek__doc__,
  270. "seek(position) -- set the current position\n"
  271. "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
  272. static PyObject *
  273. O_seek(Oobject *self, PyObject *args) {
  274. Py_ssize_t position;
  275. int mode = 0;
  276. if (!IO__opencheck(IOOOBJECT(self))) return NULL;
  277. if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
  278. return NULL;
  279. if (mode == 2) {
  280. position += self->string_size;
  281. }
  282. else if (mode == 1) {
  283. position += self->pos;
  284. }
  285. if (position > self->buf_size) {
  286. char *newbuf;
  287. self->buf_size*=2;
  288. if (self->buf_size <= position) self->buf_size=position+1;
  289. newbuf = (char*) realloc(self->buf,self->buf_size);
  290. if (!newbuf) {
  291. free(self->buf);
  292. self->buf = 0;
  293. self->buf_size=self->pos=0;
  294. return PyErr_NoMemory();
  295. }
  296. self->buf = newbuf;
  297. }
  298. else if (position < 0) position=0;
  299. self->pos=position;
  300. while (--position >= self->string_size) self->buf[position]=0;
  301. Py_INCREF(Py_None);
  302. return Py_None;
  303. }
  304. PyDoc_STRVAR(O_write__doc__,
  305. "write(s) -- Write a string to the file"
  306. "\n\nNote (hack:) writing None resets the buffer");
  307. static int
  308. O_cwrite(PyObject *self, const char *c, Py_ssize_t l) {
  309. Py_ssize_t newl;
  310. Oobject *oself;
  311. char *newbuf;
  312. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  313. oself = (Oobject *)self;
  314. newl = oself->pos+l;
  315. if (newl >= oself->buf_size) {
  316. oself->buf_size *= 2;
  317. if (oself->buf_size <= newl) {
  318. assert(newl + 1 < INT_MAX);
  319. oself->buf_size = (int)(newl+1);
  320. }
  321. newbuf = (char*)realloc(oself->buf, oself->buf_size);
  322. if (!newbuf) {
  323. PyErr_SetString(PyExc_MemoryError,"out of memory");
  324. free(oself->buf);
  325. oself->buf = 0;
  326. oself->buf_size = oself->pos = 0;
  327. return -1;
  328. }
  329. oself->buf = newbuf;
  330. }
  331. memcpy(oself->buf+oself->pos,c,l);
  332. assert(oself->pos + l < INT_MAX);
  333. oself->pos += (int)l;
  334. if (oself->string_size < oself->pos) {
  335. oself->string_size = oself->pos;
  336. }
  337. return (int)l;
  338. }
  339. static PyObject *
  340. O_write(Oobject *self, PyObject *args) {
  341. char *c;
  342. int l;
  343. if (!PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
  344. if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
  345. Py_INCREF(Py_None);
  346. return Py_None;
  347. }
  348. PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
  349. static PyObject *
  350. O_close(Oobject *self, PyObject *unused) {
  351. if (self->buf != NULL) free(self->buf);
  352. self->buf = NULL;
  353. self->pos = self->string_size = self->buf_size = 0;
  354. Py_INCREF(Py_None);
  355. return Py_None;
  356. }
  357. PyDoc_STRVAR(O_writelines__doc__,
  358. "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
  359. "\n"
  360. "Note that newlines are not added. The sequence can be any iterable object\n"
  361. "producing strings. This is equivalent to calling write() for each string.");
  362. static PyObject *
  363. O_writelines(Oobject *self, PyObject *args) {
  364. PyObject *it, *s;
  365. it = PyObject_GetIter(args);
  366. if (it == NULL)
  367. return NULL;
  368. while ((s = PyIter_Next(it)) != NULL) {
  369. Py_ssize_t n;
  370. char *c;
  371. if (PyString_AsStringAndSize(s, &c, &n) == -1) {
  372. Py_DECREF(it);
  373. Py_DECREF(s);
  374. return NULL;
  375. }
  376. if (O_cwrite((PyObject *)self, c, n) == -1) {
  377. Py_DECREF(it);
  378. Py_DECREF(s);
  379. return NULL;
  380. }
  381. Py_DECREF(s);
  382. }
  383. Py_DECREF(it);
  384. /* See if PyIter_Next failed */
  385. if (PyErr_Occurred())
  386. return NULL;
  387. Py_RETURN_NONE;
  388. }
  389. static struct PyMethodDef O_methods[] = {
  390. /* Common methods: */
  391. {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
  392. {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
  393. {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
  394. {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
  395. {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
  396. {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
  397. {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
  398. {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
  399. {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
  400. /* Read-write StringIO specific methods: */
  401. {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
  402. {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
  403. {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
  404. {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
  405. {NULL, NULL} /* sentinel */
  406. };
  407. static PyMemberDef O_memberlist[] = {
  408. {"softspace", T_INT, offsetof(Oobject, softspace), 0,
  409. "flag indicating that a space needs to be printed; used by print"},
  410. /* getattr(f, "closed") is implemented without this table */
  411. {NULL} /* Sentinel */
  412. };
  413. static void
  414. O_dealloc(Oobject *self) {
  415. if (self->buf != NULL)
  416. free(self->buf);
  417. PyObject_Del(self);
  418. }
  419. PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
  420. static PyTypeObject Otype = {
  421. PyVarObject_HEAD_INIT(NULL, 0)
  422. "cStringIO.StringO", /*tp_name*/
  423. sizeof(Oobject), /*tp_basicsize*/
  424. 0, /*tp_itemsize*/
  425. /* methods */
  426. (destructor)O_dealloc, /*tp_dealloc*/
  427. 0, /*tp_print*/
  428. 0, /*tp_getattr */
  429. 0, /*tp_setattr */
  430. 0, /*tp_compare*/
  431. 0, /*tp_repr*/
  432. 0, /*tp_as_number*/
  433. 0, /*tp_as_sequence*/
  434. 0, /*tp_as_mapping*/
  435. 0, /*tp_hash*/
  436. 0 , /*tp_call*/
  437. 0, /*tp_str*/
  438. 0, /*tp_getattro */
  439. 0, /*tp_setattro */
  440. 0, /*tp_as_buffer */
  441. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  442. Otype__doc__, /*tp_doc */
  443. 0, /*tp_traverse */
  444. 0, /*tp_clear */
  445. 0, /*tp_richcompare */
  446. 0, /*tp_weaklistoffset */
  447. PyObject_SelfIter, /*tp_iter */
  448. (iternextfunc)IO_iternext, /*tp_iternext */
  449. O_methods, /*tp_methods */
  450. O_memberlist, /*tp_members */
  451. file_getsetlist, /*tp_getset */
  452. };
  453. static PyObject *
  454. newOobject(int size) {
  455. Oobject *self;
  456. self = PyObject_New(Oobject, &Otype);
  457. if (self == NULL)
  458. return NULL;
  459. self->pos=0;
  460. self->string_size = 0;
  461. self->softspace = 0;
  462. self->buf = (char *)malloc(size);
  463. if (!self->buf) {
  464. PyErr_SetString(PyExc_MemoryError,"out of memory");
  465. self->buf_size = 0;
  466. Py_DECREF(self);
  467. return NULL;
  468. }
  469. self->buf_size=size;
  470. return (PyObject*)self;
  471. }
  472. /* End of code for StringO objects */
  473. /* -------------------------------------------------------- */
  474. static PyObject *
  475. I_close(Iobject *self, PyObject *unused) {
  476. Py_CLEAR(self->pbuf);
  477. self->buf = NULL;
  478. self->pos = self->string_size = 0;
  479. Py_INCREF(Py_None);
  480. return Py_None;
  481. }
  482. static PyObject *
  483. I_seek(Iobject *self, PyObject *args) {
  484. Py_ssize_t position;
  485. int mode = 0;
  486. if (!IO__opencheck(IOOOBJECT(self))) return NULL;
  487. if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
  488. return NULL;
  489. if (mode == 2) position += self->string_size;
  490. else if (mode == 1) position += self->pos;
  491. if (position < 0) position=0;
  492. self->pos=position;
  493. Py_INCREF(Py_None);
  494. return Py_None;
  495. }
  496. static struct PyMethodDef I_methods[] = {
  497. /* Common methods: */
  498. {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
  499. {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
  500. {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
  501. {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
  502. {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
  503. {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
  504. {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
  505. {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
  506. {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
  507. /* Read-only StringIO specific methods: */
  508. {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
  509. {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
  510. {NULL, NULL}
  511. };
  512. static void
  513. I_dealloc(Iobject *self) {
  514. Py_XDECREF(self->pbuf);
  515. PyObject_Del(self);
  516. }
  517. PyDoc_STRVAR(Itype__doc__,
  518. "Simple type for treating strings as input file streams");
  519. static PyTypeObject Itype = {
  520. PyVarObject_HEAD_INIT(NULL, 0)
  521. "cStringIO.StringI", /*tp_name*/
  522. sizeof(Iobject), /*tp_basicsize*/
  523. 0, /*tp_itemsize*/
  524. /* methods */
  525. (destructor)I_dealloc, /*tp_dealloc*/
  526. 0, /*tp_print*/
  527. 0, /* tp_getattr */
  528. 0, /*tp_setattr*/
  529. 0, /*tp_compare*/
  530. 0, /*tp_repr*/
  531. 0, /*tp_as_number*/
  532. 0, /*tp_as_sequence*/
  533. 0, /*tp_as_mapping*/
  534. 0, /*tp_hash*/
  535. 0, /*tp_call*/
  536. 0, /*tp_str*/
  537. 0, /* tp_getattro */
  538. 0, /* tp_setattro */
  539. 0, /* tp_as_buffer */
  540. Py_TPFLAGS_DEFAULT, /* tp_flags */
  541. Itype__doc__, /* tp_doc */
  542. 0, /* tp_traverse */
  543. 0, /* tp_clear */
  544. 0, /* tp_richcompare */
  545. 0, /* tp_weaklistoffset */
  546. PyObject_SelfIter, /* tp_iter */
  547. (iternextfunc)IO_iternext, /* tp_iternext */
  548. I_methods, /* tp_methods */
  549. 0, /* tp_members */
  550. file_getsetlist, /* tp_getset */
  551. };
  552. static PyObject *
  553. newIobject(PyObject *s) {
  554. Iobject *self;
  555. char *buf;
  556. Py_ssize_t size;
  557. if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
  558. PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
  559. s->ob_type->tp_name);
  560. return NULL;
  561. }
  562. self = PyObject_New(Iobject, &Itype);
  563. if (!self) return NULL;
  564. Py_INCREF(s);
  565. self->buf=buf;
  566. self->string_size=size;
  567. self->pbuf=s;
  568. self->pos=0;
  569. return (PyObject*)self;
  570. }
  571. /* End of code for StringI objects */
  572. /* -------------------------------------------------------- */
  573. PyDoc_STRVAR(IO_StringIO__doc__,
  574. "StringIO([s]) -- Return a StringIO-like stream for reading or writing");
  575. static PyObject *
  576. IO_StringIO(PyObject *self, PyObject *args) {
  577. PyObject *s=0;
  578. if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
  579. if (s) return newIobject(s);
  580. return newOobject(128);
  581. }
  582. /* List of methods defined in the module */
  583. static struct PyMethodDef IO_methods[] = {
  584. {"StringIO", (PyCFunction)IO_StringIO,
  585. METH_VARARGS, IO_StringIO__doc__},
  586. {NULL, NULL} /* sentinel */
  587. };
  588. /* Initialization function for the module (*must* be called initcStringIO) */
  589. static struct PycStringIO_CAPI CAPI = {
  590. IO_cread,
  591. IO_creadline,
  592. O_cwrite,
  593. IO_cgetval,
  594. newOobject,
  595. newIobject,
  596. &Itype,
  597. &Otype,
  598. };
  599. #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
  600. #define PyMODINIT_FUNC void
  601. #endif
  602. PyMODINIT_FUNC
  603. initcStringIO(void) {
  604. PyObject *m, *d, *v;
  605. /* Create the module and add the functions */
  606. m = Py_InitModule4("cStringIO", IO_methods,
  607. cStringIO_module_documentation,
  608. (PyObject*)NULL,PYTHON_API_VERSION);
  609. if (m == NULL) return;
  610. /* Add some symbolic constants to the module */
  611. d = PyModule_GetDict(m);
  612. /* Export C API */
  613. Py_TYPE(&Itype)=&PyType_Type;
  614. Py_TYPE(&Otype)=&PyType_Type;
  615. if (PyType_Ready(&Otype) < 0) return;
  616. if (PyType_Ready(&Itype) < 0) return;
  617. PyDict_SetItemString(d,"cStringIO_CAPI",
  618. v = PyCObject_FromVoidPtr(&CAPI,NULL));
  619. Py_XDECREF(v);
  620. /* Export Types */
  621. PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
  622. PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
  623. /* Maybe make certain warnings go away */
  624. if (0) PycString_IMPORT;
  625. }