New submission from Konrad Schwarz <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue43252>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com