You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
10 KiB

  1. """Weak reference support for Python.
  2. This module is an implementation of PEP 205:
  3. http://www.python.org/dev/peps/pep-0205/
  4. """
  5. # Naming convention: Variables named "wr" are weak reference objects;
  6. # they are called this instead of "ref" to avoid name collisions with
  7. # the module-global ref() function imported from _weakref.
  8. import UserDict
  9. from _weakref import (
  10. getweakrefcount,
  11. getweakrefs,
  12. ref,
  13. proxy,
  14. CallableProxyType,
  15. ProxyType,
  16. ReferenceType)
  17. from _weakrefset import WeakSet
  18. from exceptions import ReferenceError
  19. ProxyTypes = (ProxyType, CallableProxyType)
  20. __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
  21. "WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
  22. "CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
  23. class WeakValueDictionary(UserDict.UserDict):
  24. """Mapping class that references values weakly.
  25. Entries in the dictionary will be discarded when no strong
  26. reference to the value exists anymore
  27. """
  28. # We inherit the constructor without worrying about the input
  29. # dictionary; since it uses our .update() method, we get the right
  30. # checks (if the other dictionary is a WeakValueDictionary,
  31. # objects are unwrapped on the way out, and we always wrap on the
  32. # way in).
  33. def __init__(self, *args, **kw):
  34. def remove(wr, selfref=ref(self)):
  35. self = selfref()
  36. if self is not None:
  37. del self.data[wr.key]
  38. self._remove = remove
  39. UserDict.UserDict.__init__(self, *args, **kw)
  40. def __getitem__(self, key):
  41. o = self.data[key]()
  42. if o is None:
  43. raise KeyError, key
  44. else:
  45. return o
  46. def __contains__(self, key):
  47. try:
  48. o = self.data[key]()
  49. except KeyError:
  50. return False
  51. return o is not None
  52. def has_key(self, key):
  53. try:
  54. o = self.data[key]()
  55. except KeyError:
  56. return False
  57. return o is not None
  58. def __repr__(self):
  59. return "<WeakValueDictionary at %s>" % id(self)
  60. def __setitem__(self, key, value):
  61. self.data[key] = KeyedRef(value, self._remove, key)
  62. def copy(self):
  63. new = WeakValueDictionary()
  64. for key, wr in self.data.items():
  65. o = wr()
  66. if o is not None:
  67. new[key] = o
  68. return new
  69. __copy__ = copy
  70. def __deepcopy__(self, memo):
  71. from copy import deepcopy
  72. new = self.__class__()
  73. for key, wr in self.data.items():
  74. o = wr()
  75. if o is not None:
  76. new[deepcopy(key, memo)] = o
  77. return new
  78. def get(self, key, default=None):
  79. try:
  80. wr = self.data[key]
  81. except KeyError:
  82. return default
  83. else:
  84. o = wr()
  85. if o is None:
  86. # This should only happen
  87. return default
  88. else:
  89. return o
  90. def items(self):
  91. L = []
  92. for key, wr in self.data.items():
  93. o = wr()
  94. if o is not None:
  95. L.append((key, o))
  96. return L
  97. def iteritems(self):
  98. for wr in self.data.itervalues():
  99. value = wr()
  100. if value is not None:
  101. yield wr.key, value
  102. def iterkeys(self):
  103. return self.data.iterkeys()
  104. def __iter__(self):
  105. return self.data.iterkeys()
  106. def itervaluerefs(self):
  107. """Return an iterator that yields the weak references to the values.
  108. The references are not guaranteed to be 'live' at the time
  109. they are used, so the result of calling the references needs
  110. to be checked before being used. This can be used to avoid
  111. creating references that will cause the garbage collector to
  112. keep the values around longer than needed.
  113. """
  114. return self.data.itervalues()
  115. def itervalues(self):
  116. for wr in self.data.itervalues():
  117. obj = wr()
  118. if obj is not None:
  119. yield obj
  120. def popitem(self):
  121. while 1:
  122. key, wr = self.data.popitem()
  123. o = wr()
  124. if o is not None:
  125. return key, o
  126. def pop(self, key, *args):
  127. try:
  128. o = self.data.pop(key)()
  129. except KeyError:
  130. if args:
  131. return args[0]
  132. raise
  133. if o is None:
  134. raise KeyError, key
  135. else:
  136. return o
  137. def setdefault(self, key, default=None):
  138. try:
  139. wr = self.data[key]
  140. except KeyError:
  141. self.data[key] = KeyedRef(default, self._remove, key)
  142. return default
  143. else:
  144. return wr()
  145. def update(self, dict=None, **kwargs):
  146. d = self.data
  147. if dict is not None:
  148. if not hasattr(dict, "items"):
  149. dict = type({})(dict)
  150. for key, o in dict.items():
  151. d[key] = KeyedRef(o, self._remove, key)
  152. if len(kwargs):
  153. self.update(kwargs)
  154. def valuerefs(self):
  155. """Return a list of weak references to the values.
  156. The references are not guaranteed to be 'live' at the time
  157. they are used, so the result of calling the references needs
  158. to be checked before being used. This can be used to avoid
  159. creating references that will cause the garbage collector to
  160. keep the values around longer than needed.
  161. """
  162. return self.data.values()
  163. def values(self):
  164. L = []
  165. for wr in self.data.values():
  166. o = wr()
  167. if o is not None:
  168. L.append(o)
  169. return L
  170. class KeyedRef(ref):
  171. """Specialized reference that includes a key corresponding to the value.
  172. This is used in the WeakValueDictionary to avoid having to create
  173. a function object for each key stored in the mapping. A shared
  174. callback object can use the 'key' attribute of a KeyedRef instead
  175. of getting a reference to the key from an enclosing scope.
  176. """
  177. __slots__ = "key",
  178. def __new__(type, ob, callback, key):
  179. self = ref.__new__(type, ob, callback)
  180. self.key = key
  181. return self
  182. def __init__(self, ob, callback, key):
  183. super(KeyedRef, self).__init__(ob, callback)
  184. class WeakKeyDictionary(UserDict.UserDict):
  185. """ Mapping class that references keys weakly.
  186. Entries in the dictionary will be discarded when there is no
  187. longer a strong reference to the key. This can be used to
  188. associate additional data with an object owned by other parts of
  189. an application without adding attributes to those objects. This
  190. can be especially useful with objects that override attribute
  191. accesses.
  192. """
  193. def __init__(self, dict=None):
  194. self.data = {}
  195. def remove(k, selfref=ref(self)):
  196. self = selfref()
  197. if self is not None:
  198. del self.data[k]
  199. self._remove = remove
  200. if dict is not None: self.update(dict)
  201. def __delitem__(self, key):
  202. del self.data[ref(key)]
  203. def __getitem__(self, key):
  204. return self.data[ref(key)]
  205. def __repr__(self):
  206. return "<WeakKeyDictionary at %s>" % id(self)
  207. def __setitem__(self, key, value):
  208. self.data[ref(key, self._remove)] = value
  209. def copy(self):
  210. new = WeakKeyDictionary()
  211. for key, value in self.data.items():
  212. o = key()
  213. if o is not None:
  214. new[o] = value
  215. return new
  216. __copy__ = copy
  217. def __deepcopy__(self, memo):
  218. from copy import deepcopy
  219. new = self.__class__()
  220. for key, value in self.data.items():
  221. o = key()
  222. if o is not None:
  223. new[o] = deepcopy(value, memo)
  224. return new
  225. def get(self, key, default=None):
  226. return self.data.get(ref(key),default)
  227. def has_key(self, key):
  228. try:
  229. wr = ref(key)
  230. except TypeError:
  231. return 0
  232. return wr in self.data
  233. def __contains__(self, key):
  234. try:
  235. wr = ref(key)
  236. except TypeError:
  237. return 0
  238. return wr in self.data
  239. def items(self):
  240. L = []
  241. for key, value in self.data.items():
  242. o = key()
  243. if o is not None:
  244. L.append((o, value))
  245. return L
  246. def iteritems(self):
  247. for wr, value in self.data.iteritems():
  248. key = wr()
  249. if key is not None:
  250. yield key, value
  251. def iterkeyrefs(self):
  252. """Return an iterator that yields the weak references to the keys.
  253. The references are not guaranteed to be 'live' at the time
  254. they are used, so the result of calling the references needs
  255. to be checked before being used. This can be used to avoid
  256. creating references that will cause the garbage collector to
  257. keep the keys around longer than needed.
  258. """
  259. return self.data.iterkeys()
  260. def iterkeys(self):
  261. for wr in self.data.iterkeys():
  262. obj = wr()
  263. if obj is not None:
  264. yield obj
  265. def __iter__(self):
  266. return self.iterkeys()
  267. def itervalues(self):
  268. return self.data.itervalues()
  269. def keyrefs(self):
  270. """Return a list of weak references to the keys.
  271. The references are not guaranteed to be 'live' at the time
  272. they are used, so the result of calling the references needs
  273. to be checked before being used. This can be used to avoid
  274. creating references that will cause the garbage collector to
  275. keep the keys around longer than needed.
  276. """
  277. return self.data.keys()
  278. def keys(self):
  279. L = []
  280. for wr in self.data.keys():
  281. o = wr()
  282. if o is not None:
  283. L.append(o)
  284. return L
  285. def popitem(self):
  286. while 1:
  287. key, value = self.data.popitem()
  288. o = key()
  289. if o is not None:
  290. return o, value
  291. def pop(self, key, *args):
  292. return self.data.pop(ref(key), *args)
  293. def setdefault(self, key, default=None):
  294. return self.data.setdefault(ref(key, self._remove),default)
  295. def update(self, dict=None, **kwargs):
  296. d = self.data
  297. if dict is not None:
  298. if not hasattr(dict, "items"):
  299. dict = type({})(dict)
  300. for key, value in dict.items():
  301. d[ref(key, self._remove)] = value
  302. if len(kwargs):
  303. self.update(kwargs)