[ZODB-Dev] Re: false write conflicts
John Belmonte
john at neggie.net
Tue Mar 2 12:45:29 EST 2004
Jeremy Hylton wrote:
> 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.
Thanks for the heads-up. I'm wondering if the ghosted object issue can
be safely ignored in my case. The worst my hook can do is call
Persistent.__setattr__ when it doesn't need to, flagging the object as
dirty. However, in the case of what I'm trying to address (in-place
modification of an attribute object), a __getattr__ will have already
occurred, so the containing object will normally not be ghosted.
-John
--
http:// if ile.org/
More information about the ZODB-Dev
mailing list