From 0abb97cfae430aaf817fc11866af29667d4bdc33 Mon Sep 17 00:00:00 2001
From: Daniel Brown <ddb@star.sr.bham.ac.uk>
Date: Fri, 10 Apr 2015 14:15:00 +0100
Subject: [PATCH] fixing weakref updating on deepcopy for NodeGaussSetters

---
 pykat/components.py   |  4 ++--
 pykat/finesse.py      | 19 +++++++++++++++++--
 pykat/node_network.py |  4 ++--
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/pykat/components.py b/pykat/components.py
index f6b5e83..fa2e915 100644
--- a/pykat/components.py
+++ b/pykat/components.py
@@ -15,7 +15,7 @@ from pykat.SIfloat import *
 from pykat.param import Param, AttrParam
 import weakref
 import pykat.exceptions as pkex
-
+from copy import deepcopy
 next_component_id = 1
 
 from pykat import USE_GUI, NoGUIException
@@ -198,7 +198,7 @@ class Component(object):
         del self._params[:]
 
         self.__removed = True
-        
+            
 class AbstractMirrorComponent(Component):
     __metaclass__ = abc.ABCMeta
         
diff --git a/pykat/finesse.py b/pykat/finesse.py
index 5033707..573fccc 100644
--- a/pykat/finesse.py
+++ b/pykat/finesse.py
@@ -424,6 +424,20 @@ class kat(object):
         if kat_file != None:
             self.loadKatFile(kat_file)
 
+    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, copy.deepcopy(v, memo))
+        
+        # Update any weakrefs
+        for c in result.components:
+            result.components[c]._Component__update_node_setters()
+            
+        return result
+    
     @property
     def signals(self): return self.__signals
 
@@ -919,8 +933,9 @@ class kat(object):
                 r = katRun()
                 
             r.yaxis = self.yaxis
-            r.katScript = "".join(self.generateKatScript())   
-
+            
+            r.katScript = "".join(self.generateKatScript()) 
+            
             if (plot==None):
                 # ensure we don't do any plotting. That should be handled
                 # by user themselves
diff --git a/pykat/node_network.py b/pykat/node_network.py
index 1a565b8..d35f45f 100644
--- a/pykat/node_network.py
+++ b/pykat/node_network.py
@@ -498,7 +498,7 @@ class Node(object):
     def setGauss(self, component, *args):
         self.__q_comp = component
         
-        if len(args) == 1:
+        if len(args) == 1:  
             self.__q_x = beam_param(self._network.kat.lambda0, q=args[0])
             self.__q_y = beam_param(self._network.kat.lambda0, q=args[0])
         elif len(args) == 2:
@@ -506,7 +506,7 @@ class Node(object):
             self.__q_y = beam_param(self._network.kat.lambda0, q=args[1])
         else:
             raise pkex.BasePyKatException("Must specify either 1 Gaussian beam parameter or 2 for astigmatic beams")
-        
+                
     def getFinesseText(self):    
         if self.__q_x is None or self.__q_y is None or self.__q_comp is None:
             return []
-- 
GitLab