[ZODB-Dev] Writing Persistent Class

Marius Gedminas mgedmin at b4net.lt
Wed Jan 23 16:19:30 EST 2008


On Mon, Jan 21, 2008 at 07:15:42PM +0100, Dieter Maurer wrote:
> Marius Gedminas wrote at 2008-1-21 00:08 +0200:
> >Personally, I'd be afraid to use deepcopy on a persistent object.
> 
> A deepcopy is likely to be no copy at all.
> 
>   As Python's "deepcopy" does not know about object ids, it is likely
>   that the copy result uses the same oids as the original.
>   When you store this copy, objects with the same oid are identified.

This appears not to be the case.  The following script prints "Looks OK":


#!/usr/bin/python
import copy
import transaction
from persistent import Persistent
from ZODB.DB import DB
from ZODB.MappingStorage import MappingStorage

class SampleObject(Persistent):
    pass

db = DB(MappingStorage())
conn = db.open()
conn.root()['obj1'] = SampleObject()
transaction.commit()

# Use a different connection to sidestep the ZODB object cache
conn2 = db.open()
conn2.root()['obj2'] = copy.deepcopy(conn2.root()['obj1'])
transaction.commit()

conn3 = db.open()
obj1 = conn3.root()['obj1']
obj2 = conn3.root()['obj2']
transaction.commit()

if obj1 is obj2:
    print "Fail: instead of a copy we got the same object"
elif obj1._p_oid == obj2._p_oid:
    print "Fail: copy has the same oid"
else:
    print "Looks OK."


Marius Gedminas
-- 
I used to think I was indecisive, but now I'm not so sure.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zodb-dev/attachments/20080123/a2a01a17/attachment.bin


More information about the ZODB-Dev mailing list