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":

import copy
import transaction
from persistent import Persistent
from ZODB.DB import DB
from ZODB.MappingStorage import MappingStorage

class SampleObject(Persistent):

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

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

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

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"
    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:

ZODB-Dev mailing list  -  ZODB-Dev@zope.org

Reply via email to