[SciPy-user] rescuing old pickled numpy arrays

David M. Cooke cookedm at physics.mcmaster.ca
Tue Jun 20 14:51:29 CDT 2006


On Tue, 20 Jun 2006 11:56:39 +0100
Cory Davis <cdavis at staffmail.ed.ac.uk> wrote:

> Thanks for the advice David,
> here is an example pickle that produces the error below.  I think its 
> just a dictionary with some arrays.
> Cheers,
> Cory.

This seems to work:

import pickle
from cStringIO import StringIO

class OldNumpyUnpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == 'numpy' and name == 'dtypedescr':
            name = 'dtype'
        return pickle.Unpickler.find_class(self, module, name)

def loads(s):
    f = StringIO(s)
    return OldNumpyUnpickler(f).load()

Your pickle is new enough that basically dtypedescr == dtype for what the
unpickler needs.

> David M. Cooke wrote:
> > On Mon, Jun 19, 2006 at 02:51:36PM +0100, Cory Davis wrote:
> > 
> >>Hi All,
> >>this relates to a question I raised a few weeks ago about pickled arrays 
> >>from older Numpy versions not loading.  Here is what happens when I try 
> >>to load my old data ...
> >>
> >>---> 51     return cPickle.load(infile)
> >>
> >>AttributeError: 'module' object has no attribute 'dtypedescr'
> >>
> >>Can anyone suggest an easy way to rescue this data?
> > 
> > 
> > The easy way I suppose would be to inject a 'dtypedescr' into the
> > appropiate module.
> > 
> > Have a look at the pickletools module. pickletools.dis() will
> > disassemble the pickle, so you could figure out what it's looking for.
> > 
> > At worst, you could use pickletools.genops() to pull out the data.
> > 
> > Can you send me a copy of a pickle? I may be able to cook up an upgrade
> > routine.
> > 


-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca



More information about the SciPy-user mailing list