[New-bugs-announce] [issue43252] deepcopy of weakref proxies
Konrad Schwarz
report at bugs.python.org
Thu Feb 18 07:19:02 EST 2021
New submission from Konrad Schwarz <konrad.schwarz at gmail.com>:
copy.deepcopy()-ing a tree structure that has internal parent and cross-reference links implemented as weakref.proxy() objects causes the weak reference proxies themselves to be copied (still refering to their original referents) rather than weak references to deep-copied referents to be created.
A workaround is to add the following __deepcopy__ method to affected classes:
def __deepcopy__ (self, memo):
# taken from Stackoverflow: "How to override the
# copy deepcopy operations for a python object"
# and "Python: dereferencing weakproxy"
cls = self.__class__
result = cls.__new__ (cls)
memo [id (self)] = result
for k, v in self.__dict__.items ():
if isinstance (v, weakref.ProxyType):
new_v = weakref.proxy (copy.deepcopy (
v.__repr__.__self__, memo))
else:
new_v = copy.deepcopy (v, memo)
setattr (result, k, new_v)
return result
----------
components: Library (Lib)
messages: 387226
nosy: konrad.schwarz
priority: normal
severity: normal
status: open
title: deepcopy of weakref proxies
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43252>
_______________________________________
More information about the New-bugs-announce
mailing list