[ZODB-Dev] Re: Why does this useage of __setstate__ fail?

Tres Seaver tseaver at palladion.com
Thu Jan 17 18:05:37 EST 2008


-----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          tseaver at palladion.com
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-----



More information about the ZODB-Dev mailing list