[ZODB-Dev] None-persistent behaviour of BTree subclasses

Jeremy Hylton jeremy at alum.mit.edu
Sun Apr 4 22:17:01 EDT 2004


On Sun, 2004-04-04 at 05:14, pm5 at matilda.fiorile.org wrote:
> It seems that attributes of BTree subclasses are not saved. The
> following code raises an ``AssertionError: None != 'foo''' at the
> assertEqual line.  Descendants of Persistent (just change the definition
> of UserDefinedClass and run) perserves this attribute correctly.
> 
> I'm not sure if my testing style violates some disciplins of ZODB (use
> of connections, for one thing), so I'm posting the whole testing script.
> I would be glad if someone could point out any of the problems.

I don't know if subclassing BTrees is an officially supported feature. 
If it is, then the documentation ought to say that subclasses must
define their own __getstate__() and __setstate__() if they want to add
their attributes.  BTrees inherit from Persistence, but provide their
own __getstate__() and __setstate__() implementations.

You'd need to do something like:

class MyBTree(OOBTree):

    def __init__(self):
        OOBTree.__init__(self)
        self.attr = 1

    def __getstate__(self):
        return OOBTree.__getstate__(self), self.__dict__

    def __setstate__(self, state):
        base, ext = state
        OOBTree.__setstate__(base)
        self.__dict__.update(ext)

Jeremy





More information about the ZODB-Dev mailing list