Mercurial > cpython-lru_cache-weakref
annotate Lib/weakref.py @ 28646:193b8aec00ce legacy-trunk
Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to
work. This includes some more code that used to be part of pgen in
the main parser; I'm okay with that. I'll see if the Windows build
needs work next.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Apr 2003 14:55:42 +0000 |
| parents | ea70032a24b1 |
| children | dea38116c373 |
| 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 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 http://python.sourceforge.net/peps/pep-0205.html |
|
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 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 import UserDict |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 from _weakref import \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 getweakrefcount, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 getweakrefs, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 ref, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 proxy, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 CallableProxyType, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 ProxyType, \ |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 ReferenceType |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 |
|
20281
380d4b4ee602
ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents:
20155
diff
changeset
|
23 from exceptions import ReferenceError |
|
380d4b4ee602
ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents:
20155
diff
changeset
|
24 |
|
380d4b4ee602
ReferenceError is now built-in, so pick it up from the right place.
Fred Drake <fdrake@acm.org>
parents:
20155
diff
changeset
|
25 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 ProxyTypes = (ProxyType, CallableProxyType) |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 |
|
17386
bbcc9837312e
mapping(): Remove this function since it does not add anything to the API.
Fred Drake <fdrake@acm.org>
parents:
16889
diff
changeset
|
28 __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16884
diff
changeset
|
29 "WeakKeyDictionary", "ReferenceType", "ProxyType", |
|
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
16884
diff
changeset
|
30 "CallableProxyType", "ProxyTypes", "WeakValueDictionary"] |
|
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 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
33 class WeakValueDictionary(UserDict.UserDict): |
|
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 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 def __getitem__(self, key): |
|
18952
e90a658a58d4
Make sure that WeakValueDictionary[] raises KeyError instead of TypeError
Fred Drake <fdrake@acm.org>
parents:
17650
diff
changeset
|
46 o = self.data[key]() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 if o is None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 raise KeyError, key |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 else: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 return o |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 def __repr__(self): |
|
16884
1ce751fa67d8
Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents:
16794
diff
changeset
|
53 return "<WeakValueDictionary at %s>" % id(self) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 def __setitem__(self, key, value): |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
56 self.data[key] = ref(value, self.__makeremove(key)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 def copy(self): |
|
16884
1ce751fa67d8
Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents:
16794
diff
changeset
|
59 new = WeakValueDictionary() |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
60 for key, wr in self.data.items(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
61 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 if o is not None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 new[key] = o |
|
16884
1ce751fa67d8
Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents:
16794
diff
changeset
|
64 return new |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
66 def get(self, key, default=None): |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
67 try: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
68 wr = self.data[key] |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 except KeyError: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
70 return default |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
71 else: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
72 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 if o is None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 # This should only happen |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 return default |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 else: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 return o |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 def items(self): |
|
16417
901b94656928
WeakDictionary.items(): Do not allow (key,ref) pairs to leak out for
Fred Drake <fdrake@acm.org>
parents:
16385
diff
changeset
|
80 L = [] |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
81 for key, wr in self.data.items(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
82 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 if o is not None: |
|
16417
901b94656928
WeakDictionary.items(): Do not allow (key,ref) pairs to leak out for
Fred Drake <fdrake@acm.org>
parents:
16385
diff
changeset
|
84 L.append((key, o)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 return L |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 |
|
17650
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
87 def iteritems(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
88 return WeakValuedItemIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
89 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
90 def iterkeys(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
91 return self.data.iterkeys() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
92 __iter__ = iterkeys |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
93 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
94 def itervalues(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
95 return WeakValuedValueIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
96 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 def popitem(self): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 while 1: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
99 key, wr = self.data.popitem() |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
100 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 if o is not None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 return key, o |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 |
|
28240
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
104 def pop(self, key, *args): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
105 try: |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
106 o = self.data.pop(key)() |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
107 except KeyError: |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
108 if args: |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
109 return args[0] |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
110 raise |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
111 if o is None: |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
112 raise KeyError, key |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
113 else: |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
114 return o |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
115 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 def setdefault(self, key, default): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 try: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
118 wr = self.data[key] |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 except KeyError: |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
120 self.data[key] = ref(default, self.__makeremove(key)) |
|
16385
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 return wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 def update(self, dict): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 d = self.data |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 for key, o in dict.items(): |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
128 d[key] = ref(o, self.__makeremove(key)) |
|
16385
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 values(self): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 L = [] |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
132 for wr in self.data.values(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
133 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 if o is not None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 L.append(o) |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 return L |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
138 def __makeremove(self, key): |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
139 def remove(o, selfref=ref(self), key=key): |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
140 self = selfref() |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
141 if self is not None: |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
142 del self.data[key] |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
143 return remove |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
144 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
146 class WeakKeyDictionary(UserDict.UserDict): |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
147 """ Mapping class that references keys weakly. |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
148 |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 accesses. |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
155 """ |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
156 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
157 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
|
158 self.data = {} |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
159 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
|
160 self = selfref() |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
161 if self is not None: |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
162 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
|
163 self._remove = remove |
|
23764
d58fb5445118
SF patch 564549 (Erik Andersén).
Guido van Rossum <guido@python.org>
parents:
23620
diff
changeset
|
164 if dict is not None: self.update(dict) |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
165 |
|
19763
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
166 def __delitem__(self, key): |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
167 for ref in self.data.iterkeys(): |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
168 o = ref() |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
169 if o == key: |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
170 del self.data[ref] |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
171 return |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
172 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
173 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
|
174 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
|
175 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
176 def __repr__(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
177 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
|
178 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
179 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
|
180 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
|
181 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
182 def copy(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
183 new = WeakKeyDictionary() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
184 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
|
185 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
186 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
|
187 new[o] = value |
|
16884
1ce751fa67d8
Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents:
16794
diff
changeset
|
188 return new |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
189 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
190 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
|
191 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
|
192 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
193 def has_key(self, key): |
|
20902
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
194 try: |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
195 wr = ref(key) |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
196 except TypeError: |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
197 return 0 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
198 return wr in self.data |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
199 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
200 def __contains__(self, key): |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
201 try: |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
202 wr = ref(key) |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
203 except TypeError: |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
204 return 0 |
|
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
20902
diff
changeset
|
205 return wr in self.data |
|
24263
e6033254df21
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23764
diff
changeset
|
206 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
207 def items(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
208 L = [] |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
209 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
|
210 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
211 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
|
212 L.append((o, value)) |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
213 return L |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
214 |
|
17650
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
215 def iteritems(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
216 return WeakKeyedItemIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
217 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
218 def iterkeys(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
219 return WeakKeyedKeyIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
220 __iter__ = iterkeys |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
221 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
222 def itervalues(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
223 return self.data.itervalues() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
224 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
225 def keys(self): |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
226 L = [] |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
227 for wr in self.data.keys(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
228 o = wr() |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
229 if o is not None: |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
230 L.append(o) |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
231 return L |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
232 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
233 def popitem(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
234 while 1: |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
235 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
|
236 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
237 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
|
238 return o, value |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
239 |
|
28240
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
240 def pop(self, key, *args): |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
241 return self.data.pop(ref(key), *args) |
|
ea70032a24b1
SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents:
24263
diff
changeset
|
242 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
243 def setdefault(self, key, default): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
244 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
|
245 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
246 def update(self, dict): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
247 d = self.data |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
248 for key, value in dict.items(): |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
249 d[ref(key, self._remove)] = value |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
250 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
251 |
|
17650
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
252 class BaseIter: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
253 def __iter__(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
254 return self |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
255 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
256 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
257 class WeakKeyedKeyIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
258 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
259 self._next = weakdict.data.iterkeys().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
260 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
261 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
262 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
263 wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
264 obj = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
265 if obj is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
266 return obj |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
267 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
268 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
269 class WeakKeyedItemIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
270 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
271 self._next = weakdict.data.iteritems().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
272 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
273 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
274 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
275 wr, value = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
276 key = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
277 if key is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
278 return key, value |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
279 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
280 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
281 class WeakValuedValueIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
282 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
283 self._next = weakdict.data.itervalues().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
284 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
285 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
286 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
287 wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
288 obj = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
289 if obj is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
290 return obj |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
291 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
292 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
293 class WeakValuedItemIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
294 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
295 self._next = weakdict.data.iteritems().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
296 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
297 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
298 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
299 key, wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
300 value = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
301 if value is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
302 return key, value |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
303 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
304 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
305 # no longer needed |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
306 del UserDict |
