[ZODB-Dev] Re: false write conflicts

Jeremy Hylton jeremy at zope.com
Tue Mar 2 11:56:41 EST 2004


On Tue, 2004-03-02 at 11:15, John Belmonte wrote:
> This wrapper for Persistent.__setattr__ looks like it will suit my needs:
> 
>    class MyPersistent(Persistent):
>      def __setattr__(self, key, val):
>        if not (self.__dict__.has_key(key) and self.__dict__[key] is val):
>          Persistent.__setattr__(self, key, val)

My preference would be to avoid a setattr hook at all costs, but that's
just my preference.

We've revised the way Persistent objects should implement getattr and
setattr hooks in ZODB 3.3.  (I'm not sure how it worked in earlier
versions of ZODB.)  There is no documentation yet, but the tests in
ZODB.tests.test_overriding_attrs explain it pretty clearly.

You need to let Persistent run before any user code, because the object
could be a ghost.  If it's a ghost, then __dict__ will be an empty
dictionary and the setattr won't do what you expect.  First, you should
call Persistent._p_setattr(self, key, val).  If it returns True if
Persistent handled the attribute; for example, if you assigned to
_p_oid, the Persistent will handle the attribute assignment.

Jeremy





More information about the ZODB-Dev mailing list