Commit f2ac4871 authored by chris's avatar chris

fixed problems with incorrectly initialized Pickler and Unpickler objects

parent 433f116c
/* /*
$Id: cPickle.c,v 1.27 1997/02/28 23:09:47 chris Exp $ $Id: cPickle.c,v 1.28 1997/03/04 18:31:16 chris Exp $
Copyright Copyright
...@@ -144,7 +144,6 @@ typedef struct { ...@@ -144,7 +144,6 @@ typedef struct {
PyObject *arg; PyObject *arg;
PyObject *pers_func; PyObject *pers_func;
PyObject *inst_pers_func; PyObject *inst_pers_func;
char *mark;
int bin; int bin;
int (*write_func)(); int (*write_func)();
char *write_buf; char *write_buf;
...@@ -1547,7 +1546,7 @@ static PyObject * ...@@ -1547,7 +1546,7 @@ static PyObject *
Pickler_dump(Picklerobject *self, PyObject *args) { Pickler_dump(Picklerobject *self, PyObject *args) {
static char stop = STOP; static char stop = STOP;
UNLESS(PyArg_Parse(args, "O", &args)) UNLESS(PyArg_ParseTuple(args, "O", &args))
return NULL; return NULL;
if (save(self, args, 0) < 0) if (save(self, args, 0) < 0)
...@@ -1594,70 +1593,65 @@ dump_special(Picklerobject *self, PyObject *args) { ...@@ -1594,70 +1593,65 @@ dump_special(Picklerobject *self, PyObject *args) {
static struct PyMethodDef Pickler_methods[] = { static struct PyMethodDef Pickler_methods[] = {
{"dump", (PyCFunction)Pickler_dump, 0, ""}, {"dump", (PyCFunction)Pickler_dump, 1, ""},
{"dump_special", (PyCFunction)dump_special, 1, ""}, {"dump_special", (PyCFunction)dump_special, 1, ""},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static Picklerobject * static Picklerobject *
newPicklerobject(PyObject *file, int bin) { newPicklerobject(PyObject *file, int bin) {
Picklerobject *self; Picklerobject *self;
PyObject *memo = 0;
UNLESS(memo = PyDict_New()) goto err; UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
return NULL;
UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype)) self->fp = NULL;
goto err; self->write = NULL;
self->memo = NULL;
self->arg = NULL;
self->pers_func = NULL;
self->inst_pers_func = NULL;
self->write_buf = NULL;
self->bin = bin;
self->buf_size = 0;
Py_INCREF(file);
self->file = file;
UNLESS(self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self);
return NULL;
}
if (PyFile_Check(file)) { if (PyFile_Check(file)) {
self->fp = PyFile_AsFile(file); self->fp = PyFile_AsFile(file);
self->write_func = write_file; self->write_func = write_file;
self->write = NULL;
self->write_buf = NULL;
} }
else if (PycStringIO_OutputCheck(file)) { else if (PycStringIO_OutputCheck(file)) {
self->fp = NULL;
self->write_func = write_cStringIO; self->write_func = write_cStringIO;
self->write = NULL;
self->write_buf = NULL;
} }
else { else {
PyObject *write;
self->fp = NULL;
self->write_func = write_other; self->write_func = write_other;
UNLESS(write = PyObject_GetAttr(file, write_str)) UNLESS(self->write = PyObject_GetAttr(file, write_str))
goto err; {
PyErr_Clear();
self->write = write; PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
"attribute");
Py_XDECREF((PyObject *)self);
return NULL;
}
UNLESS(self->write_buf = UNLESS(self->write_buf =
(char *)malloc(WRITE_BUF_SIZE * sizeof(char))) { (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
PyErr_NoMemory(); PyErr_NoMemory();
goto err; Py_XDECREF((PyObject *)self);
return NULL;
} }
self->buf_size = 0;
} }
Py_INCREF(file);
self->file = file;
self->bin = bin;
self->memo = memo;
self->arg = NULL;
self->pers_func = NULL;
self->inst_pers_func = NULL;
return self; return self;
err:
Py_XDECREF((PyObject *)self);
Py_XDECREF(memo);
return NULL;
} }
...@@ -3333,7 +3327,7 @@ err: ...@@ -3333,7 +3327,7 @@ err:
static struct PyMethodDef Unpickler_methods[] = { static struct PyMethodDef Unpickler_methods[] = {
{"load", (PyCFunction)Unpickler_load, 0, ""}, {"load", (PyCFunction)Unpickler_load, 1, ""},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -3341,68 +3335,59 @@ static struct PyMethodDef Unpickler_methods[] = { ...@@ -3341,68 +3335,59 @@ static struct PyMethodDef Unpickler_methods[] = {
static Unpicklerobject * static Unpicklerobject *
newUnpicklerobject(PyObject *f) { newUnpicklerobject(PyObject *f) {
Unpicklerobject *self; Unpicklerobject *self;
PyObject *memo = 0;
UNLESS(memo = PyDict_New())
goto err;
UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype)) UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
goto err; return NULL;
self->file = NULL;
self->arg = NULL;
self->stack = NULL;
self->pers_func = NULL;
self->last_string = NULL;
self->marks = NULL;
self->num_marks = 0;
self->marks_size = 0;
self->buf_size = 0;
self->read = NULL;
self->readline = NULL;
UNLESS(self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self);
return NULL;
}
Py_INCREF(f);
self->file = f;
/* Set read, readline based on type of f */
if (PyFile_Check(f)) { if (PyFile_Check(f)) {
self->fp = PyFile_AsFile(f); self->fp = PyFile_AsFile(f);
self->read_func = read_file; self->read_func = read_file;
self->readline_func = readline_file; self->readline_func = readline_file;
self->read = NULL;
self->readline = NULL;
} }
else if (PycStringIO_InputCheck(f)) { else if (PycStringIO_InputCheck(f)) {
self->fp = NULL; self->fp = NULL;
self->read_func = read_cStringIO; self->read_func = read_cStringIO;
self->readline_func = readline_cStringIO; self->readline_func = readline_cStringIO;
self->read = NULL;
self->readline = NULL;
} }
else { else {
PyObject *readline, *read;
self->fp = NULL; self->fp = NULL;
self->read_func = read_other; self->read_func = read_other;
self->readline_func = readline_other; self->readline_func = readline_other;
UNLESS(readline = PyObject_GetAttr(f, readline_str)) UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
goto err; (self->read = PyObject_GetAttr(f, read_str)))
{
UNLESS(read = PyObject_GetAttr(f, read_str)) { PyErr_Clear();
Py_DECREF(readline); PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
goto err; "'readline' attributes" );
Py_XDECREF((PyObject *)self);
return NULL;
} }
self->read = read;
self->readline = readline;
} }
Py_INCREF(f);
self->file = f;
self->memo = memo;
self->arg = NULL;
self->stack = NULL;
self->pers_func = NULL;
self->last_string = NULL;
self->marks = NULL;
self->num_marks = 0;
self->marks_size = 0;
self->buf_size = 0;
return self; return self;
err:
Py_XDECREF(memo);
Py_XDECREF((PyObject *)self);
return NULL;
} }
...@@ -3410,7 +3395,7 @@ static PyObject * ...@@ -3410,7 +3395,7 @@ static PyObject *
get_Unpickler(PyObject *self, PyObject *args) { get_Unpickler(PyObject *self, PyObject *args) {
PyObject *file; PyObject *file;
UNLESS(PyArg_Parse(args, "O", &file)) UNLESS(PyArg_ParseTuple(args, "O", &file))
return NULL; return NULL;
return (PyObject *)newUnpicklerobject(file); return (PyObject *)newUnpicklerobject(file);
} }
...@@ -3538,7 +3523,7 @@ cpm_load(PyObject *self, PyObject *args) { ...@@ -3538,7 +3523,7 @@ cpm_load(PyObject *self, PyObject *args) {
Unpicklerobject *unpickler = 0; Unpicklerobject *unpickler = 0;
PyObject *res = NULL; PyObject *res = NULL;
UNLESS(PyArg_Parse(args, "O", &args)) UNLESS(PyArg_ParseTuple(args, "O", &args))
goto finally; goto finally;
UNLESS(unpickler = newUnpicklerobject(args)) UNLESS(unpickler = newUnpicklerobject(args))
...@@ -3558,7 +3543,7 @@ loads(PyObject *self, PyObject *args) { ...@@ -3558,7 +3543,7 @@ loads(PyObject *self, PyObject *args) {
PyObject *file = 0, *res = NULL; PyObject *file = 0, *res = NULL;
Unpicklerobject *unpickler = 0; Unpicklerobject *unpickler = 0;
UNLESS(PyArg_Parse(args, "O", &args)) UNLESS(PyArg_ParseTuple(args, "O", &args))
goto finally; goto finally;
UNLESS(file = PycStringIO->NewInput(args)) UNLESS(file = PycStringIO->NewInput(args))
...@@ -3610,10 +3595,10 @@ static PyTypeObject Unpicklertype_value() { ...@@ -3610,10 +3595,10 @@ static PyTypeObject Unpicklertype_value() {
static struct PyMethodDef cPickle_methods[] = { static struct PyMethodDef cPickle_methods[] = {
{"dump", (PyCFunction)dump, 1, ""}, {"dump", (PyCFunction)dump, 1, ""},
{"dumps", (PyCFunction)dumps, 1, ""}, {"dumps", (PyCFunction)dumps, 1, ""},
{"load", (PyCFunction)cpm_load, 0, ""}, {"load", (PyCFunction)cpm_load, 1, ""},
{"loads", (PyCFunction)loads, 0, ""}, {"loads", (PyCFunction)loads, 1, ""},
{"Pickler", (PyCFunction)get_Pickler, 1, ""}, {"Pickler", (PyCFunction)get_Pickler, 1, ""},
{"Unpickler", (PyCFunction)get_Unpickler, 0, ""}, {"Unpickler", (PyCFunction)get_Unpickler, 1, ""},
{ NULL, NULL } { NULL, NULL }
}; };
...@@ -3705,7 +3690,7 @@ init_stuff(PyObject *module, PyObject *module_dict) { ...@@ -3705,7 +3690,7 @@ init_stuff(PyObject *module, PyObject *module_dict) {
void void
initcPickle() { initcPickle() {
PyObject *m, *d; PyObject *m, *d;
char *rev="$Revision: 1.27 $"; char *rev="$Revision: 1.28 $";
PyObject *format_version; PyObject *format_version;
PyObject *compatible_formats; PyObject *compatible_formats;
......
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