Repository: byuccl/spydrnet

Issue:
Reference tracking added to definitions, but references not removed when instance reference reassigned or deleted

Discussion:

Patch:
diff --git a/spydrnet/ir/instance.py b/spydrnet/ir/instance.py
index 33b9d8b..2f739de 100644
--- a/spydrnet/ir/instance.py
+++ b/spydrnet/ir/instance.py
@@ -50,11 +50,14 @@ class Instance(Element):
                     pin._instance = None
                     pin._inner_pin = None
             self._pins.clear()
+            if self._reference:
+                self._reference._references.remove(self)
         else:
             if self._reference is not None:
                 assert len(self.reference.ports) == len(value.ports) and all(len(x.pins) == len(y.pins) for x, y in
                                                                              zip(self.reference.ports, value.ports)), \
                     "Reference reassignment only supported for definitions with matching port positions"
+                self._reference._references.remove(self)
                 for cur_port, new_port in zip(self.reference.ports, value.ports):
                     for cur_pin, new_pin in zip(cur_port.pins, new_port.pins):
                         outer_pin = self._pins.pop(cur_pin)
@@ -67,6 +70,10 @@ class Instance(Element):
             value._references.add(self)
         self._reference = value
 
+    @reference.deleter
+    def reference(self):
+        self.reference = None
+
     @property
     def pins(self):
         '''get the pins on this instance.'''


Test patch:
diff --git a/spydrnet/ir/tests/test_instance.py b/spydrnet/ir/tests/test_instance.py
index 3a74be5..0bf268a 100644
--- a/spydrnet/ir/tests/test_instance.py
+++ b/spydrnet/ir/tests/test_instance.py
@@ -116,6 +116,7 @@ class TestInstance(unittest.TestCase):
         pin1 = port.create_pin()
         pin2 = port.create_pin()
         self.instance.reference = definition
+        self.assertTrue(self.instance in definition.references)
         outer_pin1 = self.instance.pins[pin1]
         outer_pin2 = self.instance.pins[pin2]
 
@@ -124,5 +125,25 @@ class TestInstance(unittest.TestCase):
         pin1 = port.create_pin()
         pin2 = port.create_pin()
         self.instance.reference = definition2
+        self.assertTrue(self.instance not in definition.references)
+        self.assertTrue(self.instance in definition2.references)
         self.assertEqual(outer_pin1, self.instance.pins[pin1])
-        self.assertEqual(outer_pin2, self.instance.pins[pin2])
\ No newline at end of file
+        self.assertEqual(outer_pin2, self.instance.pins[pin2])
+
+    def test_reference_removal(self):
+        definition = sdn.Definition()
+        port = definition.create_port()
+        pin1 = port.create_pin()
+        pin2 = port.create_pin()
+        self.instance.reference = definition
+        self.assertTrue(self.instance in definition.references)
+        outer_pin1 = self.instance.pins[pin1]
+        outer_pin2 = self.instance.pins[pin2]
+        self.assertEqual(outer_pin1.inner_pin, pin1)
+        self.assertEqual(outer_pin2.inner_pin, pin2)
+
+        del self.instance.reference
+        self.assertTrue(self.instance not in definition.references)
+        self.assertIsNone(outer_pin1.inner_pin)
+        self.assertIsNone(outer_pin2.inner_pin)
+