Jim Fulton's patches to get rid of the class_map().

This commit is contained in:
Guido van Rossum 1998-08-08 19:40:10 +00:00
parent 1a87be1422
commit e2d81cd4d7
1 changed files with 27 additions and 73 deletions

View File

@ -1,19 +1,19 @@
/*
cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp
cPickle.c,v 1.53 1998/05/05 15:41:31 jim Exp
Copyright
Copyright 1996 Digital Creations, L.C., 910 Princess Anne
Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
rights reserved. Copyright in this software is owned by DCLC,
unless otherwise indicated. Permission to use, copy and
distribute this software is hereby granted, provided that the
above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear. Note that
any product, process or technology described in this software
may be the subject of other Intellectual Property rights
reserved by Digital Creations, L.C. and are not licensed
hereunder.
Copyright 1996, 1997, 1998 Digital Creations, Inc., 910
Princess Anne Street, Suite 300, Fredericksburg, Virginia 22401
U.S.A. All rights reserved. Copyright in this software is
owned by DCLC, unless otherwise indicated. Permission to use,
copy and distribute this software is hereby granted, provided
that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice
appear. Note that any product, process or technology described
in this software may be the subject of other Intellectual
Property rights reserved by Digital Creations, L.C. and are not
licensed hereunder.
Trademarks
@ -55,7 +55,7 @@
static char cPickle_module_documentation[] =
"C implementation and optimization of the Python pickle module\n"
"\n"
"cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp\n"
"cPickle.c,v 1.53 1998/05/05 15:41:31 jim Exp\n"
;
#include "Python.h"
@ -126,7 +126,6 @@ static PyObject *UnpicklingError;
static PyObject *dispatch_table;
static PyObject *safe_constructors;
static PyObject *class_map;
static PyObject *empty_tuple;
static PyObject *__class___str, *__getinitargs___str, *__dict___str,
@ -152,7 +151,6 @@ typedef struct {
char *write_buf;
int buf_size;
PyObject *dispatch_table;
PyObject *class_map;
} Picklerobject;
staticforward PyTypeObject Picklertype;
@ -177,7 +175,6 @@ typedef struct {
int buf_size;
char *buf;
PyObject *safe_constructors;
PyObject *class_map;
} Unpicklerobject;
staticforward PyTypeObject Unpicklertype;
@ -669,7 +666,7 @@ err:
}
static PyObject *
whichmodule(PyObject *class_map, PyObject *global, PyObject *global_name) {
whichmodule(PyObject *global, PyObject *global_name) {
int i, j;
PyObject *module = 0, *modules_dict = 0,
*global_name_attr = 0, *name = 0;
@ -678,11 +675,6 @@ whichmodule(PyObject *class_map, PyObject *global, PyObject *global_name) {
if (module) return module;
PyErr_Clear();
if ((module = PyDict_GetItem(class_map, global))) {
Py_INCREF(module);
return module;
}
UNLESS(modules_dict = PySys_GetObject("modules")) {
PyErr_SetString(PyExc_SystemError, "lost sys.modules");
return NULL;
@ -717,16 +709,6 @@ whichmodule(PyObject *class_map, PyObject *global, PyObject *global_name) {
name=__main___str;
}
/*
if (!j) {
cPickle_ErrFormat(PicklingError, "Could not find module for %s.",
"O", global_name);
return NULL;
}
*/
PyDict_SetItem(class_map, global, name);
Py_INCREF(name);
return name;
}
@ -1249,7 +1231,7 @@ save_inst(Picklerobject *self, PyObject *args) {
goto finally;
}
UNLESS(module = whichmodule(self->class_map, class, name))
UNLESS(module = whichmodule(class, name))
goto finally;
module_str = PyString_AS_STRING((PyStringObject *)module);
@ -1336,7 +1318,7 @@ save_global(Picklerobject *self, PyObject *args, PyObject *name) {
goto finally;
}
UNLESS(module = whichmodule(self->class_map, args, global_name))
UNLESS(module = whichmodule(args, global_name))
goto finally;
module_str = PyString_AS_STRING((PyStringObject *)module);
@ -1781,7 +1763,6 @@ newPicklerobject(PyObject *file, int bin) {
self->write_buf = NULL;
self->bin = bin;
self->buf_size = 0;
self->class_map = NULL;
self->dispatch_table = NULL;
Py_INCREF(file);
@ -1823,15 +1804,12 @@ newPicklerobject(PyObject *file, int bin) {
/* Restricted execution, get private tables */
PyObject *m;
UNLESS(self->class_map=PyDict_New()) goto err;
UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
Py_DECREF(m);
UNLESS(self->dispatch_table) goto err;
}
else {
self->class_map=class_map;
Py_INCREF(class_map);
self->dispatch_table=dispatch_table;
Py_INCREF(dispatch_table);
}
@ -1862,7 +1840,6 @@ Pickler_dealloc(Picklerobject *self) {
Py_XDECREF(self->file);
Py_XDECREF(self->pers_func);
Py_XDECREF(self->inst_pers_func);
Py_XDECREF(self->class_map);
Py_XDECREF(self->dispatch_table);
if (self->write_buf) {
@ -1955,34 +1932,17 @@ static PyTypeObject Picklertype = {
};
static PyObject *
find_class(PyObject *class_map,
PyObject *py_module_name, PyObject *py_global_name) {
PyObject *global = 0, *t = 0, *module;
find_class(PyObject *py_module_name, PyObject *py_global_name) {
PyObject *global = 0, *module;
UNLESS(t = PyTuple_New(2)) return NULL;
PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name);
Py_INCREF(py_module_name);
PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_global_name);
Py_INCREF(py_global_name);
global=PyDict_GetItem(class_map, t);
if (global) {
Py_DECREF(t);
Py_INCREF(global);
return global;
UNLESS(module=PySys_GetObject("modules")) return NULL;
UNLESS(module=PyDict_GetItem(module, py_module_name)) {
PyErr_Clear();
UNLESS(module=PyImport_Import(py_module_name)) return NULL;
}
PyErr_Clear();
UNLESS(module=PyImport_Import(py_module_name)) return NULL;
global=PyObject_GetAttr(module, py_global_name);
Py_DECREF(module);
UNLESS(global) return NULL;
if (PyDict_SetItem(class_map, t, global) < 0) global=NULL;
Py_DECREF(t);
return global;
}
@ -2566,7 +2526,8 @@ Instance_New(PyObject *cls, PyObject *args) {
if (!has_key)
if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
!PyObject_IsTrue(safe)) {
cPickle_ErrFormat(UnpicklingError, "%s is not safe for unpickling", "O", cls);
cPickle_ErrFormat(UnpicklingError,
"%s is not safe for unpickling", "O", cls);
Py_XDECREF(safe);
return NULL;
}
@ -2666,7 +2627,7 @@ load_inst(Unpicklerobject *self) {
UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
UNLESS(class = find_class(self->class_map, module_name, class_name))
UNLESS(class = find_class(module_name, class_name))
goto finally;
UNLESS(obj = Instance_New(class, arg_tup)) goto finally;
@ -2705,7 +2666,7 @@ load_global(Unpicklerobject *self) {
UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
goto finally;
UNLESS(class = find_class(self->class_map, module_name, class_name))
UNLESS(class = find_class(module_name, class_name))
goto finally;
if (PyList_Append(self->stack, class) < 0)
@ -3908,7 +3869,6 @@ newUnpicklerobject(PyObject *f) {
self->buf_size = 0;
self->read = NULL;
self->readline = NULL;
self->class_map = NULL;
UNLESS(self->memo = PyDict_New()) {
Py_XDECREF((PyObject *)self);
@ -3948,15 +3908,12 @@ newUnpicklerobject(PyObject *f) {
/* Restricted execution, get private tables */
PyObject *m;
UNLESS(self->class_map=PyDict_New()) goto err;
UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
Py_DECREF(m);
UNLESS(self->safe_constructors) goto err;
}
else {
self->class_map=class_map;
Py_INCREF(class_map);
self->safe_constructors=safe_constructors;
Py_INCREF(safe_constructors);
}
@ -3989,7 +3946,6 @@ Unpickler_dealloc(Unpicklerobject *self) {
Py_XDECREF(self->pers_func);
Py_XDECREF(self->arg);
Py_XDECREF(self->last_string);
Py_XDECREF(self->class_map);
Py_XDECREF(self->safe_constructors);
if (self->marks) {
@ -4271,8 +4227,6 @@ init_stuff(PyObject *module, PyObject *module_dict) {
Py_DECREF(copy_reg);
UNLESS(class_map = PyDict_New()) return -1;
/* Down to here ********************************** */
UNLESS(string = PyImport_ImportModule("string"))
@ -4308,7 +4262,7 @@ init_stuff(PyObject *module, PyObject *module_dict) {
void
initcPickle() {
PyObject *m, *d, *v;
char *rev="1.48";
char *rev="1.53";
PyObject *format_version;
PyObject *compatible_formats;