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.

Attachment: signature.asc
Description: Digital 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