annotate Lib/weakref.py @ 74135:9c19df6c8ea0 3.2

News entry for Issue12798
author Senthil Kumaran <senthil@uthcode.com>
date Fri, 23 Dec 2011 11:11:21 +0800
parents a0cf64a5291e
children 1cd0688ff004
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """Weak reference support for Python.
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 This module is an implementation of PEP 205:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4
46664
031dfe455585 Merged revisions 62425-62429,62434-62436,62441,62444,62446-62448,62450-62455,62463,62465-62466,62469,62474,62476-62478,62480,62485,62492,62497-62498,62500,62507,62513-62514,62516,62521,62531,62535,62545-62546,62548-62551,62553-62559,62569,62574,62577,62593,62595,62604-62606,62608,62616,62626-62627,62636,62638,62644-62645,62647-62648,62651-62653,62656,62661,62663,62680,62686-62687,62696,62699-62703,62711 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45284
diff changeset
5 http://www.python.org/dev/peps/pep-0205/
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 """
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
8 # Naming convention: Variables named "wr" are weak reference objects;
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
9 # they are called this instead of "ref" to avoid name collisions with
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
10 # the module-global ref() function imported from _weakref.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
11
33356
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
12 from _weakref import (
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
13 getweakrefcount,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
14 getweakrefs,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
15 ref,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
16 proxy,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
17 CallableProxyType,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
18 ProxyType,
e56192e9fd7e Use multi-line import
Andrew M. Kuchling <amk@amk.ca>
parents: 33197
diff changeset
19 ReferenceType)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
21 from _weakrefset import WeakSet, _IterationGuard
20281
380d4b4ee602 ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents: 20155
diff changeset
22
52338
8b5a80f53317 Fix a circular import dependency.
Brett Cannon <bcannon@gmail.com>
parents: 49001
diff changeset
23 import collections # Import after _weakref to avoid circular import.
8b5a80f53317 Fix a circular import dependency.
Brett Cannon <bcannon@gmail.com>
parents: 49001
diff changeset
24
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 ProxyTypes = (ProxyType, CallableProxyType)
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26
17386
bbcc9837312e mapping(): Remove this function since it does not add anything to the API.
Fred Drake <fdrake@acm.org>
parents: 16889
diff changeset
27 __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
48739
3195bd6b6dd7 ReferenceError is undefined. causes test___all__.py to fail. remove it from __all__
Gregory P. Smith <greg@mad-scientist.com>
parents: 48733
diff changeset
28 "WeakKeyDictionary", "ReferenceType", "ProxyType",
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents: 43644
diff changeset
29 "CallableProxyType", "ProxyTypes", "WeakValueDictionary",
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents: 43644
diff changeset
30 "WeakSet"]
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
32
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
33 class WeakValueDictionary(collections.MutableMapping):
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
34 """Mapping class that references values weakly.
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
36 Entries in the dictionary will be discarded when no strong
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
37 reference to the value exists anymore
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
38 """
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39 # We inherit the constructor without worrying about the input
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 # dictionary; since it uses our .update() method, we get the right
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
41 # checks (if the other dictionary is a WeakValueDictionary,
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
42 # objects are unwrapped on the way out, and we always wrap on the
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
43 # way in).
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
45 def __init__(self, *args, **kw):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
46 def remove(wr, selfref=ref(self)):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
47 self = selfref()
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
48 if self is not None:
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
49 if self._iterating:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
50 self._pending_removals.append(wr.key)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
51 else:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
52 del self.data[wr.key]
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
53 self._remove = remove
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
54 # A list of keys to be removed
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
55 self._pending_removals = []
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
56 self._iterating = set()
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
57 self.data = d = {}
54456
49485f98a308 Issue #6149: Fix initialization of WeakValueDictionary objects from non-empty parameters.
Antoine Pitrou <solipsis@pitrou.net>
parents: 54100
diff changeset
58 self.update(*args, **kw)
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
59
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
60 def _commit_removals(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
61 l = self._pending_removals
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
62 d = self.data
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
63 # We shouldn't encounter any KeyError, because this method should
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
64 # always be called *before* mutating the dict.
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
65 while l:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
66 del d[l.pop()]
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
67
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 def __getitem__(self, key):
18952
e90a658a58d4 Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents: 17650
diff changeset
69 o = self.data[key]()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
70 if o is None:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42084
diff changeset
71 raise KeyError(key)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
72 else:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
73 return o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
74
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
75 def __delitem__(self, key):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
76 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
77 self._commit_removals()
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
78 del self.data[key]
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
79
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
80 def __len__(self):
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
81 return sum(wr() is not None for wr in self.data.values())
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
82
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
83 def __contains__(self, key):
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
84 try:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
85 o = self.data[key]()
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
86 except KeyError:
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
87 return False
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
88 return o is not None
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
89
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
90 def __repr__(self):
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
91 return "<WeakValueDictionary at %s>" % id(self)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
92
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
93 def __setitem__(self, key, value):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
94 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
95 self._commit_removals()
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
96 self.data[key] = KeyedRef(value, self._remove, key)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
97
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
98 def copy(self):
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
99 new = WeakValueDictionary()
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
100 for key, wr in self.data.items():
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
101 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102 if o is not None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
103 new[key] = o
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
104 return new
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
105
54100
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
106 __copy__ = copy
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
107
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
108 def __deepcopy__(self, memo):
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
109 from copy import deepcopy
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
110 new = self.__class__()
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
111 for key, wr in self.data.items():
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
112 o = wr()
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
113 if o is not None:
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
114 new[deepcopy(key, memo)] = o
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
115 return new
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
116
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
117 def get(self, key, default=None):
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
118 try:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
119 wr = self.data[key]
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
120 except KeyError:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
121 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
122 else:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
123 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
124 if o is None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
125 # This should only happen
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
126 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
127 else:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
128 return o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
129
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
130 def items(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
131 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
132 for k, wr in self.data.items():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
133 v = wr()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
134 if v is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
135 yield k, v
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
136
49001
5b7845ef4efd Committing the patch in issue 2965, so that weakref dicts have a closer
Barry Warsaw <barry@python.org>
parents: 48739
diff changeset
137 def keys(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
138 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
139 for k, wr in self.data.items():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
140 if wr() is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
141 yield k
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
142
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
143 __iter__ = keys
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
144
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
145 def itervaluerefs(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
146 """Return an iterator that yields the weak references to the values.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
147
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
148 The references are not guaranteed to be 'live' at the time
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
149 they are used, so the result of calling the references needs
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
150 to be checked before being used. This can be used to avoid
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
151 creating references that will cause the garbage collector to
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
152 keep the values around longer than needed.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
153
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
154 """
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
155 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
156 for wr in self.data.values():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
157 yield wr
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
158
49001
5b7845ef4efd Committing the patch in issue 2965, so that weakref dicts have a closer
Barry Warsaw <barry@python.org>
parents: 48739
diff changeset
159 def values(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
160 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
161 for wr in self.data.values():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
162 obj = wr()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
163 if obj is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
164 yield obj
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
165
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
166 def popitem(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
167 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
168 self._commit_removals()
66629
a0cf64a5291e Use booleans.
Georg Brandl <georg@python.org>
parents: 58194
diff changeset
169 while True:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
170 key, wr = self.data.popitem()
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
171 o = wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
172 if o is not None:
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
173 return key, o
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
174
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
175 def pop(self, key, *args):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
176 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
177 self._commit_removals()
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
178 try:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
179 o = self.data.pop(key)()
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
180 except KeyError:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
181 if args:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
182 return args[0]
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
183 raise
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
184 if o is None:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42084
diff changeset
185 raise KeyError(key)
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
186 else:
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
187 return o
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
188
32155
efb84ed7377d The default argument in dict.setdefault() defaults to None.
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
189 def setdefault(self, key, default=None):
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
190 try:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
191 wr = self.data[key]
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
192 except KeyError:
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
193 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
194 self._commit_removals()
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
195 self.data[key] = KeyedRef(default, self._remove, key)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
196 return default
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
197 else:
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
198 return wr()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
199
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
200 def update(self, dict=None, **kwargs):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
201 if self._pending_removals:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
202 self._commit_removals()
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
203 d = self.data
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
204 if dict is not None:
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
205 if not hasattr(dict, "items"):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
206 dict = type({})(dict)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
207 for key, o in dict.items():
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
208 d[key] = KeyedRef(o, self._remove, key)
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
209 if len(kwargs):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
210 self.update(kwargs)
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
211
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
212 def valuerefs(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
213 """Return a list of weak references to the values.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
214
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
215 The references are not guaranteed to be 'live' at the time
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
216 they are used, so the result of calling the references needs
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
217 to be checked before being used. This can be used to avoid
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
218 creating references that will cause the garbage collector to
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
219 keep the values around longer than needed.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
220
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
221 """
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
222 return list(self.data.values())
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
223
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
224
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
225 class KeyedRef(ref):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
226 """Specialized reference that includes a key corresponding to the value.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
227
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
228 This is used in the WeakValueDictionary to avoid having to create
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
229 a function object for each key stored in the mapping. A shared
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
230 callback object can use the 'key' attribute of a KeyedRef instead
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
231 of getting a reference to the key from an enclosing scope.
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
232
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
233 """
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
234
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
235 __slots__ = "key",
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
236
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
237 def __new__(type, ob, callback, key):
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
238 self = ref.__new__(type, ob, callback)
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
239 self.key = key
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
240 return self
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
241
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32155
diff changeset
242 def __init__(self, ob, callback, key):
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40949
diff changeset
243 super().__init__(ob, callback)
20155
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
244
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
245
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
246 class WeakKeyDictionary(collections.MutableMapping):
17591
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
247 """ Mapping class that references keys weakly.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
248
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
249 Entries in the dictionary will be discarded when there is no
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
250 longer a strong reference to the key. This can be used to
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
251 associate additional data with an object owned by other parts of
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
252 an application without adding attributes to those objects. This
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
253 can be especially useful with objects that override attribute
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
254 accesses.
44a3ac59c268 Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents: 17546
diff changeset
255 """
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
256
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
257 def __init__(self, dict=None):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
258 self.data = {}
20155
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
259 def remove(k, selfref=ref(self)):
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
260 self = selfref()
bbb353d31e25 Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents: 19763
diff changeset
261 if self is not None:
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
262 if self._iterating:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
263 self._pending_removals.append(k)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
264 else:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
265 del self.data[k]
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
266 self._remove = remove
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
267 # A list of dead weakrefs (keys to be removed)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
268 self._pending_removals = []
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
269 self._iterating = set()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
270 if dict is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
271 self.update(dict)
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
272
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
273 def _commit_removals(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
274 # NOTE: We don't need to call this method before mutating the dict,
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
275 # because a dead weakref never compares equal to a live weakref,
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
276 # even if they happened to refer to equal objects.
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
277 # However, it means keys may already have been removed.
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
278 l = self._pending_removals
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
279 d = self.data
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
280 while l:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
281 try:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
282 del d[l.pop()]
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
283 except KeyError:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
284 pass
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
285
19763
94d0f2eea047 Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
286 def __delitem__(self, key):
29257
dea38116c373 SF 742860: WeakKeyDictionary __delitem__ uses iterkeys
Tim Peters <tim.peters@gmail.com>
parents: 28240
diff changeset
287 del self.data[ref(key)]
19763
94d0f2eea047 Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents: 18952
diff changeset
288
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
289 def __getitem__(self, key):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
290 return self.data[ref(key)]
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
291
45161
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
292 def __len__(self):
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
293 return len(self.data)
bdf7486893ba Decouple weakref containers from UserDict
Raymond Hettinger <python@rcn.com>
parents: 45159
diff changeset
294
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
295 def __repr__(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
296 return "<WeakKeyDictionary at %s>" % id(self)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
297
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
298 def __setitem__(self, key, value):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
299 self.data[ref(key, self._remove)] = value
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
300
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
301 def copy(self):
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
302 new = WeakKeyDictionary()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
303 for key, value in self.data.items():
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
304 o = key()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
305 if o is not None:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
306 new[o] = value
16884
1ce751fa67d8 Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents: 16794
diff changeset
307 return new
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
308
54100
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
309 __copy__ = copy
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
310
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
311 def __deepcopy__(self, memo):
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
312 from copy import deepcopy
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
313 new = self.__class__()
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
314 for key, value in self.data.items():
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
315 o = key()
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
316 if o is not None:
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
317 new[o] = deepcopy(value, memo)
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
318 return new
85fb4c229a9c Merged revisions 72669 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52338
diff changeset
319
17546
da5eab33147b Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents: 17386
diff changeset
320 def get(self, key, default=None):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
321 return self.data.get(ref(key),default)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
322
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
323 def __contains__(self, key):
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
324 try:
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
325 wr = ref(key)
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
326 except TypeError:
66629
a0cf64a5291e Use booleans.
Georg Brandl <georg@python.org>
parents: 58194
diff changeset
327 return False
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 20902
diff changeset
328 return wr in self.data
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23764
diff changeset
329
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
330 def items(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
331 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
332 for wr, value in self.data.items():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
333 key = wr()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
334 if key is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
335 yield key, value
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
336
49001
5b7845ef4efd Committing the patch in issue 2965, so that weakref dicts have a closer
Barry Warsaw <barry@python.org>
parents: 48739
diff changeset
337 def keys(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
338 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
339 for wr in self.data:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
340 obj = wr()
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
341 if obj is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
342 yield obj
33197
7da9201bc8b5 SF bug #1012315: weakref.WeakValueDictionary should override .has_key()
Raymond Hettinger <python@rcn.com>
parents: 33117
diff changeset
343
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
344 __iter__ = keys
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
345
49001
5b7845ef4efd Committing the patch in issue 2965, so that weakref dicts have a closer
Barry Warsaw <barry@python.org>
parents: 48739
diff changeset
346 def values(self):
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
347 with _IterationGuard(self):
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
348 for wr, value in self.data.items():
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
349 if wr() is not None:
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
350 yield value
17650
7a367ad43f35 Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents: 17591
diff changeset
351
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
352 def keyrefs(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
353 """Return a list of weak references to the keys.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
354
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
355 The references are not guaranteed to be 'live' at the time
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
356 they are used, so the result of calling the references needs
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
357 to be checked before being used. This can be used to avoid
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
358 creating references that will cause the garbage collector to
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
359 keep the keys around longer than needed.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
360
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
361 """
58194
84caac71d79b Issue #7105: Make WeakKeyDictionary and WeakValueDictionary robust against
Antoine Pitrou <solipsis@pitrou.net>
parents: 54456
diff changeset
362 return list(self.data)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35018
diff changeset
363
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
364 def popitem(self):
66629
a0cf64a5291e Use booleans.
Georg Brandl <georg@python.org>
parents: 58194
diff changeset
365 while True:
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
366 key, value = self.data.popitem()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
367 o = key()
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
368 if o is not None:
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
369 return o, value
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
370
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
371 def pop(self, key, *args):
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
372 return self.data.pop(ref(key), *args)
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 24263
diff changeset
373
32155
efb84ed7377d The default argument in dict.setdefault() defaults to None.
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
374 def setdefault(self, key, default=None):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
375 return self.data.setdefault(ref(key, self._remove),default)
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
376
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
377 def update(self, dict=None, **kwargs):
16794
29aa832b8787 Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents: 16424
diff changeset
378 d = self.data
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
379 if dict is not None:
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
380 if not hasattr(dict, "items"):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
381 dict = type({})(dict)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
382 for key, value in dict.items():
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
383 d[ref(key, self._remove)] = value
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
384 if len(kwargs):
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 29257
diff changeset
385 self.update(kwargs)