Mercurial > cpython-lru_cache-weakref
annotate Lib/weakref.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 5e4eadf1903a |
| children | bd349999cb08 |
| 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 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 def setdefault(self, key, default): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 try: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
106 wr = self.data[key] |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 except KeyError: |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
108 self.data[key] = ref(default, self.__makeremove(key)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 return default |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 else: |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
111 return wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 def update(self, dict): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 d = self.data |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 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
|
116 d[key] = ref(o, self.__makeremove(key)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 def values(self): |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 L = [] |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
120 for wr in self.data.values(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
121 o = wr() |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 if o is not None: |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 L.append(o) |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 return L |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
126 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
|
127 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
|
128 self = selfref() |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
129 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
|
130 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
|
131 return remove |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
132 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
134 class WeakKeyDictionary(UserDict.UserDict): |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
135 """ Mapping class that references keys weakly. |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
136 |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 accesses. |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
143 """ |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
144 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
145 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
|
146 self.data = {} |
|
20155
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
147 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
|
148 self = selfref() |
|
bbb353d31e25
Clean up circular references in the Weak*Dictionary classes; this avoids
Fred Drake <fdrake@acm.org>
parents:
19763
diff
changeset
|
149 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
|
150 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
|
151 self._remove = remove |
|
24994
5e4eadf1903a
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20902
diff
changeset
|
152 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
|
153 |
|
19763
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
154 def __delitem__(self, key): |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
155 for ref in self.data.iterkeys(): |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
156 o = ref() |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
157 if o == key: |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
158 del self.data[ref] |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
159 return |
|
94d0f2eea047
Add __delitem__() support for WeakKeyDictionary.
Fred Drake <fdrake@acm.org>
parents:
18952
diff
changeset
|
160 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
161 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
|
162 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
|
163 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
164 def __repr__(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
165 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
|
166 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
167 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
|
168 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
|
169 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
170 def copy(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
171 new = WeakKeyDictionary() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
172 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
|
173 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
174 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
|
175 new[o] = value |
|
16884
1ce751fa67d8
Change WeakDictionary to WeakValueDictionary in a couple more places.
Fred Drake <fdrake@acm.org>
parents:
16794
diff
changeset
|
176 return new |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
177 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
178 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
|
179 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
|
180 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
181 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
|
182 try: |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
183 wr = ref(key) |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
184 except TypeError: |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
185 return 0 |
|
d3f3d21ac04b
WeakKeyDictionary.has_key(): If the key being tested is not weakly
Fred Drake <fdrake@acm.org>
parents:
20281
diff
changeset
|
186 return self.data.has_key(wr) |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
187 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
188 def items(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
189 L = [] |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
190 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
|
191 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
192 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
|
193 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
|
194 return L |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
195 |
|
17650
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
196 def iteritems(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
197 return WeakKeyedItemIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
198 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
199 def iterkeys(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
200 return WeakKeyedKeyIterator(self) |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
201 __iter__ = iterkeys |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
202 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
203 def itervalues(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
204 return self.data.itervalues() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
205 |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
206 def keys(self): |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
207 L = [] |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
208 for wr in self.data.keys(): |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
209 o = wr() |
|
17546
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
210 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
|
211 L.append(o) |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
212 return L |
|
da5eab33147b
Weak*Dictionary.update(): Fix calls to [].append() to only have one
Fred Drake <fdrake@acm.org>
parents:
17386
diff
changeset
|
213 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
214 def popitem(self): |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
215 while 1: |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
216 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
|
217 o = key() |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
218 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
|
219 return o, value |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
220 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
221 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
|
222 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
|
223 |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
224 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
|
225 d = self.data |
|
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
226 for key, value in dict.items(): |
|
17591
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
227 d[ref(key, self._remove)] = value |
|
44a3ac59c268
Weak*Dictionary: Added docstrings to the classes.
Fred Drake <fdrake@acm.org>
parents:
17546
diff
changeset
|
228 |
|
16794
29aa832b8787
Patch #403985: Add support for weak-keyed dictionaries
Martin v. Löwis <martin@v.loewis.de>
parents:
16424
diff
changeset
|
229 |
|
17650
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
230 class BaseIter: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
231 def __iter__(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
232 return self |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
233 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
234 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
235 class WeakKeyedKeyIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
236 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
237 self._next = weakdict.data.iterkeys().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
238 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
239 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
240 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
241 wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
242 obj = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
243 if obj is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
244 return obj |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
245 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
246 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
247 class WeakKeyedItemIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
248 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
249 self._next = weakdict.data.iteritems().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
250 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
251 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
252 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
253 wr, value = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
254 key = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
255 if key is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
256 return key, value |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
257 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
258 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
259 class WeakValuedValueIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
260 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
261 self._next = weakdict.data.itervalues().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
262 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
263 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
264 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
265 wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
266 obj = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
267 if obj is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
268 return obj |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
269 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
270 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
271 class WeakValuedItemIterator(BaseIter): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
272 def __init__(self, weakdict): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
273 self._next = weakdict.data.iteritems().next |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
274 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
275 def next(self): |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
276 while 1: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
277 key, wr = self._next() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
278 value = wr() |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
279 if value is not None: |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
280 return key, value |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
281 |
|
7a367ad43f35
Added iterator support to the Weak*Dictionary classes.
Fred Drake <fdrake@acm.org>
parents:
17591
diff
changeset
|
282 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
283 # no longer needed |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 del UserDict |
