blob: c66943f02e24a7807be3e960461ba128beefdc3f [file] [log] [blame]
Fred Drake41deb1e2001-02-01 05:27:45 +00001"""Weak reference support for Python.
2
3This module is an implementation of PEP 205:
4
Alexandre Vassalottiba08f072008-04-27 00:52:24 +00005http://www.python.org/dev/peps/pep-0205/
Fred Drake41deb1e2001-02-01 05:27:45 +00006"""
7
Fred Drakebd7f8182001-04-19 16:26:06 +00008# 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 Drake41deb1e2001-02-01 05:27:45 +000012import UserDict
13
Andrew M. Kuchling33ad28b2004-08-31 11:38:12 +000014from _weakref import (
15 getweakrefcount,
16 getweakrefs,
17 ref,
18 proxy,
19 CallableProxyType,
20 ProxyType,
Antoine Pitrouf939b3c2016-12-27 15:08:27 +010021 ReferenceType,
22 _remove_dead_weakref)
Fred Drake41deb1e2001-02-01 05:27:45 +000023
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000024from _weakrefset import WeakSet, _IterationGuard
Michael Foorde6410c52010-03-29 20:04:23 +000025
Fred Drakee0292422001-10-05 21:54:09 +000026from exceptions import ReferenceError
27
28
Fred Drake41deb1e2001-02-01 05:27:45 +000029ProxyTypes = (ProxyType, CallableProxyType)
30
Fred Drake9a9d2192001-04-10 19:11:23 +000031__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
Brett Cannon88f801d2008-08-18 00:46:22 +000032 "WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
Michael Foorde6410c52010-03-29 20:04:23 +000033 "CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
Fred Drake41deb1e2001-02-01 05:27:45 +000034
Fred Drake41deb1e2001-02-01 05:27:45 +000035
Fred Drakebd7f8182001-04-19 16:26:06 +000036class WeakValueDictionary(UserDict.UserDict):
37 """Mapping class that references values weakly.
38
39 Entries in the dictionary will be discarded when no strong
40 reference to the value exists anymore
41 """
Fred Drake41deb1e2001-02-01 05:27:45 +000042 # We inherit the constructor without worrying about the input
43 # dictionary; since it uses our .update() method, we get the right
Fred Drake9d2c85d2001-03-01 03:06:03 +000044 # checks (if the other dictionary is a WeakValueDictionary,
45 # objects are unwrapped on the way out, and we always wrap on the
46 # way in).
Fred Drake41deb1e2001-02-01 05:27:45 +000047
Serhiy Storchakaf522bbc2015-09-29 23:51:27 +030048 def __init__(*args, **kw):
49 if not args:
50 raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
51 "object needs an argument")
52 self = args[0]
53 args = args[1:]
54 if len(args) > 1:
55 raise TypeError('expected at most 1 arguments, got %d' % len(args))
Fred Drake0a4dd392004-07-02 18:57:45 +000056 def remove(wr, selfref=ref(self)):
57 self = selfref()
58 if self is not None:
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000059 if self._iterating:
60 self._pending_removals.append(wr.key)
61 else:
Antoine Pitrouf939b3c2016-12-27 15:08:27 +010062 # Atomic removal is necessary since this function
63 # can be called asynchronously by the GC
64 _remove_dead_weakref(self.data, wr.key)
Fred Drake0a4dd392004-07-02 18:57:45 +000065 self._remove = remove
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000066 # A list of keys to be removed
67 self._pending_removals = []
68 self._iterating = set()
Georg Brandl9166e1a2005-06-04 09:20:03 +000069 UserDict.UserDict.__init__(self, *args, **kw)
Fred Drake0a4dd392004-07-02 18:57:45 +000070
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000071 def _commit_removals(self):
72 l = self._pending_removals
73 d = self.data
74 # We shouldn't encounter any KeyError, because this method should
75 # always be called *before* mutating the dict.
76 while l:
Antoine Pitrouf939b3c2016-12-27 15:08:27 +010077 key = l.pop()
78 _remove_dead_weakref(d, key)
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000079
Fred Drake41deb1e2001-02-01 05:27:45 +000080 def __getitem__(self, key):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +010081 if self._pending_removals:
82 self._commit_removals()
Fred Drake4fd06e02001-08-03 04:11:27 +000083 o = self.data[key]()
Fred Drake41deb1e2001-02-01 05:27:45 +000084 if o is None:
85 raise KeyError, key
86 else:
87 return o
88
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +000089 def __delitem__(self, key):
90 if self._pending_removals:
91 self._commit_removals()
92 del self.data[key]
93
Raymond Hettinger61146792004-08-19 21:32:06 +000094 def __contains__(self, key):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +010095 if self._pending_removals:
96 self._commit_removals()
Raymond Hettinger61146792004-08-19 21:32:06 +000097 try:
98 o = self.data[key]()
99 except KeyError:
100 return False
101 return o is not None
102
103 def has_key(self, key):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100104 if self._pending_removals:
105 self._commit_removals()
Raymond Hettinger61146792004-08-19 21:32:06 +0000106 try:
107 o = self.data[key]()
108 except KeyError:
109 return False
110 return o is not None
111
Fred Drake41deb1e2001-02-01 05:27:45 +0000112 def __repr__(self):
Fred Drake9d2c85d2001-03-01 03:06:03 +0000113 return "<WeakValueDictionary at %s>" % id(self)
Fred Drake41deb1e2001-02-01 05:27:45 +0000114
115 def __setitem__(self, key, value):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000116 if self._pending_removals:
117 self._commit_removals()
Fred Drake0a4dd392004-07-02 18:57:45 +0000118 self.data[key] = KeyedRef(value, self._remove, key)
Fred Drake41deb1e2001-02-01 05:27:45 +0000119
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000120 def clear(self):
121 if self._pending_removals:
122 self._commit_removals()
123 self.data.clear()
124
Fred Drake41deb1e2001-02-01 05:27:45 +0000125 def copy(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100126 if self._pending_removals:
127 self._commit_removals()
Fred Drake9d2c85d2001-03-01 03:06:03 +0000128 new = WeakValueDictionary()
Fred Drakebd7f8182001-04-19 16:26:06 +0000129 for key, wr in self.data.items():
130 o = wr()
Fred Drake41deb1e2001-02-01 05:27:45 +0000131 if o is not None:
132 new[key] = o
Fred Drake9d2c85d2001-03-01 03:06:03 +0000133 return new
Fred Drake41deb1e2001-02-01 05:27:45 +0000134
Antoine Pitrou775fd662009-05-15 16:54:52 +0000135 __copy__ = copy
136
137 def __deepcopy__(self, memo):
138 from copy import deepcopy
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100139 if self._pending_removals:
140 self._commit_removals()
Antoine Pitrou775fd662009-05-15 16:54:52 +0000141 new = self.__class__()
142 for key, wr in self.data.items():
143 o = wr()
144 if o is not None:
145 new[deepcopy(key, memo)] = o
146 return new
147
Fred Drake1d9e4b72001-04-16 17:34:48 +0000148 def get(self, key, default=None):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100149 if self._pending_removals:
150 self._commit_removals()
Fred Drake41deb1e2001-02-01 05:27:45 +0000151 try:
Fred Drakebd7f8182001-04-19 16:26:06 +0000152 wr = self.data[key]
Fred Drake41deb1e2001-02-01 05:27:45 +0000153 except KeyError:
154 return default
155 else:
Fred Drakebd7f8182001-04-19 16:26:06 +0000156 o = wr()
Fred Drake41deb1e2001-02-01 05:27:45 +0000157 if o is None:
158 # This should only happen
159 return default
160 else:
161 return o
162
163 def items(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100164 if self._pending_removals:
165 self._commit_removals()
Fred Drake312a5dc2001-02-02 15:13:24 +0000166 L = []
Fred Drakebd7f8182001-04-19 16:26:06 +0000167 for key, wr in self.data.items():
168 o = wr()
Fred Drake41deb1e2001-02-01 05:27:45 +0000169 if o is not None:
Fred Drake312a5dc2001-02-02 15:13:24 +0000170 L.append((key, o))
Fred Drake41deb1e2001-02-01 05:27:45 +0000171 return L
172
Fred Drake101209d2001-05-02 05:43:09 +0000173 def iteritems(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100174 if self._pending_removals:
175 self._commit_removals()
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000176 with _IterationGuard(self):
177 for wr in self.data.itervalues():
178 value = wr()
179 if value is not None:
180 yield wr.key, value
Fred Drake101209d2001-05-02 05:43:09 +0000181
182 def iterkeys(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100183 if self._pending_removals:
184 self._commit_removals()
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000185 with _IterationGuard(self):
186 for k in self.data.iterkeys():
187 yield k
Raymond Hettinger61146792004-08-19 21:32:06 +0000188
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000189 __iter__ = iterkeys
Fred Drake101209d2001-05-02 05:43:09 +0000190
Fred Drake017e68c2006-05-02 06:53:59 +0000191 def itervaluerefs(self):
192 """Return an iterator that yields the weak references to the values.
193
194 The references are not guaranteed to be 'live' at the time
195 they are used, so the result of calling the references needs
196 to be checked before being used. This can be used to avoid
197 creating references that will cause the garbage collector to
198 keep the values around longer than needed.
199
200 """
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100201 if self._pending_removals:
202 self._commit_removals()
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000203 with _IterationGuard(self):
204 for wr in self.data.itervalues():
205 yield wr
Fred Drake017e68c2006-05-02 06:53:59 +0000206
Fred Drake101209d2001-05-02 05:43:09 +0000207 def itervalues(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100208 if self._pending_removals:
209 self._commit_removals()
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000210 with _IterationGuard(self):
211 for wr in self.data.itervalues():
212 obj = wr()
213 if obj is not None:
214 yield obj
Fred Drake101209d2001-05-02 05:43:09 +0000215
Fred Drake41deb1e2001-02-01 05:27:45 +0000216 def popitem(self):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000217 if self._pending_removals:
218 self._commit_removals()
Fred Drake41deb1e2001-02-01 05:27:45 +0000219 while 1:
Fred Drakebd7f8182001-04-19 16:26:06 +0000220 key, wr = self.data.popitem()
221 o = wr()
Fred Drake41deb1e2001-02-01 05:27:45 +0000222 if o is not None:
223 return key, o
224
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000225 def pop(self, key, *args):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000226 if self._pending_removals:
227 self._commit_removals()
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000228 try:
229 o = self.data.pop(key)()
230 except KeyError:
Antoine Pitrou805f2832016-12-19 11:12:58 +0100231 o = None
232 if o is None:
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000233 if args:
234 return args[0]
Antoine Pitrou805f2832016-12-19 11:12:58 +0100235 else:
236 raise KeyError, key
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000237 else:
238 return o
239
Walter Dörwald80ce6dd2004-05-27 18:16:25 +0000240 def setdefault(self, key, default=None):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100241 if self._pending_removals:
242 self._commit_removals()
Fred Drake41deb1e2001-02-01 05:27:45 +0000243 try:
Antoine Pitrou805f2832016-12-19 11:12:58 +0100244 o = self.data[key]()
Fred Drake41deb1e2001-02-01 05:27:45 +0000245 except KeyError:
Antoine Pitrou805f2832016-12-19 11:12:58 +0100246 o = None
247 if o is None:
Fred Drake0a4dd392004-07-02 18:57:45 +0000248 self.data[key] = KeyedRef(default, self._remove, key)
Fred Drake41deb1e2001-02-01 05:27:45 +0000249 return default
250 else:
Antoine Pitrou805f2832016-12-19 11:12:58 +0100251 return o
Fred Drake41deb1e2001-02-01 05:27:45 +0000252
Serhiy Storchakaf522bbc2015-09-29 23:51:27 +0300253 def update(*args, **kwargs):
254 if not args:
255 raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
256 "object needs an argument")
257 self = args[0]
258 args = args[1:]
259 if len(args) > 1:
260 raise TypeError('expected at most 1 arguments, got %d' % len(args))
261 dict = args[0] if args else None
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000262 if self._pending_removals:
263 self._commit_removals()
Fred Drake41deb1e2001-02-01 05:27:45 +0000264 d = self.data
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000265 if dict is not None:
266 if not hasattr(dict, "items"):
267 dict = type({})(dict)
268 for key, o in dict.items():
Fred Drake0a4dd392004-07-02 18:57:45 +0000269 d[key] = KeyedRef(o, self._remove, key)
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000270 if len(kwargs):
271 self.update(kwargs)
Fred Drake41deb1e2001-02-01 05:27:45 +0000272
Fred Drake017e68c2006-05-02 06:53:59 +0000273 def valuerefs(self):
274 """Return a list of weak references to the values.
275
276 The references are not guaranteed to be 'live' at the time
277 they are used, so the result of calling the references needs
278 to be checked before being used. This can be used to avoid
279 creating references that will cause the garbage collector to
280 keep the values around longer than needed.
281
282 """
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100283 if self._pending_removals:
284 self._commit_removals()
Fred Drake017e68c2006-05-02 06:53:59 +0000285 return self.data.values()
286
Fred Drake41deb1e2001-02-01 05:27:45 +0000287 def values(self):
Antoine Pitrouf939b3c2016-12-27 15:08:27 +0100288 if self._pending_removals:
289 self._commit_removals()
Fred Drake41deb1e2001-02-01 05:27:45 +0000290 L = []
Fred Drakebd7f8182001-04-19 16:26:06 +0000291 for wr in self.data.values():
292 o = wr()
Fred Drake41deb1e2001-02-01 05:27:45 +0000293 if o is not None:
294 L.append(o)
295 return L
296
Fred Drake0a4dd392004-07-02 18:57:45 +0000297
298class KeyedRef(ref):
299 """Specialized reference that includes a key corresponding to the value.
300
301 This is used in the WeakValueDictionary to avoid having to create
302 a function object for each key stored in the mapping. A shared
303 callback object can use the 'key' attribute of a KeyedRef instead
304 of getting a reference to the key from an enclosing scope.
305
306 """
307
308 __slots__ = "key",
309
310 def __new__(type, ob, callback, key):
311 self = ref.__new__(type, ob, callback)
312 self.key = key
313 return self
314
315 def __init__(self, ob, callback, key):
316 super(KeyedRef, self).__init__(ob, callback)
Fred Drake746fe0f2001-09-28 19:01:26 +0000317
Fred Drake41deb1e2001-02-01 05:27:45 +0000318
Martin v. Löwis5e163332001-02-27 18:36:56 +0000319class WeakKeyDictionary(UserDict.UserDict):
Fred Drakebd7f8182001-04-19 16:26:06 +0000320 """ Mapping class that references keys weakly.
321
322 Entries in the dictionary will be discarded when there is no
323 longer a strong reference to the key. This can be used to
324 associate additional data with an object owned by other parts of
325 an application without adding attributes to those objects. This
326 can be especially useful with objects that override attribute
327 accesses.
328 """
Martin v. Löwis5e163332001-02-27 18:36:56 +0000329
330 def __init__(self, dict=None):
331 self.data = {}
Fred Drake746fe0f2001-09-28 19:01:26 +0000332 def remove(k, selfref=ref(self)):
333 self = selfref()
334 if self is not None:
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000335 if self._iterating:
336 self._pending_removals.append(k)
337 else:
338 del self.data[k]
Martin v. Löwis5e163332001-02-27 18:36:56 +0000339 self._remove = remove
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000340 # A list of dead weakrefs (keys to be removed)
341 self._pending_removals = []
342 self._iterating = set()
343 if dict is not None:
344 self.update(dict)
345
346 def _commit_removals(self):
347 # NOTE: We don't need to call this method before mutating the dict,
348 # because a dead weakref never compares equal to a live weakref,
349 # even if they happened to refer to equal objects.
350 # However, it means keys may already have been removed.
351 l = self._pending_removals
352 d = self.data
353 while l:
354 try:
355 del d[l.pop()]
356 except KeyError:
357 pass
Martin v. Löwis5e163332001-02-27 18:36:56 +0000358
Fred Drakeb663a2c2001-09-06 14:51:01 +0000359 def __delitem__(self, key):
Tim Peters886128f2003-05-25 01:45:11 +0000360 del self.data[ref(key)]
Fred Drakeb663a2c2001-09-06 14:51:01 +0000361
Martin v. Löwis5e163332001-02-27 18:36:56 +0000362 def __getitem__(self, key):
363 return self.data[ref(key)]
364
365 def __repr__(self):
366 return "<WeakKeyDictionary at %s>" % id(self)
367
368 def __setitem__(self, key, value):
369 self.data[ref(key, self._remove)] = value
370
371 def copy(self):
372 new = WeakKeyDictionary()
373 for key, value in self.data.items():
374 o = key()
375 if o is not None:
376 new[o] = value
Fred Drake9d2c85d2001-03-01 03:06:03 +0000377 return new
Martin v. Löwis5e163332001-02-27 18:36:56 +0000378
Antoine Pitrou775fd662009-05-15 16:54:52 +0000379 __copy__ = copy
380
381 def __deepcopy__(self, memo):
382 from copy import deepcopy
383 new = self.__class__()
384 for key, value in self.data.items():
385 o = key()
386 if o is not None:
387 new[o] = deepcopy(value, memo)
388 return new
389
Fred Drake1d9e4b72001-04-16 17:34:48 +0000390 def get(self, key, default=None):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000391 return self.data.get(ref(key),default)
392
Fred Drake1d9e4b72001-04-16 17:34:48 +0000393 def has_key(self, key):
Fred Drake3bae7dd2001-11-06 16:36:53 +0000394 try:
395 wr = ref(key)
396 except TypeError:
397 return 0
Raymond Hettinger54f02222002-06-01 14:18:47 +0000398 return wr in self.data
Fred Drake1d9e4b72001-04-16 17:34:48 +0000399
Raymond Hettinger54f02222002-06-01 14:18:47 +0000400 def __contains__(self, key):
401 try:
402 wr = ref(key)
403 except TypeError:
404 return 0
405 return wr in self.data
Tim Petersc411dba2002-07-16 21:35:23 +0000406
Martin v. Löwis5e163332001-02-27 18:36:56 +0000407 def items(self):
408 L = []
409 for key, value in self.data.items():
410 o = key()
411 if o is not None:
412 L.append((o, value))
413 return L
414
Fred Drake101209d2001-05-02 05:43:09 +0000415 def iteritems(self):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000416 with _IterationGuard(self):
417 for wr, value in self.data.iteritems():
418 key = wr()
419 if key is not None:
420 yield key, value
Fred Drake101209d2001-05-02 05:43:09 +0000421
Fred Drake017e68c2006-05-02 06:53:59 +0000422 def iterkeyrefs(self):
423 """Return an iterator that yields the weak references to the keys.
424
425 The references are not guaranteed to be 'live' at the time
426 they are used, so the result of calling the references needs
427 to be checked before being used. This can be used to avoid
428 creating references that will cause the garbage collector to
429 keep the keys around longer than needed.
430
431 """
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000432 with _IterationGuard(self):
433 for wr in self.data.iterkeys():
434 yield wr
Fred Drake017e68c2006-05-02 06:53:59 +0000435
Fred Drake101209d2001-05-02 05:43:09 +0000436 def iterkeys(self):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000437 with _IterationGuard(self):
438 for wr in self.data.iterkeys():
439 obj = wr()
440 if obj is not None:
441 yield obj
Raymond Hettinger61146792004-08-19 21:32:06 +0000442
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000443 __iter__ = iterkeys
Fred Drake101209d2001-05-02 05:43:09 +0000444
445 def itervalues(self):
Kristján Valur Jónsson222b2842013-12-05 10:03:45 +0000446 with _IterationGuard(self):
447 for value in self.data.itervalues():
448 yield value
Fred Drake101209d2001-05-02 05:43:09 +0000449
Fred Drake017e68c2006-05-02 06:53:59 +0000450 def keyrefs(self):
451 """Return a list of weak references to the keys.
452
453 The references are not guaranteed to be 'live' at the time
454 they are used, so the result of calling the references needs
455 to be checked before being used. This can be used to avoid
456 creating references that will cause the garbage collector to
457 keep the keys around longer than needed.
458
459 """
460 return self.data.keys()
461
Fred Drake1d9e4b72001-04-16 17:34:48 +0000462 def keys(self):
463 L = []
Fred Drakebd7f8182001-04-19 16:26:06 +0000464 for wr in self.data.keys():
465 o = wr()
Fred Drake1d9e4b72001-04-16 17:34:48 +0000466 if o is not None:
467 L.append(o)
468 return L
469
Martin v. Löwis5e163332001-02-27 18:36:56 +0000470 def popitem(self):
471 while 1:
472 key, value = self.data.popitem()
473 o = key()
474 if o is not None:
475 return o, value
476
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000477 def pop(self, key, *args):
478 return self.data.pop(ref(key), *args)
479
Walter Dörwald80ce6dd2004-05-27 18:16:25 +0000480 def setdefault(self, key, default=None):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000481 return self.data.setdefault(ref(key, self._remove),default)
482
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000483 def update(self, dict=None, **kwargs):
Martin v. Löwis5e163332001-02-27 18:36:56 +0000484 d = self.data
Raymond Hettinger31017ae2004-03-04 08:25:44 +0000485 if dict is not None:
486 if not hasattr(dict, "items"):
487 dict = type({})(dict)
488 for key, value in dict.items():
489 d[ref(key, self._remove)] = value
490 if len(kwargs):
491 self.update(kwargs)