pickling extension class

harold fellermann harold.fellermann at upf.edu
Tue Jan 18 14:03:03 EST 2005

Hi all,

I have a problem pickling an extension class. As written in the 
Extending/Embedding Manual, I
provided a function __reduce__ that returns the appropreate tuple. This 
seams to work fine,
but I still cannot pickle because of the following error:

 >>> from model import hyper
 >>> g = hyper.PeriodicGrid(4,4,1)
 >>> g.__reduce__()
(<type 'hyper.PeriodicGrid'>,(4.,4.,1.))
 >>> import pickle
 >>> pickle.dump(g,file("test","w"))
Traceback (most recent call last):
   File "pickle_test.py", line 5, in ?
   File "/sw/lib/python2.4/pickle.py", line 1382, in dump
     Pickler(file, protocol, bin).dump(obj)
   File "/sw/lib/python2.4/pickle.py", line 231, in dump
   File "/sw/lib/python2.4/pickle.py", line 338, in save
     self.save_reduce(obj=obj, *rv)
   File "/sw/lib/python2.4/pickle.py", line 414, in save_reduce
   File "/sw/lib/python2.4/pickle.py", line 293, in save
     f(self, obj) # Call unbound method with explicit self
   File "/sw/lib/python2.4/pickle.py", line 760, in save_global
     raise PicklingError(
pickle.PicklingError: Can't pickle <type 'hyper.PeriodicGrid'>: it's 
not found as hyper.PeriodicGrid
 >>> dir(hyper)
['Dir', 'Neighbors', 'PeriodicGrid', 'PeriodicPos', '__doc__', 
'__file__', '__name__', 'refcount']
 >>> hyper.PeriodicGrid
<type 'hyper.PeriodicGrid'>

So pickle complains about the class PeriodicGrid not being found in the 
module hyper, but a dir()
proves that python can find it. Has anyone an idea what's going wrong 

Any help appreceated,

- harold -

What is mind? -- Doesn't matter.
What is matter? -- Never mind!

More information about the Python-list mailing list