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