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