[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Copying weakrefs

rconradharris

2/14/2008 5:31:00 PM

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
2 Answers

rconradharris

2/14/2008 6:43:00 PM

0

On Feb 14, 12:31 pm, Rick Harris <rconradhar...@gmail.com> wrote:
> 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

Update:

I was able to monkey-patch the copy module's dispatch tables to
(seemingly) fix this.

The code is:
>>> copy._copy_dispatch[weakref.ref] = copy._copy_immutable
>>> copy._deepcopy_dispatch[weakref.KeyedRef] = copy._deepcopy_atomic


Could somebody more familiar with Python internals look into whether
this patch is a) correct and b) whether this should be added to copy
module?

Thanks,
Rick

rconradharris

2/14/2008 6:52:00 PM

0

On Feb 14, 12:31 pm, Rick Harris <rconradhar...@gmail.com> wrote:
> 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
Update:

Adding the following monkey-patch seems to fix the problem:

>>> copy._copy_dispatch[weakref.ref] = copy._copy_immutable
>>> copy._deepcopy_dispatch[weakref.KeyedRef] = copy._deepcopy_atomic

Perhaps the copy module should be patched with something along these
lines.

Thanks,
Rick