[Python-checkins] CVS: python/dist/src/Lib weakref.py,1.12,1.13

Fred L. Drake fdrake@users.sourceforge.net
Fri, 28 Sep 2001 12:01:28 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv4234

Modified Files:
	weakref.py 
Log Message:
Clean up circular references in the Weak*Dictionary classes; this avoids
depending on the cycle detector code in the library implementation.
This is a *slightly* different patch than SF patch #417795, but takes
the same approach.  (This version avoids calling the __len__() method of
the dict in the remove() functions.)
This closes SF patch #417795.


Index: weakref.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/weakref.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** weakref.py	2001/09/06 14:51:01	1.12
--- weakref.py	2001/09/28 19:01:26	1.13
***************
*** 52,58 ****
  
      def __setitem__(self, key, value):
!         def remove(o, data=self.data, key=key):
!             del data[key]
!         self.data[key] = ref(value, remove)
  
      def copy(self):
--- 52,56 ----
  
      def __setitem__(self, key, value):
!         self.data[key] = ref(value, self.__makeremove(key))
  
      def copy(self):
***************
*** 106,112 ****
              wr = self.data[key]
          except KeyError:
!             def remove(o, data=self.data, key=key):
!                 del data[key]
!             self.data[key] = ref(default, remove)
              return default
          else:
--- 104,108 ----
              wr = self.data[key]
          except KeyError:
!             self.data[key] = ref(default, self.__makeremove(key))
              return default
          else:
***************
*** 116,122 ****
          d = self.data
          for key, o in dict.items():
!             def remove(o, data=d, key=key):
!                 del data[key]
!             d[key] = ref(o, remove)
  
      def values(self):
--- 112,116 ----
          d = self.data
          for key, o in dict.items():
!             d[key] = ref(o, self.__makeremove(key))
  
      def values(self):
***************
*** 128,131 ****
--- 122,132 ----
          return L
  
+     def __makeremove(self, key):
+         def remove(o, selfref=ref(self), key=key):
+             self = selfref()
+             if self is not None:
+                 del self.data[key]
+         return remove
+ 
  
  class WeakKeyDictionary(UserDict.UserDict):
***************
*** 143,148 ****
          self.data = {}
          if dict is not None: self.update(dict)
!         def remove(k, data=self.data):
!             del data[k]
          self._remove = remove
  
--- 144,151 ----
          self.data = {}
          if dict is not None: self.update(dict)
!         def remove(k, selfref=ref(self)):
!             self = selfref()
!             if self is not None:
!                 del self.data[k]
          self._remove = remove