[albatross-users] This example is broken

Dave Cole djc at object-craft.com.au
Thu Oct 24 12:54:51 EST 2002


> http://www.object-craft.com.au/cgi-bin/alsamp/random/random.py/tree
> Template traceback (most recent call last):
> 
> Traceback (most recent call last):
>   File "/usr/lib/python2.2/site-packages/albatross/app.py", line 148, in
> run
>     self.save_session(ctx)
>   File "/usr/lib/python2.2/site-packages/albatross/app.py", line 207, in
> save_session
>     ctx.save_session()
>   File "/usr/lib/python2.2/site-packages/albatross/session.py", line 69,
> in save_session
>     text = self.encode_session()
>   File "/usr/lib/python2.2/site-packages/albatross/context.py", line
> 510, in encode_session
>     cPickle.dumps(value, 1)
> PicklingError: Can't pickle tree.Node: it's not the same object as
> tree.Node

It seems that there have been some big changes with extra checks in the
cPickle module between Python 2.1 and 2.2...

Now all of a sudden the pickler is trying to import the module that a
class belongs to.  I suspect that the code is getting confused by our
use of imp.find_module(name, [dirname]) to import the module which
contains the definition of the Node class.

I need to do some more experiments.

- Dave

diff -u Python-2.1/Modules/cPickle.c Python-2.2/Modules/cPickle.c
[snip]
 static int
 save_global(Picklerobject *self, PyObject *args, PyObject *name) {
-    PyObject *global_name = 0, *module = 0;
+    PyObject *global_name = 0, *module = 0, *mod = 0, *moddict = 0, *klass = 0;
     char *name_str, *module_str;
     int module_size, name_size, res = -1;
 
@@ -1648,6 +1698,29 @@
     module_str = PyString_AS_STRING((PyStringObject *)module);
     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;

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list