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 | |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 8 | # Naming convention: Variables named "wr" are weak reference objects; |
| 9 | # they are called this instead of "ref" to avoid name collisions with |
| 10 | # the module-global ref() function imported from _weakref. |
| 11 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 12 | import UserDict |
| 13 | |
Andrew M. Kuchling | 33ad28b | 2004-08-31 11:38:12 +0000 | [diff] [blame] | 14 | from _weakref import ( |
| 15 | getweakrefcount, |
| 16 | getweakrefs, |
| 17 | ref, |
| 18 | proxy, |
| 19 | CallableProxyType, |
| 20 | ProxyType, |
| 21 | ReferenceType) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 22 | |
Fred Drake | e029242 | 2001-10-05 21:54:09 +0000 | [diff] [blame] | 23 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 24 | ProxyTypes = (ProxyType, CallableProxyType) |
| 25 | |
Fred Drake | 9a9d219 | 2001-04-10 19:11:23 +0000 | [diff] [blame] | 26 | __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 27 | "WeakKeyDictionary", "ReferenceType", "ProxyType", |
| 28 | "CallableProxyType", "ProxyTypes", "WeakValueDictionary"] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 29 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 30 | |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 31 | class WeakValueDictionary(UserDict.UserDict): |
| 32 | """Mapping class that references values weakly. |
| 33 | |
| 34 | Entries in the dictionary will be discarded when no strong |
| 35 | reference to the value exists anymore |
| 36 | """ |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 37 | # We inherit the constructor without worrying about the input |
| 38 | # dictionary; since it uses our .update() method, we get the right |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 39 | # checks (if the other dictionary is a WeakValueDictionary, |
| 40 | # objects are unwrapped on the way out, and we always wrap on the |
| 41 | # way in). |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 42 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 43 | def __init__(self, *args, **kw): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 44 | def remove(wr, selfref=ref(self)): |
| 45 | self = selfref() |
| 46 | if self is not None: |
| 47 | del self.data[wr.key] |
| 48 | self._remove = remove |
Georg Brandl | 9166e1a | 2005-06-04 09:20:03 +0000 | [diff] [blame] | 49 | UserDict.UserDict.__init__(self, *args, **kw) |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 50 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 51 | def __getitem__(self, key): |
Fred Drake | 4fd06e0 | 2001-08-03 04:11:27 +0000 | [diff] [blame] | 52 | o = self.data[key]() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 53 | if o is None: |
| 54 | raise KeyError, key |
| 55 | else: |
| 56 | return o |
| 57 | |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 58 | def __contains__(self, key): |
| 59 | try: |
| 60 | o = self.data[key]() |
| 61 | except KeyError: |
| 62 | return False |
| 63 | return o is not None |
| 64 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 65 | def __repr__(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 66 | return "<WeakValueDictionary at %s>" % id(self) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 67 | |
| 68 | def __setitem__(self, key, value): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 69 | self.data[key] = KeyedRef(value, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 70 | |
| 71 | def copy(self): |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 72 | new = WeakValueDictionary() |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 73 | for key, wr in self.data.items(): |
| 74 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 75 | if o is not None: |
| 76 | new[key] = o |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 77 | return new |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 78 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 79 | def get(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 80 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 81 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 82 | except KeyError: |
| 83 | return default |
| 84 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 85 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 86 | if o is None: |
| 87 | # This should only happen |
| 88 | return default |
| 89 | else: |
| 90 | return o |
| 91 | |
| 92 | def items(self): |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 93 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 94 | for key, wr in self.data.items(): |
| 95 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 96 | if o is not None: |
Fred Drake | 312a5dc | 2001-02-02 15:13:24 +0000 | [diff] [blame] | 97 | L.append((key, o)) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 98 | return L |
| 99 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 100 | def iteritems(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 101 | for wr in self.data.values(): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 102 | value = wr() |
| 103 | if value is not None: |
| 104 | yield wr.key, value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 105 | |
| 106 | def iterkeys(self): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 107 | return iter(self.data.keys()) |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 108 | |
| 109 | def __iter__(self): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 110 | return iter(self.data.keys()) |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 112 | def itervaluerefs(self): |
| 113 | """Return an iterator that yields the weak references to the values. |
| 114 | |
| 115 | The references are not guaranteed to be 'live' at the time |
| 116 | they are used, so the result of calling the references needs |
| 117 | to be checked before being used. This can be used to avoid |
| 118 | creating references that will cause the garbage collector to |
| 119 | keep the values around longer than needed. |
| 120 | |
| 121 | """ |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 122 | return self.data.values() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 123 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 124 | def itervalues(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 125 | for wr in self.data.values(): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 126 | obj = wr() |
| 127 | if obj is not None: |
| 128 | yield obj |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 129 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 130 | def popitem(self): |
| 131 | while 1: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 132 | key, wr = self.data.popitem() |
| 133 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 134 | if o is not None: |
| 135 | return key, o |
| 136 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 137 | def pop(self, key, *args): |
| 138 | try: |
| 139 | o = self.data.pop(key)() |
| 140 | except KeyError: |
| 141 | if args: |
| 142 | return args[0] |
| 143 | raise |
| 144 | if o is None: |
| 145 | raise KeyError, key |
| 146 | else: |
| 147 | return o |
| 148 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 149 | def setdefault(self, key, default=None): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 150 | try: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 151 | wr = self.data[key] |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 152 | except KeyError: |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 153 | self.data[key] = KeyedRef(default, self._remove, key) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 154 | return default |
| 155 | else: |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 156 | return wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 157 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 158 | def update(self, dict=None, **kwargs): |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 159 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 160 | if dict is not None: |
| 161 | if not hasattr(dict, "items"): |
| 162 | dict = type({})(dict) |
| 163 | for key, o in dict.items(): |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 164 | d[key] = KeyedRef(o, self._remove, key) |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 165 | if len(kwargs): |
| 166 | self.update(kwargs) |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 167 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 168 | def valuerefs(self): |
| 169 | """Return a list of weak references to the values. |
| 170 | |
| 171 | The references are not guaranteed to be 'live' at the time |
| 172 | they are used, so the result of calling the references needs |
| 173 | to be checked before being used. This can be used to avoid |
| 174 | creating references that will cause the garbage collector to |
| 175 | keep the values around longer than needed. |
| 176 | |
| 177 | """ |
| 178 | return self.data.values() |
| 179 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 180 | def values(self): |
| 181 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 182 | for wr in self.data.values(): |
| 183 | o = wr() |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 184 | if o is not None: |
| 185 | L.append(o) |
| 186 | return L |
| 187 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 188 | |
| 189 | class KeyedRef(ref): |
| 190 | """Specialized reference that includes a key corresponding to the value. |
| 191 | |
| 192 | This is used in the WeakValueDictionary to avoid having to create |
| 193 | a function object for each key stored in the mapping. A shared |
| 194 | callback object can use the 'key' attribute of a KeyedRef instead |
| 195 | of getting a reference to the key from an enclosing scope. |
| 196 | |
| 197 | """ |
| 198 | |
| 199 | __slots__ = "key", |
| 200 | |
| 201 | def __new__(type, ob, callback, key): |
| 202 | self = ref.__new__(type, ob, callback) |
| 203 | self.key = key |
| 204 | return self |
| 205 | |
| 206 | def __init__(self, ob, callback, key): |
| 207 | super(KeyedRef, self).__init__(ob, callback) |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 208 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 209 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 210 | class WeakKeyDictionary(UserDict.UserDict): |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 211 | """ Mapping class that references keys weakly. |
| 212 | |
| 213 | Entries in the dictionary will be discarded when there is no |
| 214 | longer a strong reference to the key. This can be used to |
| 215 | associate additional data with an object owned by other parts of |
| 216 | an application without adding attributes to those objects. This |
| 217 | can be especially useful with objects that override attribute |
| 218 | accesses. |
| 219 | """ |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 220 | |
| 221 | def __init__(self, dict=None): |
| 222 | self.data = {} |
Fred Drake | 746fe0f | 2001-09-28 19:01:26 +0000 | [diff] [blame] | 223 | def remove(k, selfref=ref(self)): |
| 224 | self = selfref() |
| 225 | if self is not None: |
| 226 | del self.data[k] |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 227 | self._remove = remove |
Guido van Rossum | 009afb7 | 2002-06-10 20:00:52 +0000 | [diff] [blame] | 228 | if dict is not None: self.update(dict) |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 229 | |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 230 | def __delitem__(self, key): |
Tim Peters | 886128f | 2003-05-25 01:45:11 +0000 | [diff] [blame] | 231 | del self.data[ref(key)] |
Fred Drake | b663a2c | 2001-09-06 14:51:01 +0000 | [diff] [blame] | 232 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 233 | def __getitem__(self, key): |
| 234 | return self.data[ref(key)] |
| 235 | |
| 236 | def __repr__(self): |
| 237 | return "<WeakKeyDictionary at %s>" % id(self) |
| 238 | |
| 239 | def __setitem__(self, key, value): |
| 240 | self.data[ref(key, self._remove)] = value |
| 241 | |
| 242 | def copy(self): |
| 243 | new = WeakKeyDictionary() |
| 244 | for key, value in self.data.items(): |
| 245 | o = key() |
| 246 | if o is not None: |
| 247 | new[o] = value |
Fred Drake | 9d2c85d | 2001-03-01 03:06:03 +0000 | [diff] [blame] | 248 | return new |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 249 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 250 | def get(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 251 | return self.data.get(ref(key),default) |
| 252 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 253 | def __contains__(self, key): |
| 254 | try: |
| 255 | wr = ref(key) |
| 256 | except TypeError: |
| 257 | return 0 |
| 258 | return wr in self.data |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 259 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 260 | def items(self): |
| 261 | L = [] |
| 262 | for key, value in self.data.items(): |
| 263 | o = key() |
| 264 | if o is not None: |
| 265 | L.append((o, value)) |
| 266 | return L |
| 267 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 268 | def iteritems(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 269 | for wr, value in self.data.items(): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 270 | key = wr() |
| 271 | if key is not None: |
| 272 | yield key, value |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 273 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 274 | def iterkeyrefs(self): |
| 275 | """Return an iterator that yields the weak references to the keys. |
| 276 | |
| 277 | The references are not guaranteed to be 'live' at the time |
| 278 | they are used, so the result of calling the references needs |
| 279 | to be checked before being used. This can be used to avoid |
| 280 | creating references that will cause the garbage collector to |
| 281 | keep the keys around longer than needed. |
| 282 | |
| 283 | """ |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 284 | return self.data.keys() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 285 | |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 286 | def iterkeys(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 287 | for wr in self.data.keys(): |
Raymond Hettinger | 6114679 | 2004-08-19 21:32:06 +0000 | [diff] [blame] | 288 | obj = wr() |
| 289 | if obj is not None: |
| 290 | yield obj |
| 291 | |
| 292 | def __iter__(self): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 293 | return iter(self.keys()) |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 294 | |
| 295 | def itervalues(self): |
Guido van Rossum | 07f2436 | 2007-02-11 22:59:48 +0000 | [diff] [blame] | 296 | return iter(self.data.values()) |
Fred Drake | 101209d | 2001-05-02 05:43:09 +0000 | [diff] [blame] | 297 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 298 | def keyrefs(self): |
| 299 | """Return a list of weak references to the keys. |
| 300 | |
| 301 | The references are not guaranteed to be 'live' at the time |
| 302 | they are used, so the result of calling the references needs |
| 303 | to be checked before being used. This can be used to avoid |
| 304 | creating references that will cause the garbage collector to |
| 305 | keep the keys around longer than needed. |
| 306 | |
| 307 | """ |
| 308 | return self.data.keys() |
| 309 | |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 310 | def keys(self): |
| 311 | L = [] |
Fred Drake | bd7f818 | 2001-04-19 16:26:06 +0000 | [diff] [blame] | 312 | for wr in self.data.keys(): |
| 313 | o = wr() |
Fred Drake | 1d9e4b7 | 2001-04-16 17:34:48 +0000 | [diff] [blame] | 314 | if o is not None: |
| 315 | L.append(o) |
| 316 | return L |
| 317 | |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 318 | def popitem(self): |
| 319 | while 1: |
| 320 | key, value = self.data.popitem() |
| 321 | o = key() |
| 322 | if o is not None: |
| 323 | return o, value |
| 324 | |
Raymond Hettinger | 2c2d322 | 2003-03-09 07:05:43 +0000 | [diff] [blame] | 325 | def pop(self, key, *args): |
| 326 | return self.data.pop(ref(key), *args) |
| 327 | |
Walter Dörwald | 80ce6dd | 2004-05-27 18:16:25 +0000 | [diff] [blame] | 328 | def setdefault(self, key, default=None): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 329 | return self.data.setdefault(ref(key, self._remove),default) |
| 330 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 331 | def update(self, dict=None, **kwargs): |
Martin v. Löwis | 5e16333 | 2001-02-27 18:36:56 +0000 | [diff] [blame] | 332 | d = self.data |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 333 | if dict is not None: |
| 334 | if not hasattr(dict, "items"): |
| 335 | dict = type({})(dict) |
| 336 | for key, value in dict.items(): |
| 337 | d[ref(key, self._remove)] = value |
| 338 | if len(kwargs): |
| 339 | self.update(kwargs) |