-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dieter Maurer wrote:
> Tres Seaver wrote at 2008-1-17 01:30 -0500:
>> ...
>> Mika, David P (GE, Research) wrote:
>>
>>>> Can someone explain why the test below  (test_persistence) is failing?
>>>> I am adding an attribute after object creation with __setstate__, but
>>>> I can't get the new attribute to persist.
>> You are mutating the object *inside* your __setstate__:  the ZODB
>> persistence machinery clears the '_p_changed' flag after you *exit* from
>> '__setstate__':  the protocol is not intended to support a persistent
>> "write-on-read".
> 
> When I remember right, newer ZODB versions allow the "__setstate__"
> implementation to tell whether "_p_changed" should or should not be
> cleared (default: cleared).
> 
> Tim Peters added this feature to support the frequent use case,
> that "__setstate__" is used for object migration.

The code in question is in $ZOPE_HOME/lib/python/persistent/cPersistence.c:

    /* set state to CHANGED while setstate() call is in progress
       to prevent a recursive call to _PyPersist_Load().
    */
    self->state = cPersistent_CHANGED_STATE;
    /* Call the object's __setstate__() */
    r = PyObject_CallMethod(self->jar, "setstate", "O", (PyObject \
                               *)self);
    if (r == NULL) {
        ghostify(self);
        return -1;
    }
    self->state = cPersistent_UPTODATE_STATE;

which blows away any changes made to self._p_changed during the call the
'__setstate__'.  The intent is to allow '__setstate__' to do a
*noo-persistent* fixup[1] of the object at un-ghosting:  read-on-write
generally doesn't scale well, which means ZODB wants to avoid doing it
implicitly.

[1] The fixup will be saved only if some other code causes the object to
set the changed flag.


Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          [EMAIL PROTECTED]
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHj99B+gerLs4ltQ4RAigFAJ9C5W5W7G1+trdk0om9ge0XDUpk/wCfQQpz
cU6TCAIZsXM30oD6qxsVN88=
=C6jt
-----END PGP SIGNATURE-----

_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to