Commit 9420effc authored by Jim Fulton's avatar Jim Fulton

Various Guido-fications.

parent 40adf1a0
/* /*
$Id: cStringIO.c,v 1.22 1997/09/04 19:51:03 jim Exp $ $Id: cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp $
A simple fast partial StringIO replacement. A simple fast partial StringIO replacement.
...@@ -85,7 +85,7 @@ static char cStringIO_module_documentation[] = ...@@ -85,7 +85,7 @@ static char cStringIO_module_documentation[] =
"If someone else wants to provide a more complete implementation,\n" "If someone else wants to provide a more complete implementation,\n"
"go for it. :-) \n" "go for it. :-) \n"
"\n" "\n"
"$Id: cStringIO.c,v 1.22 1997/09/04 19:51:03 jim Exp $\n" "$Id: cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp $\n"
; ;
#include "Python.h" #include "Python.h"
...@@ -141,17 +141,14 @@ static PyObject * ...@@ -141,17 +141,14 @@ static PyObject *
O_seek(Oobject *self, PyObject *args) { O_seek(Oobject *self, PyObject *args) {
int position, mode = 0; int position, mode = 0;
UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) {
{
return NULL; return NULL;
} }
if (mode == 2) if (mode == 2) {
{
position += self->string_size; position += self->string_size;
} }
else if (mode == 1) else if (mode == 1) {
{
position += self->pos; position += self->pos;
} }
...@@ -171,8 +168,7 @@ O_cread(PyObject *self, char **output, int n) { ...@@ -171,8 +168,7 @@ O_cread(PyObject *self, char **output, int n) {
int l; int l;
l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos; l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
if (n < 0 || n > l) if (n < 0 || n > l) {
{
n = l; n = l;
} }
...@@ -234,14 +230,12 @@ O_cwrite(PyObject *self, char *c, int l) { ...@@ -234,14 +230,12 @@ O_cwrite(PyObject *self, char *c, int l) {
int newl; int newl;
newl=((Oobject*)self)->pos+l; newl=((Oobject*)self)->pos+l;
if(newl >= ((Oobject*)self)->buf_size) if(newl >= ((Oobject*)self)->buf_size) {
{
((Oobject*)self)->buf_size*=2; ((Oobject*)self)->buf_size*=2;
if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1; if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
UNLESS(((Oobject*)self)->buf= UNLESS(((Oobject*)self)->buf=
(char*)realloc(((Oobject*)self)->buf, (char*)realloc(((Oobject*)self)->buf,
(((Oobject*)self)->buf_size) *sizeof(char))) (((Oobject*)self)->buf_size) *sizeof(char))) {
{
PyErr_SetString(PyExc_MemoryError,"out of memory"); PyErr_SetString(PyExc_MemoryError,"out of memory");
((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
return -1; return -1;
...@@ -252,8 +246,7 @@ O_cwrite(PyObject *self, char *c, int l) { ...@@ -252,8 +246,7 @@ O_cwrite(PyObject *self, char *c, int l) {
((Oobject*)self)->pos += l; ((Oobject*)self)->pos += l;
if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
{
((Oobject*)self)->string_size = ((Oobject*)self)->pos; ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
} }
...@@ -314,10 +307,9 @@ static char O_close__doc__[] = "close(): explicitly release resources held."; ...@@ -314,10 +307,9 @@ static char O_close__doc__[] = "close(): explicitly release resources held.";
static PyObject * static PyObject *
O_close(Oobject *self, PyObject *args) { O_close(Oobject *self, PyObject *args) {
if(self->buf) { if (self->buf != NULL)
free(self->buf); free(self->buf);
self->buf=NULL; self->buf = NULL;
}
self->pos = self->string_size = self->buf_size = 0; self->pos = self->string_size = self->buf_size = 0;
self->closed = 1; self->closed = 1;
...@@ -341,29 +333,24 @@ O_writelines(Oobject *self, PyObject *args) { ...@@ -341,29 +333,24 @@ O_writelines(Oobject *self, PyObject *args) {
PyObject *string_module = 0; PyObject *string_module = 0;
static PyObject *string_joinfields = 0; static PyObject *string_joinfields = 0;
UNLESS(PyArg_Parse(args, "O", args)) UNLESS(PyArg_Parse(args, "O", args)) {
{
return NULL; return NULL;
} }
if (!string_joinfields) if (!string_joinfields) {
{ UNLESS(string_module = PyImport_ImportModule("string")) {
UNLESS(string_module = PyImport_ImportModule("string"))
{
return NULL; return NULL;
} }
UNLESS(string_joinfields= UNLESS(string_joinfields=
PyObject_GetAttrString(string_module, "joinfields")) PyObject_GetAttrString(string_module, "joinfields")) {
{
return NULL; return NULL;
} }
Py_DECREF(string_module); Py_DECREF(string_module);
} }
if (PyObject_Length(args) == -1) if (PyObject_Length(args) == -1) {
{
return NULL; return NULL;
} }
...@@ -395,7 +382,8 @@ static struct PyMethodDef O_methods[] = { ...@@ -395,7 +382,8 @@ static struct PyMethodDef O_methods[] = {
static void static void
O_dealloc(Oobject *self) { O_dealloc(Oobject *self) {
if(self->buf) free(self->buf); if (self->buf != NULL)
free(self->buf);
PyMem_DEL(self); PyMem_DEL(self);
} }
...@@ -462,8 +450,7 @@ newOobject(int size) { ...@@ -462,8 +450,7 @@ newOobject(int size) {
self->string_size = 0; self->string_size = 0;
self->softspace = 0; self->softspace = 0;
UNLESS(self->buf=malloc(size*sizeof(char))) UNLESS(self->buf=malloc(size*sizeof(char))) {
{
PyErr_SetString(PyExc_MemoryError,"out of memory"); PyErr_SetString(PyExc_MemoryError,"out of memory");
self->buf_size = 0; self->buf_size = 0;
return NULL; return NULL;
...@@ -478,11 +465,8 @@ newOobject(int size) { ...@@ -478,11 +465,8 @@ newOobject(int size) {
static PyObject * static PyObject *
I_close(Iobject *self, PyObject *args) { I_close(Iobject *self, PyObject *args) {
if(self->pbuf) Py_XDECREF(self->pbuf);
{ self->pbuf = NULL;
Py_DECREF(self->pbuf);
self->pbuf=0;
}
self->pos = self->string_size = 0; self->pos = self->string_size = 0;
self->closed = 1; self->closed = 1;
...@@ -603,7 +587,7 @@ static struct PycStringIO_CAPI CAPI = { ...@@ -603,7 +587,7 @@ static struct PycStringIO_CAPI CAPI = {
void void
initcStringIO() { initcStringIO() {
PyObject *m, *d; PyObject *m, *d, *v;
/* Create the module and add the functions */ /* Create the module and add the functions */
...@@ -617,7 +601,9 @@ initcStringIO() { ...@@ -617,7 +601,9 @@ initcStringIO() {
/* Export C API */ /* Export C API */
Itype.ob_type=&PyType_Type; Itype.ob_type=&PyType_Type;
Otype.ob_type=&PyType_Type; Otype.ob_type=&PyType_Type;
PyDict_SetItemString(d,"cStringIO_CAPI", PyCObject_FromVoidPtr(&CAPI,NULL)); PyDict_SetItemString(d,"cStringIO_CAPI",
v = PyCObject_FromVoidPtr(&CAPI,NULL));
Py_XDECREF(v);
/* Export Types */ /* Export Types */
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
...@@ -629,60 +615,3 @@ initcStringIO() { ...@@ -629,60 +615,3 @@ initcStringIO() {
/* Check for errors */ /* Check for errors */
if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO");
} }
/******************************************************************************
$Log: cStringIO.c,v $
Revision 1.22 1997/09/04 19:51:03 jim
Fixed bug in close/dealloc.
Revision 1.21 1997/06/19 18:51:42 jim
Added ident string.
Revision 1.20 1997/06/13 20:50:50 jim
- Various changes to make gcc -Wall -pedantic happy, including
getting rid of staticforward declarations and adding pretend use
of two statics defined in .h file.
Revision 1.19 1997/06/02 18:15:17 jim
Merged in guido's changes.
Revision 1.18 1997/05/07 16:26:47 jim
getvalue() can nor be given an argument. If this argument is true,
then getvalue returns the text upto the current position. Otherwise
it returns all of the text. The default value of the argument is
false.
Revision 1.17 1997/04/17 18:02:46 chris
getvalue() now returns entire string, not just the string up to
current position
Revision 2.5 1997/04/11 19:56:06 guido
My own patch: support writable 'softspace' attribute.
> Jim asked: What is softspace for?
It's an old feature. The print statement uses this to remember
whether it should insert a space before the next item or not.
Implementation is in fileobject.c.
Revision 1.11 1997/01/23 20:45:01 jim
ANSIfied it.
Changed way C API was exported.
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.
*****************************************************************************/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment