From 01841b0f4833ac21a97d02618e3949569796c78b Mon Sep 17 00:00:00 2001 From: Daniel Brown <ddb@star.sr.bham.ac.uk> Date: Fri, 10 Apr 2015 23:23:15 +0100 Subject: [PATCH] adding in param weakref refreshing --- pykat/components.py | 15 ++++++++++++++- pykat/param.py | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pykat/components.py b/pykat/components.py index fa2e915..7931e3a 100644 --- a/pykat/components.py +++ b/pykat/components.py @@ -198,7 +198,20 @@ class Component(object): del self._params[:] self.__removed = True - + + def __deepcopy__(self, memo): + cls = self.__class__ + result = cls.__new__(cls) + memo[id(self)] = result + + for k, v in self.__dict__.items(): + setattr(result, k, deepcopy(v, memo)) + + for p in result._params: + p._updateOwner(result) + + return result + class AbstractMirrorComponent(Component): __metaclass__ = abc.ABCMeta diff --git a/pykat/param.py b/pykat/param.py index 31fe7c3..a5bb11d 100644 --- a/pykat/param.py +++ b/pykat/param.py @@ -87,6 +87,15 @@ class Param(putable, putter): putter.__init__(self, var_name, isPutter) putable.__init__(self, owner.name, name, isPutable) + + def _updateOwner(self, newOwner): + """ + This updates the internal weak reference to link a parameter to who owns it. + Should only be called by the __deepcopy__ component method to ensure things + are kept up to date. + """ + del self._owner + self._owner = weakref.ref(newOwner) @property def canFsig(self): return self._canFsig -- GitLab