Jim's latest version.

This commit is contained in:
Guido van Rossum 1997-01-06 22:57:52 +00:00
parent 635abd24f0
commit 55702f8d6a

View file

@ -58,8 +58,24 @@
$Log$ $Log$
Revision 2.1 1996/12/05 23:30:40 guido Revision 2.2 1997/01/06 22:57:52 guido
Jim F's brainchild Jim's latest version.
Revision 1.10 1997/01/02 15:19:55 chris
checked in to be sure repository is up to date.
Revision 1.9 1996/12/27 21:40:29 jim
Took out some lamosities in interface, like returning self from
write.
Revision 1.8 1996/12/23 15:52:49 jim
Added ifdef to check for CObject before using it.
Revision 1.7 1996/12/23 15:22:35 jim
Finished implementation, adding full compatibility with StringIO, and
then some.
We still need to take out some cStringIO oddities.
Revision 1.6 1996/10/15 18:42:07 jim Revision 1.6 1996/10/15 18:42:07 jim
Added lots of casts to make warnings go away. Added lots of casts to make warnings go away.
@ -114,6 +130,14 @@ static char cStringIO_module_documentation[] =
static PyObject *ErrorObject; static PyObject *ErrorObject;
#ifdef __cplusplus
#define ARG(T,N) T N
#define ARGDECL(T,N)
#else
#define ARG(T,N) N
#define ARGDECL(T,N) T N;
#endif
#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E)) #define UNLESS(E) if(!(E))
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V) #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
@ -153,7 +177,9 @@ static char O_reset__doc__[] =
; ;
static PyObject * static PyObject *
O_reset(Oobject *self, PyObject *args) O_reset(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
self->pos = 0; self->pos = 0;
@ -166,7 +192,9 @@ static char O_tell__doc__[] =
"tell() -- get the current position."; "tell() -- get the current position.";
static PyObject * static PyObject *
O_tell(Oobject *self, PyObject *args) O_tell(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
return PyInt_FromLong(self->pos); return PyInt_FromLong(self->pos);
} }
@ -177,7 +205,9 @@ static char O_seek__doc__[] =
"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
static PyObject * static PyObject *
O_seek(Oobject *self, PyObject *args) O_seek(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
int position, mode = 0; int position, mode = 0;
@ -198,7 +228,8 @@ O_seek(Oobject *self, PyObject *args)
self->pos = (position > self->string_size ? self->string_size : self->pos = (position > self->string_size ? self->string_size :
(position < 0 ? 0 : position)); (position < 0 ? 0 : position));
return PyInt_FromLong(self->pos); Py_INCREF(Py_None);
return Py_None;
} }
static char O_read__doc__[] = static char O_read__doc__[] =
@ -206,7 +237,10 @@ static char O_read__doc__[] =
; ;
static int static int
O_cread(Oobject *self, char **output, int n) O_cread(ARG(Oobject*, self), ARG(char**, output), ARG(int, n))
ARGDECL(Oobject*, self)
ARGDECL(char**, output)
ARGDECL(int, n)
{ {
int l; int l;
@ -222,7 +256,9 @@ O_cread(Oobject *self, char **output, int n)
} }
static PyObject * static PyObject *
O_read(Oobject *self, PyObject *args) O_read(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
int n = -1; int n = -1;
char *output; char *output;
@ -240,7 +276,9 @@ static char O_readline__doc__[] =
; ;
static int static int
O_creadline(Oobject *self, char **output) O_creadline(ARG(Oobject*, self), ARG(char**, output))
ARGDECL(Oobject*, self)
ARGDECL(char**, output)
{ {
char *n, *s; char *n, *s;
int l; int l;
@ -256,7 +294,9 @@ O_creadline(Oobject *self, char **output)
} }
static PyObject * static PyObject *
O_readline(Oobject *self, PyObject *args) O_readline(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
int n; int n;
char *output; char *output;
@ -272,9 +312,15 @@ static char O_write__doc__[] =
static int static int
O_cwrite(Oobject *self, char *c, int l) O_cwrite(ARG(Oobject*, self), ARG(char*, c), ARG(int, l))
ARGDECL(Oobject*, self)
ARGDECL(char*, c)
ARGDECL(int, l)
{ {
int newl; PyObject *s;
char *b;
int newl, space_needed;
newl=self->pos+l; newl=self->pos+l;
if(newl > self->buf_size) if(newl > self->buf_size)
{ {
@ -301,40 +347,27 @@ O_cwrite(Oobject *self, char *c, int l)
} }
static PyObject * static PyObject *
O_write(Oobject *self, PyObject *args) O_write(ARG(Oobject*, self), ARG(PyObject*, args))
ARGDECL(Oobject*, self)
ARGDECL(PyObject*, args)
{ {
PyObject *s; PyObject *s;
char *c; char *c, *b;
int l; int l, newl, space_needed;
UNLESS(PyArg_Parse(args, "O", &s)) return NULL; UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
if(s!=Py_None) UNLESS(-1 != (l=PyString_Size(s))) return NULL;
{ UNLESS(c=PyString_AsString(s)) return NULL;
UNLESS(-1 != (l=PyString_Size(s))) return NULL; UNLESS(-1 != O_cwrite(self,c,l)) return NULL;
UNLESS(c=PyString_AsString(s)) return NULL;
UNLESS(-1 != O_cwrite(self,c,l)) return NULL;
}
else
{
self->pos=0;
self->string_size = 0;
}
Py_INCREF(self); Py_INCREF(Py_None);
return (PyObject *)self; return Py_None;
} }
static PyObject * static PyObject *
O_repr(self) O_getval(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
{ ARGDECL(PyObject*, args)
return PyString_FromStringAndSize(self->buf,self->string_size);
}
static PyObject *
O_getval(self,args)
Oobject *self;
PyObject *args;
{ {
return PyString_FromStringAndSize(self->buf,self->pos); return PyString_FromStringAndSize(self->buf,self->pos);
} }
@ -343,9 +376,9 @@ static char O_truncate__doc__[] =
"truncate(): truncate the file at the current position."; "truncate(): truncate the file at the current position.";
static PyObject * static PyObject *
O_truncate(self, args) O_truncate(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
self->string_size = self->pos; self->string_size = self->pos;
Py_INCREF(Py_None); Py_INCREF(Py_None);
@ -355,9 +388,9 @@ O_truncate(self, args)
static char O_isatty__doc__[] = "isatty(): always returns 0"; static char O_isatty__doc__[] = "isatty(): always returns 0";
static PyObject * static PyObject *
O_isatty(self, args) O_isatty(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
return PyInt_FromLong(0); return PyInt_FromLong(0);
} }
@ -365,9 +398,9 @@ O_isatty(self, args)
static char O_close__doc__[] = "close(): explicitly release resources held."; static char O_close__doc__[] = "close(): explicitly release resources held.";
static PyObject * static PyObject *
O_close(self, args) O_close(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
free(self->buf); free(self->buf);
@ -381,9 +414,9 @@ O_close(self, args)
static char O_flush__doc__[] = "flush(): does nothing."; static char O_flush__doc__[] = "flush(): does nothing.";
static PyObject * static PyObject *
O_flush(self, args) O_flush(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
@ -392,9 +425,9 @@ O_flush(self, args)
static char O_writelines__doc__[] = "blah"; static char O_writelines__doc__[] = "blah";
static PyObject * static PyObject *
O_writelines(self, args) O_writelines(ARG(Oobject*, self), ARG(PyObject*, args))
Oobject *self; ARGDECL(Oobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
PyObject *string_module = 0; PyObject *string_module = 0;
static PyObject *string_joinfields = 0; static PyObject *string_joinfields = 0;
@ -449,7 +482,8 @@ static struct PyMethodDef O_methods[] = {
static Oobject * static Oobject *
newOobject(int size) newOobject(ARG(int, size))
ARGDECL(int, size)
{ {
Oobject *self; Oobject *self;
@ -473,17 +507,17 @@ newOobject(int size)
static void static void
O_dealloc(self) O_dealloc(ARG(Oobject*, self))
Oobject *self; ARGDECL(Oobject*, self)
{ {
free(self->buf); free(self->buf);
PyMem_DEL(self); PyMem_DEL(self);
} }
static PyObject * static PyObject *
O_getattr(self, name) O_getattr(ARG(Oobject*, self), ARG(char*, name))
Oobject *self; ARGDECL(Oobject*, self)
char *name; ARGDECL(char*, name)
{ {
return Py_FindMethod(O_methods, (PyObject *)self, name); return Py_FindMethod(O_methods, (PyObject *)self, name);
} }
@ -494,36 +528,36 @@ static char Otype__doc__[] =
static PyTypeObject Otype = { static PyTypeObject Otype = {
PyObject_HEAD_INIT(&PyType_Type) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"StringO", /*tp_name*/ "StringO", /*tp_name*/
sizeof(Oobject), /*tp_basicsize*/ sizeof(Oobject), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)O_dealloc, /*tp_dealloc*/ (destructor)O_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/ (printfunc)0, /*tp_print*/
(getattrfunc)O_getattr, /*tp_getattr*/ (getattrfunc)O_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/ (setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/ (cmpfunc)0, /*tp_compare*/
(reprfunc)O_repr, /*tp_repr*/ (reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
0, /*tp_as_sequence*/ 0, /*tp_as_sequence*/
0, /*tp_as_mapping*/ 0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/ (hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/ (ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/ (reprfunc)0, /*tp_str*/
/* Space for future expansion */ /* Space for future expansion */
0L,0L,0L,0L, 0L,0L,0L,0L,
Otype__doc__ /* Documentation string */ Otype__doc__ /* Documentation string */
}; };
/* End of code for StringO objects */ /* End of code for StringO objects */
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
static PyObject * static PyObject *
I_close(self, args) I_close(ARG(Iobject*, self), ARG(PyObject*, args))
Iobject *self; ARGDECL(Iobject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
Py_DECREF(self->pbuf); Py_DECREF(self->pbuf);
@ -551,7 +585,8 @@ static struct PyMethodDef I_methods[] = {
static Iobject * static Iobject *
newIobject(PyObject *s) newIobject(ARG(PyObject*, s))
ARGDECL(PyObject*, s)
{ {
Iobject *self; Iobject *self;
char *buf; char *buf;
@ -572,17 +607,17 @@ newIobject(PyObject *s)
static void static void
I_dealloc(self) I_dealloc(ARG(Iobject*, self))
Iobject *self; ARGDECL(Iobject*, self)
{ {
Py_DECREF(self->pbuf); Py_DECREF(self->pbuf);
PyMem_DEL(self); PyMem_DEL(self);
} }
static PyObject * static PyObject *
I_getattr(self, name) I_getattr(ARG(Iobject*, self), ARG(char*, name))
Iobject *self; ARGDECL(Iobject*, self)
char *name; ARGDECL(char*, name)
{ {
return Py_FindMethod(I_methods, (PyObject *)self, name); return Py_FindMethod(I_methods, (PyObject *)self, name);
} }
@ -593,27 +628,27 @@ static char Itype__doc__[] =
static PyTypeObject Itype = { static PyTypeObject Itype = {
PyObject_HEAD_INIT(&PyType_Type) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"StringI", /*tp_name*/ "StringI", /*tp_name*/
sizeof(Iobject), /*tp_basicsize*/ sizeof(Iobject), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)I_dealloc, /*tp_dealloc*/ (destructor)I_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/ (printfunc)0, /*tp_print*/
(getattrfunc)I_getattr, /*tp_getattr*/ (getattrfunc)I_getattr, /*tp_getattr*/
(setattrfunc)0, /*tp_setattr*/ (setattrfunc)0, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/ (cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/ (reprfunc)0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
0, /*tp_as_sequence*/ 0, /*tp_as_sequence*/
0, /*tp_as_mapping*/ 0, /*tp_as_mapping*/
(hashfunc)0, /*tp_hash*/ (hashfunc)0, /*tp_hash*/
(ternaryfunc)0, /*tp_call*/ (ternaryfunc)0, /*tp_call*/
(reprfunc)0, /*tp_str*/ (reprfunc)0, /*tp_str*/
/* Space for future expansion */ /* Space for future expansion */
0L,0L,0L,0L, 0L,0L,0L,0L,
Itype__doc__ /* Documentation string */ Itype__doc__ /* Documentation string */
}; };
/* End of code for StringI objects */ /* End of code for StringI objects */
@ -625,9 +660,9 @@ static char IO_StringIO__doc__[] =
; ;
static PyObject * static PyObject *
IO_StringIO(self, args) IO_StringIO(ARG(PyObject*, self), ARG(PyObject*, args))
PyObject *self; /* Not used */ ARGDECL(PyObject*, self)
PyObject *args; ARGDECL(PyObject*, args)
{ {
PyObject *s=0; PyObject *s=0;
@ -639,8 +674,8 @@ IO_StringIO(self, args)
/* List of methods defined in the module */ /* List of methods defined in the module */
static struct PyMethodDef IO_methods[] = { static struct PyMethodDef IO_methods[] = {
{"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
@ -661,12 +696,15 @@ initcStringIO()
ErrorObject = PyString_FromString("cStringIO.error"); ErrorObject = PyString_FromString("cStringIO.error");
PyDict_SetItemString(d, "error", ErrorObject); PyDict_SetItemString(d, "error", ErrorObject);
#ifdef Py_COBJECT_H
/* Export C API */
PyDict_SetItemString(d,"cread", PyCObject_FromVoidPtr(O_cread,NULL)); PyDict_SetItemString(d,"cread", PyCObject_FromVoidPtr(O_cread,NULL));
PyDict_SetItemString(d,"creadline", PyCObject_FromVoidPtr(O_creadline,NULL)); PyDict_SetItemString(d,"creadline", PyCObject_FromVoidPtr(O_creadline,NULL));
PyDict_SetItemString(d,"cwrite", PyCObject_FromVoidPtr(O_cwrite,NULL)); PyDict_SetItemString(d,"cwrite", PyCObject_FromVoidPtr(O_cwrite,NULL));
PyDict_SetItemString(d,"cgetvalue", PyCObject_FromVoidPtr(O_getval,NULL)); PyDict_SetItemString(d,"cgetvalue", PyCObject_FromVoidPtr(O_getval,NULL));
PyDict_SetItemString(d,"NewInput", PyCObject_FromVoidPtr(newIobject,NULL)); PyDict_SetItemString(d,"NewInput", PyCObject_FromVoidPtr(newIobject,NULL));
PyDict_SetItemString(d,"NewOutput", PyCObject_FromVoidPtr(newOobject,NULL)); PyDict_SetItemString(d,"NewOutput", PyCObject_FromVoidPtr(newOobject,NULL));
#endif
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
@ -676,3 +714,4 @@ initcStringIO()
if (PyErr_Occurred()) if (PyErr_Occurred())
Py_FatalError("can't initialize module cStringIO"); Py_FatalError("can't initialize module cStringIO");
} }