This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author konrad.schwarz
Recipients konrad.schwarz
Date 2021-02-18.12:19:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1613650741.99.0.0896392354865.issue43252@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-02-18 12:19:02konrad.schwarzsetrecipients: + konrad.schwarz
2021-02-18 12:19:01konrad.schwarzsetmessageid: <1613650741.99.0.0896392354865.issue43252@roundup.psfhosted.org>
2021-02-18 12:19:01konrad.schwarzlinkissue43252 messages
2021-02-18 12:19:01konrad.schwarzcreate