You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

760 lines
21 KiB

20 years ago
  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. int b;
  108. Py_ssize_t s;
  109. if (!IO__opencheck(self)) return NULL;
  110. if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
  111. b = PyObject_IsTrue(use_pos);
  112. if (b < 0)
  113. return NULL;
  114. if (b) {
  115. s=self->pos;
  116. if (s > self->string_size) s=self->string_size;
  117. }
  118. else
  119. s=self->string_size;
  120. assert(self->pos >= 0);
  121. return PyString_FromStringAndSize(self->buf, s);
  122. }
  123. PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
  124. static PyObject *
  125. IO_isatty(IOobject *self, PyObject *unused) {
  126. if (!IO__opencheck(self)) return NULL;
  127. Py_INCREF(Py_False);
  128. return Py_False;
  129. }
  130. PyDoc_STRVAR(IO_read__doc__,
  131. "read([s]) -- Read s characters, or the rest of the string");
  132. static int
  133. IO_cread(PyObject *self, char **output, Py_ssize_t n) {
  134. Py_ssize_t l;
  135. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  136. assert(IOOOBJECT(self)->pos >= 0);
  137. assert(IOOOBJECT(self)->string_size >= 0);
  138. l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
  139. if (n < 0 || n > l) {
  140. n = l;
  141. if (n < 0) n=0;
  142. }
  143. *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
  144. ((IOobject*)self)->pos += n;
  145. return n;
  146. }
  147. static PyObject *
  148. IO_read(IOobject *self, PyObject *args) {
  149. Py_ssize_t n = -1;
  150. char *output = NULL;
  151. if (!PyArg_ParseTuple(args, "|n:read", &n)) return NULL;
  152. if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
  153. return PyString_FromStringAndSize(output, n);
  154. }
  155. PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
  156. static int
  157. IO_creadline(PyObject *self, char **output) {
  158. char *n, *s;
  159. Py_ssize_t l;
  160. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  161. for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
  162. s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
  163. n < s && *n != '\n'; n++);
  164. if (n < s) n++;
  165. *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
  166. l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
  167. assert(IOOOBJECT(self)->pos <= PY_SSIZE_T_MAX - l);
  168. assert(IOOOBJECT(self)->pos >= 0);
  169. assert(IOOOBJECT(self)->string_size >= 0);
  170. ((IOobject*)self)->pos += l;
  171. return (int)l;
  172. }
  173. static PyObject *
  174. IO_readline(IOobject *self, PyObject *args) {
  175. int n, m=-1;
  176. char *output;
  177. if (args)
  178. if (!PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
  179. if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
  180. if (m >= 0 && m < n) {
  181. m = n - m;
  182. n -= m;
  183. self->pos -= m;
  184. }
  185. assert(IOOOBJECT(self)->pos >= 0);
  186. return PyString_FromStringAndSize(output, n);
  187. }
  188. PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
  189. static PyObject *
  190. IO_readlines(IOobject *self, PyObject *args) {
  191. int n;
  192. char *output;
  193. PyObject *result, *line;
  194. int hint = 0, length = 0;
  195. if (!PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
  196. result = PyList_New(0);
  197. if (!result)
  198. return NULL;
  199. while (1){
  200. if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
  201. goto err;
  202. if (n == 0)
  203. break;
  204. line = PyString_FromStringAndSize (output, n);
  205. if (!line)
  206. goto err;
  207. if (PyList_Append (result, line) == -1) {
  208. Py_DECREF (line);
  209. goto err;
  210. }
  211. Py_DECREF (line);
  212. length += n;
  213. if (hint > 0 && length >= hint)
  214. break;
  215. }
  216. return result;
  217. err:
  218. Py_DECREF(result);
  219. return NULL;
  220. }
  221. PyDoc_STRVAR(IO_reset__doc__,
  222. "reset() -- Reset the file position to the beginning");
  223. static PyObject *
  224. IO_reset(IOobject *self, PyObject *unused) {
  225. if (!IO__opencheck(self)) return NULL;
  226. self->pos = 0;
  227. Py_INCREF(Py_None);
  228. return Py_None;
  229. }
  230. PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
  231. static PyObject *
  232. IO_tell(IOobject *self, PyObject *unused) {
  233. if (!IO__opencheck(self)) return NULL;
  234. assert(self->pos >= 0);
  235. return PyInt_FromSsize_t(self->pos);
  236. }
  237. PyDoc_STRVAR(IO_truncate__doc__,
  238. "truncate(): truncate the file at the current position.");
  239. static PyObject *
  240. IO_truncate(IOobject *self, PyObject *args) {
  241. Py_ssize_t pos = -1;
  242. if (!IO__opencheck(self)) return NULL;
  243. if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;
  244. if (PyTuple_Size(args) == 0) {
  245. /* No argument passed, truncate to current position */
  246. pos = self->pos;
  247. }
  248. if (pos < 0) {
  249. errno = EINVAL;
  250. PyErr_SetFromErrno(PyExc_IOError);
  251. return NULL;
  252. }
  253. if (self->string_size > pos) self->string_size = pos;
  254. self->pos = self->string_size;
  255. Py_INCREF(Py_None);
  256. return Py_None;
  257. }
  258. static PyObject *
  259. IO_iternext(Iobject *self)
  260. {
  261. PyObject *next;
  262. next = IO_readline((IOobject *)self, NULL);
  263. if (!next)
  264. return NULL;
  265. if (!PyString_GET_SIZE(next)) {
  266. Py_DECREF(next);
  267. PyErr_SetNone(PyExc_StopIteration);
  268. return NULL;
  269. }
  270. return next;
  271. }
  272. /* Read-write object methods */
  273. PyDoc_STRVAR(IO_seek__doc__,
  274. "seek(position) -- set the current position\n"
  275. "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
  276. static PyObject *
  277. IO_seek(Iobject *self, PyObject *args) {
  278. Py_ssize_t position;
  279. int mode = 0;
  280. if (!IO__opencheck(IOOOBJECT(self))) return NULL;
  281. if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
  282. return NULL;
  283. if (mode == 2) {
  284. position += self->string_size;
  285. }
  286. else if (mode == 1) {
  287. position += self->pos;
  288. }
  289. if (position < 0) position=0;
  290. self->pos=position;
  291. Py_INCREF(Py_None);
  292. return Py_None;
  293. }
  294. PyDoc_STRVAR(O_write__doc__,
  295. "write(s) -- Write a string to the file"
  296. "\n\nNote (hack:) writing None resets the buffer");
  297. static int
  298. O_cwrite(PyObject *self, const char *c, Py_ssize_t l) {
  299. Py_ssize_t newl;
  300. Oobject *oself;
  301. char *newbuf;
  302. if (!IO__opencheck(IOOOBJECT(self))) return -1;
  303. oself = (Oobject *)self;
  304. newl = oself->pos+l;
  305. if (newl >= oself->buf_size) {
  306. oself->buf_size *= 2;
  307. if (oself->buf_size <= newl) {
  308. assert(newl + 1 < INT_MAX);
  309. oself->buf_size = (int)(newl+1);
  310. }
  311. newbuf = (char*)realloc(oself->buf, oself->buf_size);
  312. if (!newbuf) {
  313. PyErr_SetString(PyExc_MemoryError,"out of memory");
  314. free(oself->buf);
  315. oself->buf = 0;
  316. oself->buf_size = oself->pos = 0;
  317. return -1;
  318. }
  319. oself->buf = newbuf;
  320. }
  321. if (oself->string_size < oself->pos) {
  322. /* In case of overseek, pad with null bytes the buffer region between
  323. the end of stream and the current position.
  324. 0 lo string_size hi
  325. | |<---used--->|<----------available----------->|
  326. | | <--to pad-->|<---to write---> |
  327. 0 buf position
  328. */
  329. memset(oself->buf + oself->string_size, '\0',
  330. (oself->pos - oself->string_size) * sizeof(char));
  331. }
  332. memcpy(oself->buf+oself->pos,c,l);
  333. assert(oself->pos + l < INT_MAX);
  334. oself->pos += (int)l;
  335. if (oself->string_size < oself->pos) {
  336. oself->string_size = oself->pos;
  337. }
  338. return (int)l;
  339. }
  340. static PyObject *
  341. O_write(Oobject *self, PyObject *args) {
  342. char *c;
  343. int l;
  344. if (!PyArg_ParseTuple(args, "t#:write", &c, &l)) return NULL;
  345. if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
  346. Py_INCREF(Py_None);
  347. return Py_None;
  348. }
  349. PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
  350. static PyObject *
  351. O_close(Oobject *self, PyObject *unused) {
  352. if (self->buf != NULL) free(self->buf);
  353. self->buf = NULL;
  354. self->pos = self->string_size = self->buf_size = 0;
  355. Py_INCREF(Py_None);
  356. return Py_None;
  357. }
  358. PyDoc_STRVAR(O_writelines__doc__,
  359. "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
  360. "\n"
  361. "Note that newlines are not added. The sequence can be any iterable object\n"
  362. "producing strings. This is equivalent to calling write() for each string.");
  363. static PyObject *
  364. O_writelines(Oobject *self, PyObject *args) {
  365. PyObject *it, *s;
  366. it = PyObject_GetIter(args);
  367. if (it == NULL)
  368. return NULL;
  369. while ((s = PyIter_Next(it)) != NULL) {
  370. Py_ssize_t n;
  371. char *c;
  372. if (PyString_AsStringAndSize(s, &c, &n) == -1) {
  373. Py_DECREF(it);
  374. Py_DECREF(s);
  375. return NULL;
  376. }
  377. if (O_cwrite((PyObject *)self, c, n) == -1) {
  378. Py_DECREF(it);
  379. Py_DECREF(s);
  380. return NULL;
  381. }
  382. Py_DECREF(s);
  383. }
  384. Py_DECREF(it);
  385. /* See if PyIter_Next failed */
  386. if (PyErr_Occurred())
  387. return NULL;
  388. Py_RETURN_NONE;
  389. }
  390. static struct PyMethodDef O_methods[] = {
  391. /* Common methods: */
  392. {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
  393. {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
  394. {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
  395. {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
  396. {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
  397. {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
  398. {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
  399. {"seek", (PyCFunction)IO_seek, METH_VARARGS, IO_seek__doc__},
  400. {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
  401. {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
  402. /* Read-write StringIO specific methods: */
  403. {"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
  404. {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
  405. {"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
  406. {NULL, NULL} /* sentinel */
  407. };
  408. static PyMemberDef O_memberlist[] = {
  409. {"softspace", T_INT, offsetof(Oobject, softspace), 0,
  410. "flag indicating that a space needs to be printed; used by print"},
  411. /* getattr(f, "closed") is implemented without this table */
  412. {NULL} /* Sentinel */
  413. };
  414. static void
  415. O_dealloc(Oobject *self) {
  416. if (self->buf != NULL)
  417. free(self->buf);
  418. PyObject_Del(self);
  419. }
  420. PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
  421. static PyTypeObject Otype = {
  422. PyVarObject_HEAD_INIT(NULL, 0)
  423. "cStringIO.StringO", /*tp_name*/
  424. sizeof(Oobject), /*tp_basicsize*/
  425. 0, /*tp_itemsize*/
  426. /* methods */
  427. (destructor)O_dealloc, /*tp_dealloc*/
  428. 0, /*tp_print*/
  429. 0, /*tp_getattr */
  430. 0, /*tp_setattr */
  431. 0, /*tp_compare*/
  432. 0, /*tp_repr*/
  433. 0, /*tp_as_number*/
  434. 0, /*tp_as_sequence*/
  435. 0, /*tp_as_mapping*/
  436. 0, /*tp_hash*/
  437. 0 , /*tp_call*/
  438. 0, /*tp_str*/
  439. 0, /*tp_getattro */
  440. 0, /*tp_setattro */
  441. 0, /*tp_as_buffer */
  442. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  443. Otype__doc__, /*tp_doc */
  444. 0, /*tp_traverse */
  445. 0, /*tp_clear */
  446. 0, /*tp_richcompare */
  447. 0, /*tp_weaklistoffset */
  448. PyObject_SelfIter, /*tp_iter */
  449. (iternextfunc)IO_iternext, /*tp_iternext */
  450. O_methods, /*tp_methods */
  451. O_memberlist, /*tp_members */
  452. file_getsetlist, /*tp_getset */
  453. };
  454. static PyObject *
  455. newOobject(int size) {
  456. Oobject *self;
  457. self = PyObject_New(Oobject, &Otype);
  458. if (self == NULL)
  459. return NULL;
  460. self->pos=0;
  461. self->string_size = 0;
  462. self->softspace = 0;
  463. self->buf = (char *)malloc(size);
  464. if (!self->buf) {
  465. PyErr_SetString(PyExc_MemoryError,"out of memory");
  466. self->buf_size = 0;
  467. Py_DECREF(self);
  468. return NULL;
  469. }
  470. self->buf_size=size;
  471. return (PyObject*)self;
  472. }
  473. /* End of code for StringO objects */
  474. /* -------------------------------------------------------- */
  475. static PyObject *
  476. I_close(Iobject *self, PyObject *unused) {
  477. Py_CLEAR(self->pbuf);
  478. self->buf = NULL;
  479. self->pos = self->string_size = 0;
  480. Py_INCREF(Py_None);
  481. return Py_None;
  482. }
  483. static struct PyMethodDef I_methods[] = {
  484. /* Common methods: */
  485. {"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
  486. {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
  487. {"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
  488. {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
  489. {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
  490. {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
  491. {"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
  492. {"seek", (PyCFunction)IO_seek, METH_VARARGS, IO_seek__doc__},
  493. {"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
  494. {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
  495. /* Read-only StringIO specific methods: */
  496. {"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
  497. {NULL, NULL}
  498. };
  499. static void
  500. I_dealloc(Iobject *self) {
  501. Py_XDECREF(self->pbuf);
  502. PyObject_Del(self);
  503. }
  504. PyDoc_STRVAR(Itype__doc__,
  505. "Simple type for treating strings as input file streams");
  506. static PyTypeObject Itype = {
  507. PyVarObject_HEAD_INIT(NULL, 0)
  508. "cStringIO.StringI", /*tp_name*/
  509. sizeof(Iobject), /*tp_basicsize*/
  510. 0, /*tp_itemsize*/
  511. /* methods */
  512. (destructor)I_dealloc, /*tp_dealloc*/
  513. 0, /*tp_print*/
  514. 0, /* tp_getattr */
  515. 0, /*tp_setattr*/
  516. 0, /*tp_compare*/
  517. 0, /*tp_repr*/
  518. 0, /*tp_as_number*/
  519. 0, /*tp_as_sequence*/
  520. 0, /*tp_as_mapping*/
  521. 0, /*tp_hash*/
  522. 0, /*tp_call*/
  523. 0, /*tp_str*/
  524. 0, /* tp_getattro */
  525. 0, /* tp_setattro */
  526. 0, /* tp_as_buffer */
  527. Py_TPFLAGS_DEFAULT, /* tp_flags */
  528. Itype__doc__, /* tp_doc */
  529. 0, /* tp_traverse */
  530. 0, /* tp_clear */
  531. 0, /* tp_richcompare */
  532. 0, /* tp_weaklistoffset */
  533. PyObject_SelfIter, /* tp_iter */
  534. (iternextfunc)IO_iternext, /* tp_iternext */
  535. I_methods, /* tp_methods */
  536. 0, /* tp_members */
  537. file_getsetlist, /* tp_getset */
  538. };
  539. static PyObject *
  540. newIobject(PyObject *s) {
  541. Iobject *self;
  542. char *buf;
  543. Py_ssize_t size;
  544. if (PyUnicode_Check(s)) {
  545. if (PyObject_AsCharBuffer(s, (const char **)&buf, &size) != 0)
  546. return NULL;
  547. }
  548. else if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
  549. PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
  550. s->ob_type->tp_name);
  551. return NULL;
  552. }
  553. self = PyObject_New(Iobject, &Itype);
  554. if (!self) return NULL;
  555. Py_INCREF(s);
  556. self->buf=buf;
  557. self->string_size=size;
  558. self->pbuf=s;
  559. self->pos=0;
  560. return (PyObject*)self;
  561. }
  562. /* End of code for StringI objects */
  563. /* -------------------------------------------------------- */
  564. PyDoc_STRVAR(IO_StringIO__doc__,
  565. "StringIO([s]) -- Return a StringIO-like stream for reading or writing");
  566. static PyObject *
  567. IO_StringIO(PyObject *self, PyObject *args) {
  568. PyObject *s=0;
  569. if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
  570. if (s) return newIobject(s);
  571. return newOobject(128);
  572. }
  573. /* List of methods defined in the module */
  574. static struct PyMethodDef IO_methods[] = {
  575. {"StringIO", (PyCFunction)IO_StringIO,
  576. METH_VARARGS, IO_StringIO__doc__},
  577. {NULL, NULL} /* sentinel */
  578. };
  579. /* Initialization function for the module (*must* be called initcStringIO) */
  580. static struct PycStringIO_CAPI CAPI = {
  581. IO_cread,
  582. IO_creadline,
  583. O_cwrite,
  584. IO_cgetval,
  585. newOobject,
  586. newIobject,
  587. &Itype,
  588. &Otype,
  589. };
  590. #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
  591. #define PyMODINIT_FUNC void
  592. #endif
  593. PyMODINIT_FUNC
  594. initcStringIO(void) {
  595. PyObject *m, *d, *v;
  596. /* Create the module and add the functions */
  597. m = Py_InitModule4("cStringIO", IO_methods,
  598. cStringIO_module_documentation,
  599. (PyObject*)NULL,PYTHON_API_VERSION);
  600. if (m == NULL) return;
  601. /* Add some symbolic constants to the module */
  602. d = PyModule_GetDict(m);
  603. /* Export C API */
  604. Py_TYPE(&Itype)=&PyType_Type;
  605. Py_TYPE(&Otype)=&PyType_Type;
  606. if (PyType_Ready(&Otype) < 0) return;
  607. if (PyType_Ready(&Itype) < 0) return;
  608. v = PyCapsule_New(&CAPI, PycStringIO_CAPSULE_NAME, NULL);
  609. PyDict_SetItemString(d,"cStringIO_CAPI", v);
  610. Py_XDECREF(v);
  611. /* Export Types */
  612. PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
  613. PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
  614. /* Maybe make certain warnings go away */
  615. if (0) PycString_IMPORT;
  616. }