[Python-checkins] CVS: python/dist/src/Modules cPickle.c,2.62,2.63
Guido van Rossum
gvanrossum@users.sourceforge.net
2001年8月18日 14:22:09 -0700
Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv17479
Modified Files:
cPickle.c
Log Message:
SF patch #452239 by Gordon McMillan, to fix SF bug #451547.
This patch attempts to do to cPickle what Guido did
for pickle.py v 1.50. That is: save_global tries
importing the module, and fetching the name from the
module. If that fails, or the returned object is not
the same one we started with, it raises a
PicklingError. (All this so pickling a lambda will
fail at save time, rather than load time).
Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.62
retrieving revision 2.63
diff -C2 -d -r2.62 -r2.63
*** cPickle.c 2001年08月17日 18:39:25 2.62
--- cPickle.c 2001年08月18日 21:22:07 2.63
***************
*** 1624,1628 ****
static int
save_global(Picklerobject *self, PyObject *args, PyObject *name) {
! PyObject *global_name = 0, *module = 0;
char *name_str, *module_str;
int module_size, name_size, res = -1;
--- 1624,1628 ----
static int
save_global(Picklerobject *self, PyObject *args, PyObject *name) {
! PyObject *global_name = 0, *module = 0, *mod = 0, *moddict = 0, *klass = 0;
char *name_str, *module_str;
int module_size, name_size, res = -1;
***************
*** 1649,1652 ****
--- 1649,1675 ----
name_str = PyString_AS_STRING((PyStringObject *)global_name);
+ mod = PyImport_ImportModule(module_str);
+ if (mod == NULL) {
+ /* Py_ErrClear(); ?? */
+ cPickle_ErrFormat(PicklingError,
+ "Can't pickle %s: it's not found as %s.%s",
+ "OSS", args, module, global_name);
+ goto finally;
+ }
+ moddict = PyModule_GetDict(mod); /* borrowed ref */
+ klass = PyDict_GetItemString(moddict, name_str); /* borrowed ref */
+ if (klass == NULL) {
+ cPickle_ErrFormat(PicklingError,
+ "Can't pickle %s: it's not found as %s.%s",
+ "OSS", args, module, global_name);
+ goto finally;
+ }
+ if (klass != args) {
+ cPickle_ErrFormat(PicklingError,
+ "Can't pickle %s: it's not the same object as %s.%s",
+ "OSS", args, module, global_name);
+ goto finally;
+ }
+
if ((*self->write_func)(self, &global, 1) < 0)
goto finally;
***************
*** 1672,1675 ****
--- 1695,1699 ----
Py_XDECREF(module);
Py_XDECREF(global_name);
+ Py_XDECREF(mod);
return res;