📄 cstringio.c
字号:
self->pos = self->string_size = self->buf_size = 0;
Py_INCREF(Py_None);
return Py_None;
}
#ifndef SYMBIAN
static char O_writelines__doc__[] =
"writelines(sequence_of_strings): write each string";
#endif
static PyObject *
O_writelines(Oobject *self, PyObject *args) {
PyObject *tmp = 0;
#ifndef SYMBIAN
static PyObject *joiner = NULL;
#endif
UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
#ifndef SYMBIAN
if (!joiner) {
PyObject *empty_string = PyString_FromString("");
if (empty_string == NULL)
return NULL;
joiner = PyObject_GetAttrString(empty_string, "join");
Py_DECREF(empty_string);
if (joiner == NULL)
return NULL;
}
#endif
if (PyObject_Size(args) < 0) return NULL;
#ifndef SYMBIAN
tmp = PyObject_CallFunction(joiner, "O", args);
#else
tmp = PyObject_CallFunction(_mod_dict_get_s("_joiner"), "O", args);
#endif
UNLESS (tmp) return NULL;
args = Py_BuildValue("(O)", tmp);
Py_DECREF(tmp);
UNLESS (args) return NULL;
tmp = O_write(self, args);
Py_DECREF(args);
return tmp;
}
#ifndef SYMBIAN
static struct PyMethodDef O_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"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_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-write StringIO specific methods: */
{"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
{"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
{"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
{NULL, NULL} /* sentinel */
};
#else
const static struct PyMethodDef O_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, NULL},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, NULL},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, NULL},
{"read", (PyCFunction)IO_read, METH_VARARGS, NULL},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, NULL},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, NULL},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, NULL},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, NULL},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, NULL},
/* Read-write StringIO specific methods: */
{"close", (PyCFunction)O_close, METH_VARARGS, NULL},
{"seek", (PyCFunction)O_seek, METH_VARARGS, NULL},
{"write", (PyCFunction)O_write, METH_VARARGS, NULL},
{"writelines", (PyCFunction)O_writelines, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};
#endif /* SYMBIAN */
static void
O_dealloc(Oobject *self) {
if (self->buf != NULL)
free(self->buf);
PyObject_Del(self);
}
static PyObject *
O_getattr(Oobject *self, char *name) {
if (strcmp(name, "softspace") == 0) {
return PyInt_FromLong(self->softspace);
}
return Py_FindMethod(O_methods, (PyObject *)self, name);
}
static int
O_setattr(Oobject *self, char *name, PyObject *value) {
long x;
if (strcmp(name, "softspace") != 0) {
PyErr_SetString(PyExc_AttributeError, name);
return -1;
}
x = PyInt_AsLong(value);
if (x < 0 && PyErr_Occurred())
return -1;
self->softspace = x;
return 0;
}
#ifndef SYMBIAN
static char Otype__doc__[] =
"Simple type for output to strings."
;
#endif
#ifndef SYMBIAN
static PyTypeObject Otype = {
#else
const static PyTypeObject c_Otype = {
#endif
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"cStringIO.StringO", /*tp_name*/
sizeof(Oobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)O_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)O_getattr, /*tp_getattr*/
(setattrfunc)O_setattr, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
/* Space for future expansion */
0L,0L,0L,0L,
#ifndef SYMBIAN
Otype__doc__ /* Documentation string */
#else
NULL
#endif
};
static PyObject *
newOobject(int size) {
Oobject *self;
#ifndef SYMBIAN
self = PyObject_New(Oobject, &Otype);
#else
// XXX:CW32
self = PyObject_New(Oobject, (PyTypeObject *)_mod_dict_get_s("OutputType"));
#endif
if (self == NULL)
return NULL;
self->pos=0;
self->string_size = 0;
self->softspace = 0;
UNLESS (self->buf=malloc(size*sizeof(char))) {
PyErr_SetString(PyExc_MemoryError,"out of memory");
self->buf_size = 0;
return NULL;
}
self->buf_size=size;
return (PyObject*)self;
}
/* End of code for StringO objects */
/* -------------------------------------------------------- */
static PyObject *
I_close(Iobject *self, PyObject *args) {
UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Py_XDECREF(self->pbuf);
self->pbuf = NULL;
self->buf = NULL;
self->pos = self->string_size = 0;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
I_seek(Iobject *self, PyObject *args) {
int position, mode = 0;
UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
return NULL;
if (mode == 2) position += self->string_size;
else if (mode == 1) position += self->pos;
if (position < 0) position=0;
self->pos=position;
Py_INCREF(Py_None);
return Py_None;
}
#ifndef SYMBIAN
static struct PyMethodDef I_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
{"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_VARARGS, IO_reset__doc__},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
/* Read-only StringIO specific methods: */
{"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
{"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
{NULL, NULL}
};
#else
const static struct PyMethodDef I_methods[] = {
/* Common methods: */
{"flush", (PyCFunction)IO_flush, METH_VARARGS, NULL},
{"getvalue", (PyCFunction)IO_getval, METH_VARARGS, NULL},
{"isatty", (PyCFunction)IO_isatty, METH_VARARGS, NULL},
{"read", (PyCFunction)IO_read, METH_VARARGS, NULL},
{"readline", (PyCFunction)IO_readline, METH_VARARGS, NULL},
{"readlines", (PyCFunction)IO_readlines,METH_VARARGS, NULL},
{"reset", (PyCFunction)IO_reset, METH_VARARGS, NULL},
{"tell", (PyCFunction)IO_tell, METH_VARARGS, NULL},
{"truncate", (PyCFunction)IO_truncate, METH_VARARGS, NULL},
/* Read-only StringIO specific methods: */
{"close", (PyCFunction)I_close, METH_VARARGS, NULL},
{"seek", (PyCFunction)I_seek, METH_VARARGS, NULL},
{NULL, NULL}
};
#endif /* SYMBIAN */
static void
I_dealloc(Iobject *self) {
Py_XDECREF(self->pbuf);
PyObject_Del(self);
}
static PyObject *
I_getattr(Iobject *self, char *name) {
return Py_FindMethod(I_methods, (PyObject *)self, name);
}
static PyObject *
I_getiter(Iobject *self)
{
PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
"readline");
PyObject *emptystring = PyString_FromString("");
PyObject *iter = NULL;
if (!myreadline || !emptystring)
goto finally;
iter = PyCallIter_New(myreadline, emptystring);
finally:
Py_XDECREF(myreadline);
Py_XDECREF(emptystring);
return iter;
}
#ifndef SYMBIAN
static char Itype__doc__[] =
"Simple type for treating strings as input file streams"
;
#endif
#ifndef SYMBIAN
static PyTypeObject Itype = {
#else
const static PyTypeObject c_Itype = {
#endif
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"cStringIO.StringI", /*tp_name*/
sizeof(Iobject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)I_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)I_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
#ifndef SYMBIAN
Itype__doc__, /* tp_doc */
#else
NULL, /* tp_doc */
#endif
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)I_getiter, /* tp_iter */
0, /* tp_iternext */
};
static PyObject *
newIobject(PyObject *s) {
Iobject *self;
char *buf;
int size;
if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
s->ob_type->tp_name);
return NULL;
}
#ifndef SYMBIAN
UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
#else
// XXX:CW32
UNLESS (self = PyObject_New(Iobject, (PyTypeObject *)_mod_dict_get_s("InputType"))) return NULL;
#endif
Py_INCREF(s);
self->buf=buf;
self->string_size=size;
self->pbuf=s;
self->pos=0;
return (PyObject*)self;
}
/* End of code for StringI objects */
/* -------------------------------------------------------- */
#ifndef SYMBIAN
static char IO_StringIO__doc__[] =
"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
;
#endif
static PyObject *
IO_StringIO(PyObject *self, PyObject *args) {
PyObject *s=0;
if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
if (s) return newIobject(s);
return newOobject(128);
}
/* List of methods defined in the module */
#ifndef SYMBIAN
static struct PyMethodDef IO_methods[] = {
{"StringIO", (PyCFunction)IO_StringIO,
METH_VARARGS, IO_StringIO__doc__},
{NULL, NULL} /* sentinel */
};
#else
const static struct PyMethodDef IO_methods[] = {
{"StringIO", (PyCFunction)IO_StringIO,
METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};
#endif
/* Initialization function for the module (*must* be called initcStringIO) */
#ifndef SYMBIAN
static struct PycStringIO_CAPI CAPI = {
IO_cread,
IO_creadline,
O_cwrite,
IO_cgetval,
newOobject,
newIobject,
&Itype,
&Otype,
};
#endif /* not SYMBIAN */
#ifndef DL_EXPORT /* declarations for DLL import/export */
#define DL_EXPORT(RTYPE) RTYPE
#endif
DL_EXPORT(void)
initcStringIO(void) {
PyObject *m, *d, *v;
#ifdef SYMBIAN
PyTypeObject *_Otype, *_Itype;
struct PycStringIO_CAPI CAPI = {
IO_cread,
IO_creadline,
O_cwrite,
IO_cgetval,
newOobject,
newIobject,
NULL,
NULL,
};
#endif
/* Create the module and add the functions */
m = Py_InitModule4("cStringIO", IO_methods,
cStringIO_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
/* Export C API */
#ifdef SYMBIAN
_Otype = PyObject_New(PyTypeObject, &PyType_Type);
*_Otype = c_Otype;
_Otype->ob_type = &PyType_Type;
_Itype = PyObject_New(PyTypeObject, &PyType_Type);
*_Itype = c_Itype;
_Itype->ob_type = &PyType_Type;
CAPI.InputType = _Itype;
CAPI.OutputType = _Otype;
#else
Itype.ob_type=&PyType_Type;
Otype.ob_type=&PyType_Type;
#endif
PyDict_SetItemString(d,"cStringIO_CAPI",
v = PyCObject_FromVoidPtr(&CAPI,NULL));
Py_XDECREF(v);
/* Export Types */
#ifdef SYMBIAN
PyDict_SetItemString(d,"InputType", (PyObject*)_Itype);
PyDict_SetItemString(d,"OutputType", (PyObject*)_Otype);
#else
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
#endif
#ifdef SYMBIAN
{
PyObject *empty_string = PyString_FromString("");
PyObject *joiner = PyObject_GetAttrString(empty_string, "join");
Py_DECREF(empty_string);
PyDict_SetItemString(d,"_joiner", joiner);
}
#endif
/* Maybe make certain warnings go away */
if (0) PycString_IMPORT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -