Copying weakrefs

Rick Harris rconradharris at gmail.com
Thu Feb 14 12:31:25 EST 2008


I am working with an object, Context, that maintains an identity map
by using the weakref.WeakValueDictionary. I would like to clone this
Context object (and objects that may have a Context object) using
copy.deepcopy(). When I try to do this, the deep copy operation
recurses down to the WeakValueDictionary, down to the KeyedRef
subclass of ref, and then fails. It seems that copy.copy() and
copy.deepcopy() operations on weakrefs just won't work.

The failure can be replicated with this (much simpler) scenario:

>>> import weakref, copy
>>> class Test(object): pass
>>> t = Test()
>>> wr = weakref.ref(t)
>>> wr_new = copy.copy(wr) # Fails, not enough args to __new__

Anybody out there have some insight into this?

Thanks
Rick



More information about the Python-list mailing list