__getattr__ and pickle

Christian Tismer tismer at appliedbiometrics.com
Sun Jul 2 13:22:02 EDT 2000


Hi Sean,

seanmcgrath at my-deja.com wrote:
> 
> At 09:07 02/07/00 -0400, Gordon McMillan wrote:
> >
> >Do you have a __getinitargs__ or __getstate__ or
> __setstate__ that might be
> >triggering a call to __getattr__?
> >
> 
> I don't have calls to __getinitargs__,
> __getstate__ or __setstate__.
> 
> What I do have is a __getattr__ which I suspect is
> involved in this:-)
> 
> The patient is pyxie.py
> (http://www.digitome.com/pyxie.py).
> The object I am trying to pickle is a tree
> structure.
> A variable CurPos points to a sub-ordinate object
> which can be of type xElement or xData.
> 
> The __getattr__ hook achieves a Delphi-like effect
> in which attributes of these sub-ordinate objects
> appear as attributes of the top level object.
> 
> It seemed like a good idea at the time:-)

Well, I see that your __getattr__ is a little insane.

        def __getattr__(self,n):
                if hasattr(self.CurPos,n):
                        return getattr(self.CurPos,n)
                else:
                        raise PyxieException (
                        "No attribute '%s' on xTree or current xNode" % n)

It redirects *every* reading attribute access to the CurPos
object, so Pickle can't even figure out what your xTree's
class is. Also, the CurPos object can's be saved with it,
nothing is accessible.

First, I suggest to redirect all requests with __ to the class,
second, give access to xTree object attributes a higher priority
by first looking into self by self.__dict__.has_key(n) .

This might work. Maybe you also want to avoid to store
CurPos in the pickle at all. This can be done by introducing
__getinitargs__ or __getstate__ and __setstate__ .

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list