pickling extension class

David M. Cooke cookedm+news at physics.mcmaster.ca
Tue Jan 18 16:38:07 EST 2005


harold fellermann <harold.fellermann at upf.edu> writes:

> 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 ?
>      pickle.dump(g,file("test","w"))
>    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
>      self.save(obj)
>    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
>      save(func)
>    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'>
         ^^^^^

I think that's your error. The extension type is declared to be
hyper.PeriodicGrid, where it actually is model.hyper.PeriodicGrid
(because hyper is in the model package).

Pickle stores g.__class__.__module__ (which is "hyper") and
g.__class__.__name__ (="PeriodicGrid") to find the class object for
reimporting, and on unpickling, tries to do __import__("hyper"), which
fails.

The tp_name slot of your extension type should be "model.hyper.PeriodicGrid".

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list