[hg] galaxy 1580: Use a weakref in metadata to prevent a circula...

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[hg] galaxy 1580: Use a weakref in metadata to prevent a circula...

Greg Von Kuster
details:   http://www.bx.psu.edu/hg/galaxy/rev/91f6455e19e4
changeset: 1580:91f6455e19e4
user:      Dan Blankenberg <[hidden email]>
date:      Tue Oct 28 12:57:39 2008 -0400
description:
Use a weakref in metadata to prevent a circular reference possibly interfering with garbage collection: hda/lda (parent) <--> MetadataCollection (self)

1 file(s) affected in this change:

lib/galaxy/datatypes/metadata.py

diffs (32 lines):

diff -r 778fcc385ab7 -r 91f6455e19e4 lib/galaxy/datatypes/metadata.py
--- a/lib/galaxy/datatypes/metadata.py Tue Oct 28 11:27:42 2008 -0400
+++ b/lib/galaxy/datatypes/metadata.py Tue Oct 28 12:57:39 2008 -0400
@@ -1,4 +1,4 @@
-import sys, logging, copy, shutil
+import sys, logging, copy, shutil, weakref
 
 from galaxy.util import string_as_bool
 from galaxy.util.odict import odict
@@ -40,6 +40,13 @@
         #initialize dict if needed
         if self.parent._metadata is None:
             self.parent._metadata = {}
+    def get_parent( self ):
+        if "_parent" in self.__dict__:
+            return self.__dict__["_parent"]()
+        return None
+    def set_parent( self, parent ):
+        self.__dict__["_parent"] = weakref.ref( parent ) # use weakref to prevent a circular reference interfering with garbage collection: hda/lda (parent) <--> MetadataCollection (self) ; needs to be hashable, so cannot use proxy.
+    parent = property( get_parent, set_parent )
     @property
     def spec( self ):
         return self.parent.datatype.metadata_spec
@@ -65,7 +72,7 @@
             return self.parent._metadata[name]
     def __setattr__( self, name, value ):
         if name == "parent":
-            self.__dict__[name] = value
+            return self.set_parent( value )
         else:
             if name in self.spec:
                 self.parent._metadata[name] = self.spec[name].unwrap( value )