__getattr__ and pickle
Gordon McMillan
gmcm at hypernet.com
Sun Jul 2 09:07:44 EDT 2000
[posted and mailed]
Sean Mc Grath wrote:
>Sheesh! I really need a holiday!
Probably <wink>!
>I have a reproduceable GPF on NT when pickling objects of a class
>that has a __getattr__ implementation. Take out the __getattr__ and
>the pickle works fine.
>
>Are there ways to implement __getattr__ and yet preserve the ability
>to pickle?
C'mon Sean! You've been around a while. You know when __getattr__ hacks go
bad it's because they go recursive. (And the stack size on Windows is off
by some tiny amount, so it GPFs instead of traps.)
So what do we need:
>>> class A:
... def __getattr__(self, nm):
... print "get", nm
... raise AttributeError, nm
...
>>> a = A()
>>> import pickle
>>> s = pickle.dumps(a)
get __getinitargs__
get __getstate__
>>> x = pickle.loads(s)
get __setstate__
>>> import cPickle
>>> s = cPickle.dumps(a)
get __getinitargs__
get __getstate__
>>> x = cPickle.loads(s)
get __setstate__
>>>
Do you have a __getinitargs__ or __getstate__ or __setstate__ that might be
triggering a call to __getattr__?
Does an Irishman like to talk?
- Gordon
More information about the Python-list
mailing list