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

Fred L. Drake fdrake@users.sourceforge.net
2001年4月19日 09:26:08 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv519
Modified Files:
	weakref.py 
Log Message:
Weak*Dictionary: Added docstrings to the classes.
Weak*Dictionary.update(): No longer create a temporary list to hold the
 things that will be stuffed into the underlying dictionary. This had
 been done so that if any of the objects used as the weakly-held value
 was not weakly-referencable, no updates would take place (TypeError
 would be raised). With this change, TypeError will still be raised
 but a partial update could occur. This is more like other .update()
 implementations.
Thoughout, use of the name "ref" as a local variable has been removed. The
original use of the name occurred when the function to create a weak
reference was called "new"; the overloaded use of the name could be
confusing for someone reading the code. "ref" used as a variable name
has been replaced with "wr" (for 'weak reference').
Index: weakref.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/weakref.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** weakref.py	2001年04月16日 17:34:48	1.8
--- weakref.py	2001年04月19日 16:26:06	1.9
***************
*** 6,9 ****
--- 6,13 ----
 """
 
+ # Naming convention: Variables named "wr" are weak reference objects;
+ # they are called this instead of "ref" to avoid name collisions with
+ # the module-global ref() function imported from _weakref.
+ 
 import UserDict
 
***************
*** 24,29 ****
--- 28,38 ----
 "CallableProxyType", "ProxyTypes", "WeakValueDictionary"]
 
+ 
 class WeakValueDictionary(UserDict.UserDict):
+ """Mapping class that references values weakly.
 
+ Entries in the dictionary will be discarded when no strong
+ reference to the value exists anymore
+ """
 # We inherit the constructor without worrying about the input
 # dictionary; since it uses our .update() method, we get the right
***************
*** 49,54 ****
 def copy(self):
 new = WeakValueDictionary()
! for key, ref in self.data.items():
! o = ref()
 if o is not None:
 new[key] = o
--- 58,63 ----
 def copy(self):
 new = WeakValueDictionary()
! for key, wr in self.data.items():
! o = wr()
 if o is not None:
 new[key] = o
***************
*** 57,65 ****
 def get(self, key, default=None):
 try:
! ref = self.data[key]
 except KeyError:
 return default
 else:
! o = ref()
 if o is None:
 # This should only happen
--- 66,74 ----
 def get(self, key, default=None):
 try:
! wr = self.data[key]
 except KeyError:
 return default
 else:
! o = wr()
 if o is None:
 # This should only happen
***************
*** 70,75 ****
 def items(self):
 L = []
! for key, ref in self.data.items():
! o = ref()
 if o is not None:
 L.append((key, o))
--- 79,84 ----
 def items(self):
 L = []
! for key, wr in self.data.items():
! o = wr()
 if o is not None:
 L.append((key, o))
***************
*** 78,83 ****
 def popitem(self):
 while 1:
! key, ref = self.data.popitem()
! o = ref()
 if o is not None:
 return key, o
--- 87,92 ----
 def popitem(self):
 while 1:
! key, wr = self.data.popitem()
! o = wr()
 if o is not None:
 return key, o
***************
*** 85,112 ****
 def setdefault(self, key, default):
 try:
! ref = self.data[key]
 except KeyError:
 def remove(o, data=self.data, key=key):
 del data[key]
! ref = ref(default, remove)
! self.data[key] = ref
 return default
 else:
! return ref()
 
 def update(self, dict):
 d = self.data
- L = []
 for key, o in dict.items():
 def remove(o, data=d, key=key):
 del data[key]
! L.append((key, ref(o, remove)))
! for key, r in L:
! d[key] = r
 
 def values(self):
 L = []
! for ref in self.data.values():
! o = ref()
 if o is not None:
 L.append(o)
--- 94,117 ----
 def setdefault(self, key, default):
 try:
! 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:
! return wr()
 
 def update(self, dict):
 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):
 L = []
! for wr in self.data.values():
! o = wr()
 if o is not None:
 L.append(o)
***************
*** 115,119 ****
--- 120,133 ----
 
 class WeakKeyDictionary(UserDict.UserDict):
+ """ Mapping class that references keys weakly.
 
+ Entries in the dictionary will be discarded when there is no
+ longer a strong reference to the key. This can be used to
+ associate additional data with an object owned by other parts of
+ an application without adding attributes to those objects. This
+ can be especially useful with objects that override attribute
+ accesses.
+ """
+ 
 def __init__(self, dict=None):
 self.data = {}
***************
*** 156,161 ****
 def keys(self):
 L = []
! for ref in self.data.keys():
! o = ref()
 if o is not None:
 L.append(o)
--- 170,175 ----
 def keys(self):
 L = []
! for wr in self.data.keys():
! o = wr()
 if o is not None:
 L.append(o)
***************
*** 174,182 ****
 def update(self, dict):
 d = self.data
- L = []
 for key, value in dict.items():
! L.append((ref(key, self._remove), value))
! for key, r in L:
! d[key] = r
 
 # no longer needed
--- 188,194 ----
 def update(self, dict):
 d = self.data
 for key, value in dict.items():
! d[ref(key, self._remove)] = value
! 
 
 # no longer needed

AltStyle によって変換されたページ (->オリジナル) /