2002-06-13 17:33:02 -03:00
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include "import.h"
|
|
|
|
#include "cStringIO.h"
|
2003-04-24 12:50:11 -03:00
|
|
|
#include "structmember.h"
|
2002-06-13 17:33:02 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(cStringIO_module_documentation,
|
1996-12-05 19:30:48 -04:00
|
|
|
"A simple fast partial StringIO replacement.\n"
|
|
|
|
"\n"
|
|
|
|
"This module provides a simple useful replacement for\n"
|
|
|
|
"the StringIO module that is written in C. It does not provide the\n"
|
2000-06-19 10:17:41 -03:00
|
|
|
"full generality of StringIO, but it provides enough for most\n"
|
2000-07-16 09:04:32 -03:00
|
|
|
"applications and is especially useful in conjunction with the\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
"pickle module.\n"
|
|
|
|
"\n"
|
|
|
|
"Usage:\n"
|
|
|
|
"\n"
|
|
|
|
" from cStringIO import StringIO\n"
|
|
|
|
"\n"
|
|
|
|
" an_output_stream=StringIO()\n"
|
|
|
|
" an_output_stream.write(some_stuff)\n"
|
|
|
|
" ...\n"
|
2002-03-08 13:17:33 -04:00
|
|
|
" value=an_output_stream.getvalue()\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
"\n"
|
|
|
|
" an_input_stream=StringIO(a_string)\n"
|
|
|
|
" spam=an_input_stream.readline()\n"
|
|
|
|
" spam=an_input_stream.read(5)\n"
|
1998-11-25 12:17:32 -04:00
|
|
|
" an_input_stream.seek(0) # OK, start over\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
" spam=an_input_stream.read() # and read it all\n"
|
|
|
|
" \n"
|
|
|
|
"If someone else wants to provide a more complete implementation,\n"
|
|
|
|
"go for it. :-) \n"
|
1997-08-13 00:14:41 -03:00
|
|
|
"\n"
|
2002-06-13 17:33:02 -03:00
|
|
|
"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Declaration for file-like objects that manage data as strings
|
2000-10-06 16:24:23 -03:00
|
|
|
|
|
|
|
The IOobject type should be though of as a common base type for
|
|
|
|
Iobjects, which provide input (read-only) StringIO objects and
|
|
|
|
Oobjects, which provide read-write objects. Most of the methods
|
|
|
|
depend only on common data.
|
|
|
|
*/
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
char *buf;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t pos, string_size;
|
2000-10-06 16:24:23 -03:00
|
|
|
} IOobject;
|
|
|
|
|
|
|
|
#define IOOOBJECT(O) ((IOobject*)(O))
|
|
|
|
|
2003-08-11 10:15:11 -03:00
|
|
|
/* Declarations for objects of type StringO */
|
2000-10-06 16:24:23 -03:00
|
|
|
|
|
|
|
typedef struct { /* Subtype of IOobject */
|
|
|
|
PyObject_HEAD
|
|
|
|
char *buf;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t pos, string_size;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t buf_size;
|
|
|
|
int softspace;
|
1996-12-05 19:30:48 -04:00
|
|
|
} Oobject;
|
|
|
|
|
|
|
|
/* Declarations for objects of type StringI */
|
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
typedef struct { /* Subtype of IOobject */
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject_HEAD
|
|
|
|
char *buf;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t pos, string_size;
|
2013-03-19 21:17:47 -03:00
|
|
|
Py_buffer pbuf;
|
1996-12-05 19:30:48 -04:00
|
|
|
} Iobject;
|
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
/* IOobject (common) methods */
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_flush__doc__, "flush(): does nothing.");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
static int
|
|
|
|
IO__opencheck(IOobject *self) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!self->buf) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"I/O operation on closed file");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2003-08-08 09:20:03 -03:00
|
|
|
static PyObject *
|
|
|
|
IO_get_closed(IOobject *self, void *closure)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *result = Py_False;
|
2003-08-08 09:20:03 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->buf == NULL)
|
|
|
|
result = Py_True;
|
|
|
|
Py_INCREF(result);
|
|
|
|
return result;
|
2003-08-08 09:20:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef file_getsetlist[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"closed", (getter)IO_get_closed, NULL, "True if the file is closed"},
|
|
|
|
{0},
|
2003-08-08 09:20:03 -03:00
|
|
|
};
|
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
IO_flush(IOobject *self, PyObject *unused) {
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(self)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_getval__doc__,
|
|
|
|
"getvalue([use_pos]) -- Get the string value."
|
|
|
|
"\n"
|
|
|
|
"If use_pos is specified and is a true value, then the string returned\n"
|
|
|
|
"will include only the text up to the current file position.\n");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_cgetval(PyObject *self) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(IOOOBJECT(self))) return NULL;
|
|
|
|
assert(IOOOBJECT(self)->pos >= 0);
|
|
|
|
return PyString_FromStringAndSize(((IOobject*)self)->buf,
|
|
|
|
((IOobject*)self)->pos);
|
2000-10-06 16:24:23 -03:00
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
static PyObject *
|
|
|
|
IO_getval(IOobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *use_pos=Py_None;
|
2012-08-15 18:16:51 -03:00
|
|
|
int b;
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t s;
|
|
|
|
|
|
|
|
if (!IO__opencheck(self)) return NULL;
|
|
|
|
if (!PyArg_UnpackTuple(args,"getval", 0, 1,&use_pos)) return NULL;
|
|
|
|
|
2012-08-15 18:16:51 -03:00
|
|
|
b = PyObject_IsTrue(use_pos);
|
|
|
|
if (b < 0)
|
|
|
|
return NULL;
|
|
|
|
if (b) {
|
2010-05-09 11:46:46 -03:00
|
|
|
s=self->pos;
|
|
|
|
if (s > self->string_size) s=self->string_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s=self->string_size;
|
|
|
|
assert(self->pos >= 0);
|
|
|
|
return PyString_FromStringAndSize(self->buf, s);
|
2000-10-06 16:24:23 -03:00
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_isatty__doc__, "isatty(): always returns 0");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
IO_isatty(IOobject *self, PyObject *unused) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(self)) return NULL;
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
return Py_False;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_read__doc__,
|
|
|
|
"read([s]) -- Read s characters, or the rest of the string");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
IO_cread(PyObject *self, char **output, Py_ssize_t n) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t l;
|
|
|
|
|
|
|
|
if (!IO__opencheck(IOOOBJECT(self))) return -1;
|
|
|
|
assert(IOOOBJECT(self)->pos >= 0);
|
|
|
|
assert(IOOOBJECT(self)->string_size >= 0);
|
|
|
|
l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
|
|
|
|
if (n < 0 || n > l) {
|
|
|
|
n = l;
|
|
|
|
if (n < 0) n=0;
|
|
|
|
}
|
2013-02-09 07:47:43 -04:00
|
|
|
if (n > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"length too large");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
*output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
|
|
|
|
((IOobject*)self)->pos += n;
|
2013-02-09 07:47:43 -04:00
|
|
|
return (int)n;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_read(IOobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t n = -1;
|
|
|
|
char *output = NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|n:read", &n)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return PyString_FromStringAndSize(output, n);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_readline__doc__, "readline() -- Read one line");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static int
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_creadline(PyObject *self, char **output) {
|
2013-02-09 07:47:43 -04:00
|
|
|
char *n, *start, *end;
|
|
|
|
Py_ssize_t len;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(IOOOBJECT(self))) return -1;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
n = start = ((IOobject*)self)->buf + ((IOobject*)self)->pos;
|
|
|
|
end = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
|
|
|
|
while (n < end && *n != '\n')
|
|
|
|
n++;
|
2008-06-11 04:41:16 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
if (n < end) n++;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
len = n - start;
|
2013-02-12 15:36:47 -04:00
|
|
|
if (len > INT_MAX)
|
|
|
|
len = INT_MAX;
|
2013-02-09 07:47:43 -04:00
|
|
|
|
|
|
|
*output=start;
|
2008-06-11 04:41:16 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
assert(IOOOBJECT(self)->pos <= PY_SSIZE_T_MAX - len);
|
2010-05-09 11:46:46 -03:00
|
|
|
assert(IOOOBJECT(self)->pos >= 0);
|
|
|
|
assert(IOOOBJECT(self)->string_size >= 0);
|
2008-06-11 04:41:16 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
((IOobject*)self)->pos += len;
|
|
|
|
return (int)len;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_readline(IOobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
int n, m=-1;
|
|
|
|
char *output;
|
|
|
|
|
|
|
|
if (args)
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
|
|
|
|
|
|
|
|
if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
|
|
|
|
if (m >= 0 && m < n) {
|
|
|
|
m = n - m;
|
|
|
|
n -= m;
|
|
|
|
self->pos -= m;
|
|
|
|
}
|
|
|
|
assert(IOOOBJECT(self)->pos >= 0);
|
|
|
|
return PyString_FromStringAndSize(output, n);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_readlines__doc__, "readlines() -- Read all lines");
|
2000-09-19 08:06:46 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_readlines(IOobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
int n;
|
|
|
|
char *output;
|
|
|
|
PyObject *result, *line;
|
2013-02-09 07:47:43 -04:00
|
|
|
Py_ssize_t hint = 0, length = 0;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|n:readlines", &hint)) return NULL;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
result = PyList_New(0);
|
|
|
|
if (!result)
|
2000-10-06 16:24:23 -03:00
|
|
|
return NULL;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
while (1){
|
|
|
|
if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
|
|
|
|
goto err;
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
line = PyString_FromStringAndSize (output, n);
|
|
|
|
if (!line)
|
|
|
|
goto err;
|
|
|
|
if (PyList_Append (result, line) == -1) {
|
|
|
|
Py_DECREF (line);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
Py_DECREF (line);
|
|
|
|
length += n;
|
|
|
|
if (hint > 0 && length >= hint)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
err:
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
2000-09-19 08:06:46 -03:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_reset__doc__,
|
|
|
|
"reset() -- Reset the file position to the beginning");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
IO_reset(IOobject *self, PyObject *unused) {
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(self)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
self->pos = 0;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_tell__doc__, "tell() -- get the current position.");
|
2000-10-06 16:24:23 -03:00
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
IO_tell(IOobject *self, PyObject *unused) {
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(self)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
assert(self->pos >= 0);
|
|
|
|
return PyInt_FromSsize_t(self->pos);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_truncate__doc__,
|
|
|
|
"truncate(): truncate the file at the current position.");
|
1998-12-15 17:43:15 -04:00
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_truncate(IOobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t pos = -1;
|
|
|
|
|
|
|
|
if (!IO__opencheck(self)) return NULL;
|
|
|
|
if (!PyArg_ParseTuple(args, "|n:truncate", &pos)) return NULL;
|
|
|
|
|
|
|
|
if (PyTuple_Size(args) == 0) {
|
|
|
|
/* No argument passed, truncate to current position */
|
|
|
|
pos = self->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->string_size > pos) self->string_size = pos;
|
|
|
|
self->pos = self->string_size;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2003-04-24 12:50:11 -03:00
|
|
|
static PyObject *
|
|
|
|
IO_iternext(Iobject *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *next;
|
|
|
|
next = IO_readline((IOobject *)self, NULL);
|
|
|
|
if (!next)
|
|
|
|
return NULL;
|
|
|
|
if (!PyString_GET_SIZE(next)) {
|
|
|
|
Py_DECREF(next);
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return next;
|
2003-04-24 12:50:11 -03:00
|
|
|
}
|
|
|
|
|
2000-10-06 16:24:23 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Read-write object methods */
|
|
|
|
|
2010-10-11 16:13:04 -03:00
|
|
|
PyDoc_STRVAR(IO_seek__doc__,
|
2000-10-06 16:24:23 -03:00
|
|
|
"seek(position) -- set the current position\n"
|
2002-06-13 17:33:02 -03:00
|
|
|
"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2010-10-11 16:13:04 -03:00
|
|
|
IO_seek(Iobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t position;
|
|
|
|
int mode = 0;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!IO__opencheck(IOOOBJECT(self))) return NULL;
|
|
|
|
if (!PyArg_ParseTuple(args, "n|i:seek", &position, &mode))
|
|
|
|
return NULL;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (mode == 2) {
|
|
|
|
position += self->string_size;
|
|
|
|
}
|
|
|
|
else if (mode == 1) {
|
|
|
|
position += self->pos;
|
|
|
|
}
|
|
|
|
|
2010-10-11 16:13:04 -03:00
|
|
|
if (position < 0) position=0;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
|
|
|
self->pos=position;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(O_write__doc__,
|
2000-10-06 16:24:23 -03:00
|
|
|
"write(s) -- Write a string to the file"
|
2002-06-13 17:33:02 -03:00
|
|
|
"\n\nNote (hack:) writing None resets the buffer");
|
2000-10-06 16:24:23 -03:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2013-02-09 07:47:43 -04:00
|
|
|
O_cwrite(PyObject *self, const char *c, Py_ssize_t len) {
|
|
|
|
Py_ssize_t newpos;
|
2010-05-09 11:46:46 -03:00
|
|
|
Oobject *oself;
|
|
|
|
char *newbuf;
|
|
|
|
|
|
|
|
if (!IO__opencheck(IOOOBJECT(self))) return -1;
|
|
|
|
oself = (Oobject *)self;
|
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
if (len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"length too large");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(len >= 0);
|
|
|
|
if (oself->pos >= PY_SSIZE_T_MAX - len) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"new position too large");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
newpos = oself->pos + len;
|
|
|
|
if (newpos >= oself->buf_size) {
|
|
|
|
size_t newsize = oself->buf_size;
|
|
|
|
newsize *= 2;
|
|
|
|
if (newsize <= (size_t)newpos || newsize > PY_SSIZE_T_MAX) {
|
|
|
|
assert(newpos < PY_SSIZE_T_MAX - 1);
|
|
|
|
newsize = newpos + 1;
|
2010-05-09 11:46:46 -03:00
|
|
|
}
|
2013-02-09 07:47:43 -04:00
|
|
|
newbuf = (char*)realloc(oself->buf, newsize);
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!newbuf) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError,"out of memory");
|
|
|
|
return -1;
|
2013-02-09 07:47:43 -04:00
|
|
|
}
|
|
|
|
oself->buf_size = (Py_ssize_t)newsize;
|
2010-05-09 11:46:46 -03:00
|
|
|
oself->buf = newbuf;
|
2013-02-09 07:47:43 -04:00
|
|
|
}
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-10-11 16:13:04 -03:00
|
|
|
if (oself->string_size < oself->pos) {
|
|
|
|
/* In case of overseek, pad with null bytes the buffer region between
|
|
|
|
the end of stream and the current position.
|
|
|
|
|
|
|
|
0 lo string_size hi
|
|
|
|
| |<---used--->|<----------available----------->|
|
|
|
|
| | <--to pad-->|<---to write---> |
|
|
|
|
0 buf position
|
|
|
|
*/
|
|
|
|
memset(oself->buf + oself->string_size, '\0',
|
|
|
|
(oself->pos - oself->string_size) * sizeof(char));
|
|
|
|
}
|
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
memcpy(oself->buf + oself->pos, c, len);
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
oself->pos = newpos;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (oself->string_size < oself->pos) {
|
|
|
|
oself->string_size = oself->pos;
|
|
|
|
}
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-02-09 07:47:43 -04:00
|
|
|
return (int)len;
|
2000-10-06 16:24:23 -03:00
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2000-10-06 16:24:23 -03:00
|
|
|
O_write(Oobject *self, PyObject *args) {
|
2013-03-19 21:17:47 -03:00
|
|
|
Py_buffer buf;
|
|
|
|
int result;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-03-19 21:17:47 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s*:write", &buf)) return NULL;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2013-03-19 21:17:47 -03:00
|
|
|
result = O_cwrite((PyObject*)self, buf.buf, buf.len);
|
|
|
|
PyBuffer_Release(&buf);
|
|
|
|
if (result < 0) return NULL;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(O_close__doc__, "close(): explicitly release resources held.");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
O_close(Oobject *self, PyObject *unused) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->buf != NULL) free(self->buf);
|
|
|
|
self->buf = NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
self->pos = self->string_size = self->buf_size = 0;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(O_writelines__doc__,
|
2004-03-08 14:17:31 -04:00
|
|
|
"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
|
|
|
|
"\n"
|
|
|
|
"Note that newlines are not added. The sequence can be any iterable object\n"
|
|
|
|
"producing strings. This is equivalent to calling write() for each string.");
|
1996-12-05 19:30:48 -04:00
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_writelines(Oobject *self, PyObject *args) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *it, *s;
|
|
|
|
|
|
|
|
it = PyObject_GetIter(args);
|
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
while ((s = PyIter_Next(it)) != NULL) {
|
|
|
|
Py_ssize_t n;
|
|
|
|
char *c;
|
|
|
|
if (PyString_AsStringAndSize(s, &c, &n) == -1) {
|
|
|
|
Py_DECREF(it);
|
|
|
|
Py_DECREF(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (O_cwrite((PyObject *)self, c, n) == -1) {
|
|
|
|
Py_DECREF(it);
|
|
|
|
Py_DECREF(s);
|
|
|
|
return NULL;
|
2016-09-17 04:54:55 -03:00
|
|
|
}
|
|
|
|
Py_DECREF(s);
|
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2016-09-17 04:54:55 -03:00
|
|
|
Py_DECREF(it);
|
2005-09-22 06:19:01 -03:00
|
|
|
|
2016-09-17 04:54:55 -03:00
|
|
|
/* See if PyIter_Next failed */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2005-09-22 06:19:01 -03:00
|
|
|
|
2016-09-17 04:54:55 -03:00
|
|
|
Py_RETURN_NONE;
|
2005-09-22 06:19:01 -03:00
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
static struct PyMethodDef O_methods[] = {
|
2000-10-06 16:24:23 -03:00
|
|
|
/* Common methods: */
|
2003-01-03 04:24:58 -04:00
|
|
|
{"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
|
2000-10-06 16:24:23 -03:00
|
|
|
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
|
2003-01-03 04:24:58 -04:00
|
|
|
{"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
|
2010-05-09 11:46:46 -03:00
|
|
|
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
|
|
|
|
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
|
|
|
|
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
|
|
|
|
{"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
|
2010-10-11 16:13:04 -03:00
|
|
|
{"seek", (PyCFunction)IO_seek, METH_VARARGS, IO_seek__doc__},
|
2003-01-03 04:24:58 -04:00
|
|
|
{"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
|
2000-10-06 16:24:23 -03:00
|
|
|
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
|
|
|
|
|
|
|
|
/* Read-write StringIO specific methods: */
|
2003-01-03 04:24:58 -04:00
|
|
|
{"close", (PyCFunction)O_close, METH_NOARGS, O_close__doc__},
|
2010-05-09 11:46:46 -03:00
|
|
|
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
|
|
|
|
{"writelines", (PyCFunction)O_writelines, METH_O, O_writelines__doc__},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
2003-04-24 12:50:11 -03:00
|
|
|
static PyMemberDef O_memberlist[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"softspace", T_INT, offsetof(Oobject, softspace), 0,
|
|
|
|
"flag indicating that a space needs to be printed; used by print"},
|
|
|
|
/* getattr(f, "closed") is implemented without this table */
|
|
|
|
{NULL} /* Sentinel */
|
2003-04-24 12:50:11 -03:00
|
|
|
};
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static void
|
1997-04-09 14:35:33 -03:00
|
|
|
O_dealloc(Oobject *self) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->buf != NULL)
|
|
|
|
free(self->buf);
|
|
|
|
PyObject_Del(self);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(Otype__doc__, "Simple type for output to strings.");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyTypeObject Otype = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2010-05-09 11:46:46 -03:00
|
|
|
"cStringIO.StringO", /*tp_name*/
|
|
|
|
sizeof(Oobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
1997-04-09 14:35:33 -03:00
|
|
|
/* methods */
|
2010-05-09 11:46:46 -03:00
|
|
|
(destructor)O_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr */
|
|
|
|
0, /*tp_setattr */
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0 , /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro */
|
|
|
|
0, /*tp_setattro */
|
|
|
|
0, /*tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
|
|
Otype__doc__, /*tp_doc */
|
|
|
|
0, /*tp_traverse */
|
|
|
|
0, /*tp_clear */
|
|
|
|
0, /*tp_richcompare */
|
|
|
|
0, /*tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /*tp_iter */
|
|
|
|
(iternextfunc)IO_iternext, /*tp_iternext */
|
|
|
|
O_methods, /*tp_methods */
|
|
|
|
O_memberlist, /*tp_members */
|
|
|
|
file_getsetlist, /*tp_getset */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
static PyObject *
|
|
|
|
newOobject(int size) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Oobject *self;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
self = PyObject_New(Oobject, &Otype);
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->pos=0;
|
|
|
|
self->string_size = 0;
|
|
|
|
self->softspace = 0;
|
|
|
|
|
|
|
|
self->buf = (char *)malloc(size);
|
|
|
|
if (!self->buf) {
|
|
|
|
PyErr_SetString(PyExc_MemoryError,"out of memory");
|
|
|
|
self->buf_size = 0;
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->buf_size=size;
|
|
|
|
return (PyObject*)self;
|
1997-08-13 00:14:41 -03:00
|
|
|
}
|
|
|
|
|
2003-08-11 10:15:11 -03:00
|
|
|
/* End of code for StringO objects */
|
1996-12-05 19:30:48 -04:00
|
|
|
/* -------------------------------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
2003-01-03 04:24:58 -04:00
|
|
|
I_close(Iobject *self, PyObject *unused) {
|
2013-03-19 21:17:47 -03:00
|
|
|
PyBuffer_Release(&self->pbuf);
|
2010-05-09 11:46:46 -03:00
|
|
|
self->buf = NULL;
|
1998-12-15 17:43:15 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
self->pos = self->string_size = 0;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1998-12-15 17:43:15 -04:00
|
|
|
}
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static struct PyMethodDef I_methods[] = {
|
2000-10-06 16:24:23 -03:00
|
|
|
/* Common methods: */
|
2003-01-03 04:24:58 -04:00
|
|
|
{"flush", (PyCFunction)IO_flush, METH_NOARGS, IO_flush__doc__},
|
2000-10-06 16:24:23 -03:00
|
|
|
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
|
2003-01-03 04:24:58 -04:00
|
|
|
{"isatty", (PyCFunction)IO_isatty, METH_NOARGS, IO_isatty__doc__},
|
2010-05-09 11:46:46 -03:00
|
|
|
{"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
|
|
|
|
{"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
|
|
|
|
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
|
|
|
|
{"reset", (PyCFunction)IO_reset, METH_NOARGS, IO_reset__doc__},
|
2010-10-11 16:13:04 -03:00
|
|
|
{"seek", (PyCFunction)IO_seek, METH_VARARGS, IO_seek__doc__},
|
2003-01-03 04:24:58 -04:00
|
|
|
{"tell", (PyCFunction)IO_tell, METH_NOARGS, IO_tell__doc__},
|
2000-10-06 16:24:23 -03:00
|
|
|
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
|
|
|
|
|
|
|
|
/* Read-only StringIO specific methods: */
|
2003-01-03 04:24:58 -04:00
|
|
|
{"close", (PyCFunction)I_close, METH_NOARGS, O_close__doc__},
|
2010-05-09 11:46:46 -03:00
|
|
|
{NULL, NULL}
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1997-04-09 14:35:33 -03:00
|
|
|
I_dealloc(Iobject *self) {
|
2013-03-19 21:17:47 -03:00
|
|
|
PyBuffer_Release(&self->pbuf);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
2001-09-22 01:36:49 -03:00
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(Itype__doc__,
|
|
|
|
"Simple type for treating strings as input file streams");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyTypeObject Itype = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2010-05-09 11:46:46 -03:00
|
|
|
"cStringIO.StringI", /*tp_name*/
|
|
|
|
sizeof(Iobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
1997-04-09 14:35:33 -03:00
|
|
|
/* methods */
|
2010-05-09 11:46:46 -03:00
|
|
|
(destructor)I_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
Itype__doc__, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
(iternextfunc)IO_iternext, /* tp_iternext */
|
|
|
|
I_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
file_getsetlist, /* tp_getset */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
static PyObject *
|
|
|
|
newIobject(PyObject *s) {
|
|
|
|
Iobject *self;
|
2013-03-19 21:17:47 -03:00
|
|
|
Py_buffer buf;
|
|
|
|
PyObject *args;
|
|
|
|
int result;
|
2000-04-12 19:04:01 -03:00
|
|
|
|
2013-03-19 21:17:47 -03:00
|
|
|
args = Py_BuildValue("(O)", s);
|
|
|
|
if (args == NULL)
|
|
|
|
return NULL;
|
|
|
|
result = PyArg_ParseTuple(args, "s*:StringIO", &buf);
|
|
|
|
Py_DECREF(args);
|
|
|
|
if (!result)
|
2011-10-22 16:26:01 -03:00
|
|
|
return NULL;
|
2006-10-12 06:47:12 -03:00
|
|
|
|
2006-03-01 19:10:49 -04:00
|
|
|
self = PyObject_New(Iobject, &Itype);
|
2013-03-19 21:17:47 -03:00
|
|
|
if (!self) {
|
|
|
|
PyBuffer_Release(&buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
self->buf=buf.buf;
|
|
|
|
self->string_size=buf.len;
|
|
|
|
self->pbuf=buf;
|
1997-08-13 00:14:41 -03:00
|
|
|
self->pos=0;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
return (PyObject*)self;
|
|
|
|
}
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
/* End of code for StringI objects */
|
|
|
|
/* -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(IO_StringIO__doc__,
|
|
|
|
"StringIO([s]) -- Return a StringIO-like stream for reading or writing");
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
IO_StringIO(PyObject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject *s=0;
|
|
|
|
|
2003-01-03 04:24:58 -04:00
|
|
|
if (!PyArg_UnpackTuple(args, "StringIO", 0, 1, &s)) return NULL;
|
2000-10-06 16:24:23 -03:00
|
|
|
|
|
|
|
if (s) return newIobject(s);
|
1997-04-09 14:35:33 -03:00
|
|
|
return newOobject(128);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List of methods defined in the module */
|
|
|
|
|
|
|
|
static struct PyMethodDef IO_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"StringIO", (PyCFunction)IO_StringIO,
|
|
|
|
METH_VARARGS, IO_StringIO__doc__},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization function for the module (*must* be called initcStringIO) */
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
static struct PycStringIO_CAPI CAPI = {
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_cread,
|
|
|
|
IO_creadline,
|
1997-04-09 14:35:33 -03:00
|
|
|
O_cwrite,
|
2000-10-06 16:24:23 -03:00
|
|
|
IO_cgetval,
|
1997-04-09 14:35:33 -03:00
|
|
|
newOobject,
|
|
|
|
newIobject,
|
|
|
|
&Itype,
|
|
|
|
&Otype,
|
|
|
|
};
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
|
2002-08-01 23:27:13 -03:00
|
|
|
#define PyMODINIT_FUNC void
|
1998-12-15 17:43:15 -04:00
|
|
|
#endif
|
2002-08-01 23:27:13 -03:00
|
|
|
PyMODINIT_FUNC
|
2000-07-24 11:43:35 -03:00
|
|
|
initcStringIO(void) {
|
1997-09-03 15:19:40 -03:00
|
|
|
PyObject *m, *d, *v;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule4("cStringIO", IO_methods,
|
2010-05-09 11:46:46 -03:00
|
|
|
cStringIO_module_documentation,
|
|
|
|
(PyObject*)NULL,PYTHON_API_VERSION);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL) return;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
2010-05-09 11:46:46 -03:00
|
|
|
|
1997-01-06 18:57:52 -04:00
|
|
|
/* Export C API */
|
2007-12-18 22:37:44 -04:00
|
|
|
Py_TYPE(&Itype)=&PyType_Type;
|
|
|
|
Py_TYPE(&Otype)=&PyType_Type;
|
2003-04-24 12:50:11 -03:00
|
|
|
if (PyType_Ready(&Otype) < 0) return;
|
|
|
|
if (PyType_Ready(&Itype) < 0) return;
|
2010-03-24 21:54:54 -03:00
|
|
|
v = PyCapsule_New(&CAPI, PycStringIO_CAPSULE_NAME, NULL);
|
|
|
|
PyDict_SetItemString(d,"cStringIO_CAPI", v);
|
1997-09-03 15:19:40 -03:00
|
|
|
Py_XDECREF(v);
|
1997-04-09 14:35:33 -03:00
|
|
|
|
|
|
|
/* Export Types */
|
1996-12-05 19:30:48 -04:00
|
|
|
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
|
|
|
|
PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
|
1997-08-13 00:14:41 -03:00
|
|
|
|
|
|
|
/* Maybe make certain warnings go away */
|
2000-10-06 16:24:23 -03:00
|
|
|
if (0) PycString_IMPORT;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|