Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1 | """Weak reference support for Python. |
| 2 | |
| 3 | This module is an implementation of PEP 205: |
| 4 | |
| 5 | http://python.sourceforge.net/peps/pep-0205.html |
| 6 | """ |
| 7 | |
| 8 | import UserDict |
| 9 | |
| 10 | from _weakref import \ |
| 11 | getweakrefcount, \ |
| 12 | getweakrefs, \ |
| 13 | ref, \ |
| 14 | proxy, \ |
| 15 | ReferenceError, \ |
| 16 | CallableProxyType, \ |
| 17 | ProxyType, \ |
| 18 | ReferenceType |
| 19 | |
| 20 | ProxyTypes = (ProxyType, CallableProxyType) |
| 21 | |
| 22 | |
| 23 | def mapping(dict=None): |
| 24 | return WeakDictionary(dict) |
| 25 | |
| 26 | |
| 27 | class WeakDictionary(UserDict.UserDict): |
| 28 | |
| 29 | # We inherit the constructor without worrying about the input |
| 30 | # dictionary; since it uses our .update() method, we get the right |
| 31 | # checks (if the other dictionary is a WeakDictionary, objects are |
| 32 | # unwrapped on the way out, and we always wrap on the way in). |
| 33 | |
| 34 | def __getitem__(self, key): |
| 35 | o = self.data.get(key)() |
| 36 | if o is None: |
| 37 | raise KeyError, key |
| 38 | else: |
| 39 | return o |
| 40 | |
| 41 | def __repr__(self): |
| 42 | return "<WeakDictionary at %s>" % id(self) |
| 43 | |
| 44 | def __setitem__(self, key, value): |
| 45 | def remove(o, data=self.data, key=key): |
| 46 | del data[key] |
| 47 | self.data[key] = ref(value, remove) |
| 48 | |
| 49 | def copy(self): |
| 50 | new = WeakDictionary() |
| 51 | for key, ref in self.data.items(): |
| 52 | o = ref() |
| 53 | if o is not None: |
| 54 | new[key] = o |
| 55 | |
| 56 | def get(self, key, default): |
| 57 | try: |
| 58 | ref = self.data[key] |
| 59 | except KeyError: |
| 60 | return default |
| 61 | else: |
| 62 | o = ref() |
| 63 | if o is None: |
| 64 | # This should only happen |
| 65 | return default |
| 66 | else: |
| 67 | return o |
| 68 | |
| 69 | def items(self): |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 70 | L = [] |
| 71 | for key, ref in self.data.items(): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 72 | o = ref() |
| 73 | if o is not None: |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 74 | L.append((key, o)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 75 | return L |
| 76 | |
| 77 | def popitem(self): |
| 78 | while 1: |
| 79 | key, ref = self.data.popitem() |
| 80 | o = ref() |
| 81 | if o is not None: |
| 82 | return key, o |
| 83 | |
| 84 | def setdefault(self, key, default): |
| 85 | try: |
| 86 | ref = self.data[key] |
| 87 | except KeyError: |
| 88 | def remove(o, data=self.data, key=key): |
| 89 | del data[key] |
| 90 | ref = ref(default, remove) |
| 91 | self.data[key] = ref |
| 92 | return default |
| 93 | else: |
| 94 | return ref() |
| 95 | |
| 96 | def update(self, dict): |
| 97 | d = self.data |
| 98 | L = [] |
| 99 | for key, o in dict.items(): |
| 100 | def remove(o, data=d, key=key): |
| 101 | del data[key] |
| 102 | L.append(key, ref(o, remove)) |
| 103 | for key, r in L: |
| 104 | d[key] = r |
| 105 | |
| 106 | def values(self): |
| 107 | L = [] |
| 108 | for ref in self.data.values(): |
| 109 | o = ref() |
| 110 | if o is not None: |
| 111 | L.append(o) |
| 112 | return L |
| 113 | |
| 114 | |
| 115 | # no longer needed |
| 116 | del UserDict |