Commit d2724aca authored by Jim Fulton's avatar Jim Fulton

Fixed a number of bugs. These had to do with

unusual cases related to weird seeks.  There was also
a case where an optional argument (to O_getval) was not
properly initialized.
parent 14668331
/* /*
* $Id: cStringIO.c,v 1.26 1998/10/01 22:30:56 jim Exp $ * $Id: cStringIO.c,v 1.27 1998/12/15 20:32:13 jim Exp $
* *
* Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA. * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
* All rights reserved. * All rights reserved.
...@@ -78,7 +78,7 @@ static char cStringIO_module_documentation[] = ...@@ -78,7 +78,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.26 1998/10/01 22:30:56 jim Exp $\n" "$Id: cStringIO.c,v 1.27 1998/12/15 20:32:13 jim Exp $\n"
; ;
#include "Python.h" #include "Python.h"
...@@ -92,7 +92,7 @@ static char cStringIO_module_documentation[] = ...@@ -92,7 +92,7 @@ static char cStringIO_module_documentation[] =
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
char *buf; char *buf;
int pos, string_size, buf_size, closed, softspace; int pos, string_size, buf_size, softspace;
} Oobject; } Oobject;
/* Declarations for objects of type StringI */ /* Declarations for objects of type StringI */
...@@ -100,7 +100,7 @@ typedef struct { ...@@ -100,7 +100,7 @@ typedef struct {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
char *buf; char *buf;
int pos, string_size, closed; int pos, string_size;
PyObject *pbuf; PyObject *pbuf;
} Iobject; } Iobject;
...@@ -174,6 +174,7 @@ O_cread(PyObject *self, char **output, int n) { ...@@ -174,6 +174,7 @@ O_cread(PyObject *self, char **output, int n) {
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;
if (n < 0) n=0;
} }
*output=((Oobject*)self)->buf + ((Oobject*)self)->pos; *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
...@@ -263,7 +264,7 @@ O_write(Oobject *self, PyObject *args) { ...@@ -263,7 +264,7 @@ O_write(Oobject *self, PyObject *args) {
char *c; char *c;
int l; int l;
UNLESS(PyArg_Parse(args, "O", &s)) return NULL; UNLESS(PyArg_ParseTuple(args, "O", &s)) return NULL;
UNLESS(-1 != (l=PyString_Size(s))) return NULL; UNLESS(-1 != (l=PyString_Size(s))) return NULL;
UNLESS(c=PyString_AsString(s)) return NULL; UNLESS(c=PyString_AsString(s)) return NULL;
UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
...@@ -272,15 +273,26 @@ O_write(Oobject *self, PyObject *args) { ...@@ -272,15 +273,26 @@ O_write(Oobject *self, PyObject *args) {
return Py_None; return Py_None;
} }
static char O_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"
;
static PyObject * static PyObject *
O_getval(Oobject *self, PyObject *args) { O_getval(Oobject *self, PyObject *args) {
PyObject *use_pos; PyObject *use_pos=Py_None;
int s; int s;
use_pos=Py_None; use_pos=Py_None;
UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL; UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL;
if(PyObject_IsTrue(use_pos)) s=self->pos; if(PyObject_IsTrue(use_pos)) {
else s=self->string_size; s=self->pos;
if (s > self->string_size) s=self->string_size;
}
else
s=self->string_size;
return PyString_FromStringAndSize(self->buf, s); return PyString_FromStringAndSize(self->buf, s);
} }
...@@ -295,7 +307,8 @@ static char O_truncate__doc__[] = ...@@ -295,7 +307,8 @@ static char O_truncate__doc__[] =
static PyObject * static PyObject *
O_truncate(Oobject *self, PyObject *args) { O_truncate(Oobject *self, PyObject *args) {
self->string_size = self->pos; if (self->string_size > self->pos)
self->string_size = self->pos;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
...@@ -316,7 +329,6 @@ O_close(Oobject *self, PyObject *args) { ...@@ -316,7 +329,6 @@ O_close(Oobject *self, PyObject *args) {
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;
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
...@@ -337,7 +349,7 @@ O_writelines(Oobject *self, PyObject *args) { ...@@ -337,7 +349,7 @@ 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_ParseTuple(args, "O", args)) {
return NULL; return NULL;
} }
...@@ -363,24 +375,19 @@ O_writelines(Oobject *self, PyObject *args) { ...@@ -363,24 +375,19 @@ O_writelines(Oobject *self, PyObject *args) {
} }
static struct PyMethodDef O_methods[] = { static struct PyMethodDef O_methods[] = {
{"write", (PyCFunction)O_write, 0, O_write__doc__}, {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
{"read", (PyCFunction)O_read, 1, O_read__doc__}, {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
{"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
{"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
{"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
{"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
{"getvalue", (PyCFunction)O_getval, 1, {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
"getvalue([use_pos]) -- Get the string value." {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
"\n" {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
"If use_pos is specified and is a true value, then the string returned\n" {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
"will include only the text up to the current file position.\n" {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
}, {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
{"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, {NULL, NULL} /* sentinel */
{"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
{"close", (PyCFunction)O_close, 0, O_close__doc__},
{"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
{"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__},
{NULL, NULL} /* sentinel */
}; };
...@@ -450,7 +457,6 @@ newOobject(int size) { ...@@ -450,7 +457,6 @@ newOobject(int size) {
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->pos=0; self->pos=0;
self->closed = 0;
self->string_size = 0; self->string_size = 0;
self->softspace = 0; self->softspace = 0;
...@@ -473,23 +479,46 @@ I_close(Iobject *self, PyObject *args) { ...@@ -473,23 +479,46 @@ I_close(Iobject *self, PyObject *args) {
self->pbuf = NULL; self->pbuf = NULL;
self->pos = self->string_size = 0; self->pos = self->string_size = 0;
self->closed = 1;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
I_seek(Oobject *self, PyObject *args) {
int position, mode = 0;
UNLESS(PyArg_ParseTuple(args, "i|i", &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); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static struct PyMethodDef I_methods[] = { static struct PyMethodDef I_methods[] = {
{"read", (PyCFunction)O_read, 1, O_read__doc__}, {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
{"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
{"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
{"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
{"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
{"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
{"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
{"close", (PyCFunction)I_close, 0, O_close__doc__}, {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
{"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
{NULL, NULL} /* sentinel */ {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
{NULL, NULL}
}; };
static void static void
...@@ -546,7 +575,6 @@ newIobject(PyObject *s) { ...@@ -546,7 +575,6 @@ newIobject(PyObject *s) {
self->string_size=size; self->string_size=size;
self->pbuf=s; self->pbuf=s;
self->pos=0; self->pos=0;
self->closed = 0;
return (PyObject*)self; return (PyObject*)self;
} }
...@@ -589,7 +617,10 @@ static struct PycStringIO_CAPI CAPI = { ...@@ -589,7 +617,10 @@ static struct PycStringIO_CAPI CAPI = {
&Otype, &Otype,
}; };
void #ifndef DL_EXPORT /* declarations for DLL import/export */
#define DL_EXPORT(RTYPE) RTYPE
#endif
DL_EXPORT(void)
initcStringIO() { initcStringIO() {
PyObject *m, *d, *v; PyObject *m, *d, *v;
......
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