2001-02-01 01:27:45 -04:00
|
|
|
"""Weak reference support for Python.
|
|
|
|
|
|
|
|
This module is an implementation of PEP 205:
|
|
|
|
|
2008-04-26 21:52:24 -03:00
|
|
|
http://www.python.org/dev/peps/pep-0205/
|
2001-02-01 01:27:45 -04:00
|
|
|
"""
|
|
|
|
|
2001-04-19 13:26:06 -03:00
|
|
|
# Naming convention: Variables named "wr" are weak reference objects;
|
|
|
|
# they are called this instead of "ref" to avoid name collisions with
|
|
|
|
# the module-global ref() function imported from _weakref.
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
import UserDict
|
|
|
|
|
2004-08-31 08:38:12 -03:00
|
|
|
from _weakref import (
|
|
|
|
getweakrefcount,
|
|
|
|
getweakrefs,
|
|
|
|
ref,
|
|
|
|
proxy,
|
|
|
|
CallableProxyType,
|
|
|
|
ProxyType,
|
2016-12-27 10:08:27 -04:00
|
|
|
ReferenceType,
|
|
|
|
_remove_dead_weakref)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
from _weakrefset import WeakSet, _IterationGuard
|
2010-03-29 17:04:23 -03:00
|
|
|
|
2001-10-05 18:54:09 -03:00
|
|
|
from exceptions import ReferenceError
|
|
|
|
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
ProxyTypes = (ProxyType, CallableProxyType)
|
|
|
|
|
2001-04-10 16:11:23 -03:00
|
|
|
__all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
|
2008-08-17 21:46:22 -03:00
|
|
|
"WeakKeyDictionary", "ReferenceError", "ReferenceType", "ProxyType",
|
2010-03-29 17:04:23 -03:00
|
|
|
"CallableProxyType", "ProxyTypes", "WeakValueDictionary", 'WeakSet']
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-04-19 13:26:06 -03:00
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
class WeakValueDictionary(UserDict.UserDict):
|
2001-04-19 13:26:06 -03:00
|
|
|
"""Mapping class that references values weakly.
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-04-19 13:26:06 -03:00
|
|
|
Entries in the dictionary will be discarded when no strong
|
|
|
|
reference to the value exists anymore
|
|
|
|
"""
|
2001-02-01 01:27:45 -04:00
|
|
|
# We inherit the constructor without worrying about the input
|
|
|
|
# dictionary; since it uses our .update() method, we get the right
|
2001-02-28 23:06:03 -04:00
|
|
|
# checks (if the other dictionary is a WeakValueDictionary,
|
|
|
|
# objects are unwrapped on the way out, and we always wrap on the
|
|
|
|
# way in).
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2015-09-29 17:51:27 -03:00
|
|
|
def __init__(*args, **kw):
|
|
|
|
if not args:
|
|
|
|
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
|
|
|
|
"object needs an argument")
|
|
|
|
self = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
2004-07-02 15:57:45 -03:00
|
|
|
def remove(wr, selfref=ref(self)):
|
|
|
|
self = selfref()
|
|
|
|
if self is not None:
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._iterating:
|
|
|
|
self._pending_removals.append(wr.key)
|
|
|
|
else:
|
2016-12-27 10:08:27 -04:00
|
|
|
# Atomic removal is necessary since this function
|
|
|
|
# can be called asynchronously by the GC
|
|
|
|
_remove_dead_weakref(self.data, wr.key)
|
2004-07-02 15:57:45 -03:00
|
|
|
self._remove = remove
|
2013-12-05 06:03:45 -04:00
|
|
|
# A list of keys to be removed
|
|
|
|
self._pending_removals = []
|
|
|
|
self._iterating = set()
|
2005-06-04 06:20:03 -03:00
|
|
|
UserDict.UserDict.__init__(self, *args, **kw)
|
2004-07-02 15:57:45 -03:00
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
def _commit_removals(self):
|
|
|
|
l = self._pending_removals
|
|
|
|
d = self.data
|
|
|
|
# We shouldn't encounter any KeyError, because this method should
|
|
|
|
# always be called *before* mutating the dict.
|
|
|
|
while l:
|
2016-12-27 10:08:27 -04:00
|
|
|
key = l.pop()
|
|
|
|
_remove_dead_weakref(d, key)
|
2013-12-05 06:03:45 -04:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def __getitem__(self, key):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-08-03 01:11:27 -03:00
|
|
|
o = self.data[key]()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is None:
|
|
|
|
raise KeyError, key
|
|
|
|
else:
|
|
|
|
return o
|
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
def __delitem__(self, key):
|
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
|
|
|
del self.data[key]
|
|
|
|
|
2004-08-19 18:32:06 -03:00
|
|
|
def __contains__(self, key):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2004-08-19 18:32:06 -03:00
|
|
|
try:
|
|
|
|
o = self.data[key]()
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
return o is not None
|
|
|
|
|
|
|
|
def has_key(self, key):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2004-08-19 18:32:06 -03:00
|
|
|
try:
|
|
|
|
o = self.data[key]()
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
return o is not None
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def __repr__(self):
|
2001-02-28 23:06:03 -04:00
|
|
|
return "<WeakValueDictionary at %s>" % id(self)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2004-07-02 15:57:45 -03:00
|
|
|
self.data[key] = KeyedRef(value, self._remove, key)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
def clear(self):
|
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
|
|
|
self.data.clear()
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def copy(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-28 23:06:03 -04:00
|
|
|
new = WeakValueDictionary()
|
2001-04-19 13:26:06 -03:00
|
|
|
for key, wr in self.data.items():
|
|
|
|
o = wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is not None:
|
|
|
|
new[key] = o
|
2001-02-28 23:06:03 -04:00
|
|
|
return new
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2009-05-15 13:54:52 -03:00
|
|
|
__copy__ = copy
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
from copy import deepcopy
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2009-05-15 13:54:52 -03:00
|
|
|
new = self.__class__()
|
|
|
|
for key, wr in self.data.items():
|
|
|
|
o = wr()
|
|
|
|
if o is not None:
|
|
|
|
new[deepcopy(key, memo)] = o
|
|
|
|
return new
|
|
|
|
|
2001-04-16 14:34:48 -03:00
|
|
|
def get(self, key, default=None):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-01 01:27:45 -04:00
|
|
|
try:
|
2001-04-19 13:26:06 -03:00
|
|
|
wr = self.data[key]
|
2001-02-01 01:27:45 -04:00
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
else:
|
2001-04-19 13:26:06 -03:00
|
|
|
o = wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is None:
|
|
|
|
# This should only happen
|
|
|
|
return default
|
|
|
|
else:
|
|
|
|
return o
|
|
|
|
|
|
|
|
def items(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-02 11:13:24 -04:00
|
|
|
L = []
|
2001-04-19 13:26:06 -03:00
|
|
|
for key, wr in self.data.items():
|
|
|
|
o = wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is not None:
|
2001-02-02 11:13:24 -04:00
|
|
|
L.append((key, o))
|
2001-02-01 01:27:45 -04:00
|
|
|
return L
|
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def iteritems(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr in self.data.itervalues():
|
|
|
|
value = wr()
|
|
|
|
if value is not None:
|
|
|
|
yield wr.key, value
|
2001-05-02 02:43:09 -03:00
|
|
|
|
|
|
|
def iterkeys(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for k in self.data.iterkeys():
|
|
|
|
yield k
|
2004-08-19 18:32:06 -03:00
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
__iter__ = iterkeys
|
2001-05-02 02:43:09 -03:00
|
|
|
|
2006-05-02 03:53:59 -03:00
|
|
|
def itervaluerefs(self):
|
|
|
|
"""Return an iterator that yields the weak references to the values.
|
|
|
|
|
|
|
|
The references are not guaranteed to be 'live' at the time
|
|
|
|
they are used, so the result of calling the references needs
|
|
|
|
to be checked before being used. This can be used to avoid
|
|
|
|
creating references that will cause the garbage collector to
|
|
|
|
keep the values around longer than needed.
|
|
|
|
|
|
|
|
"""
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr in self.data.itervalues():
|
|
|
|
yield wr
|
2006-05-02 03:53:59 -03:00
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def itervalues(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr in self.data.itervalues():
|
|
|
|
obj = wr()
|
|
|
|
if obj is not None:
|
|
|
|
yield obj
|
2001-05-02 02:43:09 -03:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def popitem(self):
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-01 01:27:45 -04:00
|
|
|
while 1:
|
2001-04-19 13:26:06 -03:00
|
|
|
key, wr = self.data.popitem()
|
|
|
|
o = wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is not None:
|
|
|
|
return key, o
|
|
|
|
|
2003-03-09 03:05:43 -04:00
|
|
|
def pop(self, key, *args):
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2003-03-09 03:05:43 -04:00
|
|
|
try:
|
|
|
|
o = self.data.pop(key)()
|
|
|
|
except KeyError:
|
2016-12-19 06:12:58 -04:00
|
|
|
o = None
|
|
|
|
if o is None:
|
2003-03-09 03:05:43 -04:00
|
|
|
if args:
|
|
|
|
return args[0]
|
2016-12-19 06:12:58 -04:00
|
|
|
else:
|
|
|
|
raise KeyError, key
|
2003-03-09 03:05:43 -04:00
|
|
|
else:
|
|
|
|
return o
|
|
|
|
|
2004-05-27 15:16:25 -03:00
|
|
|
def setdefault(self, key, default=None):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-01 01:27:45 -04:00
|
|
|
try:
|
2016-12-19 06:12:58 -04:00
|
|
|
o = self.data[key]()
|
2001-02-01 01:27:45 -04:00
|
|
|
except KeyError:
|
2016-12-19 06:12:58 -04:00
|
|
|
o = None
|
|
|
|
if o is None:
|
2004-07-02 15:57:45 -03:00
|
|
|
self.data[key] = KeyedRef(default, self._remove, key)
|
2001-02-01 01:27:45 -04:00
|
|
|
return default
|
|
|
|
else:
|
2016-12-19 06:12:58 -04:00
|
|
|
return o
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2015-09-29 17:51:27 -03:00
|
|
|
def update(*args, **kwargs):
|
|
|
|
if not args:
|
|
|
|
raise TypeError("descriptor 'update' of 'WeakValueDictionary' "
|
|
|
|
"object needs an argument")
|
|
|
|
self = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise TypeError('expected at most 1 arguments, got %d' % len(args))
|
|
|
|
dict = args[0] if args else None
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-01 01:27:45 -04:00
|
|
|
d = self.data
|
2004-03-04 04:25:44 -04:00
|
|
|
if dict is not None:
|
|
|
|
if not hasattr(dict, "items"):
|
|
|
|
dict = type({})(dict)
|
|
|
|
for key, o in dict.items():
|
2004-07-02 15:57:45 -03:00
|
|
|
d[key] = KeyedRef(o, self._remove, key)
|
2004-03-04 04:25:44 -04:00
|
|
|
if len(kwargs):
|
|
|
|
self.update(kwargs)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2006-05-02 03:53:59 -03:00
|
|
|
def valuerefs(self):
|
|
|
|
"""Return a list of weak references to the values.
|
|
|
|
|
|
|
|
The references are not guaranteed to be 'live' at the time
|
|
|
|
they are used, so the result of calling the references needs
|
|
|
|
to be checked before being used. This can be used to avoid
|
|
|
|
creating references that will cause the garbage collector to
|
|
|
|
keep the values around longer than needed.
|
|
|
|
|
|
|
|
"""
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2006-05-02 03:53:59 -03:00
|
|
|
return self.data.values()
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def values(self):
|
2016-12-27 10:08:27 -04:00
|
|
|
if self._pending_removals:
|
|
|
|
self._commit_removals()
|
2001-02-01 01:27:45 -04:00
|
|
|
L = []
|
2001-04-19 13:26:06 -03:00
|
|
|
for wr in self.data.values():
|
|
|
|
o = wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
if o is not None:
|
|
|
|
L.append(o)
|
|
|
|
return L
|
|
|
|
|
2004-07-02 15:57:45 -03:00
|
|
|
|
|
|
|
class KeyedRef(ref):
|
|
|
|
"""Specialized reference that includes a key corresponding to the value.
|
|
|
|
|
|
|
|
This is used in the WeakValueDictionary to avoid having to create
|
|
|
|
a function object for each key stored in the mapping. A shared
|
|
|
|
callback object can use the 'key' attribute of a KeyedRef instead
|
|
|
|
of getting a reference to the key from an enclosing scope.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = "key",
|
|
|
|
|
|
|
|
def __new__(type, ob, callback, key):
|
|
|
|
self = ref.__new__(type, ob, callback)
|
|
|
|
self.key = key
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __init__(self, ob, callback, key):
|
|
|
|
super(KeyedRef, self).__init__(ob, callback)
|
2001-09-28 16:01:26 -03:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
class WeakKeyDictionary(UserDict.UserDict):
|
2001-04-19 13:26:06 -03:00
|
|
|
""" Mapping class that references keys weakly.
|
|
|
|
|
|
|
|
Entries in the dictionary will be discarded when there is no
|
|
|
|
longer a strong reference to the key. This can be used to
|
|
|
|
associate additional data with an object owned by other parts of
|
|
|
|
an application without adding attributes to those objects. This
|
|
|
|
can be especially useful with objects that override attribute
|
|
|
|
accesses.
|
|
|
|
"""
|
2001-02-27 14:36:56 -04:00
|
|
|
|
|
|
|
def __init__(self, dict=None):
|
|
|
|
self.data = {}
|
2001-09-28 16:01:26 -03:00
|
|
|
def remove(k, selfref=ref(self)):
|
|
|
|
self = selfref()
|
|
|
|
if self is not None:
|
2013-12-05 06:03:45 -04:00
|
|
|
if self._iterating:
|
|
|
|
self._pending_removals.append(k)
|
|
|
|
else:
|
|
|
|
del self.data[k]
|
2001-02-27 14:36:56 -04:00
|
|
|
self._remove = remove
|
2013-12-05 06:03:45 -04:00
|
|
|
# A list of dead weakrefs (keys to be removed)
|
|
|
|
self._pending_removals = []
|
|
|
|
self._iterating = set()
|
|
|
|
if dict is not None:
|
|
|
|
self.update(dict)
|
|
|
|
|
|
|
|
def _commit_removals(self):
|
|
|
|
# NOTE: We don't need to call this method before mutating the dict,
|
|
|
|
# because a dead weakref never compares equal to a live weakref,
|
|
|
|
# even if they happened to refer to equal objects.
|
|
|
|
# However, it means keys may already have been removed.
|
|
|
|
l = self._pending_removals
|
|
|
|
d = self.data
|
|
|
|
while l:
|
|
|
|
try:
|
|
|
|
del d[l.pop()]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2001-02-27 14:36:56 -04:00
|
|
|
|
2001-09-06 11:51:01 -03:00
|
|
|
def __delitem__(self, key):
|
2003-05-24 22:45:11 -03:00
|
|
|
del self.data[ref(key)]
|
2001-09-06 11:51:01 -03:00
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.data[ref(key)]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<WeakKeyDictionary at %s>" % id(self)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.data[ref(key, self._remove)] = value
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
new = WeakKeyDictionary()
|
|
|
|
for key, value in self.data.items():
|
|
|
|
o = key()
|
|
|
|
if o is not None:
|
|
|
|
new[o] = value
|
2001-02-28 23:06:03 -04:00
|
|
|
return new
|
2001-02-27 14:36:56 -04:00
|
|
|
|
2009-05-15 13:54:52 -03:00
|
|
|
__copy__ = copy
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
from copy import deepcopy
|
|
|
|
new = self.__class__()
|
|
|
|
for key, value in self.data.items():
|
|
|
|
o = key()
|
|
|
|
if o is not None:
|
|
|
|
new[o] = deepcopy(value, memo)
|
|
|
|
return new
|
|
|
|
|
2001-04-16 14:34:48 -03:00
|
|
|
def get(self, key, default=None):
|
2001-02-27 14:36:56 -04:00
|
|
|
return self.data.get(ref(key),default)
|
|
|
|
|
2001-04-16 14:34:48 -03:00
|
|
|
def has_key(self, key):
|
2001-11-06 12:36:53 -04:00
|
|
|
try:
|
|
|
|
wr = ref(key)
|
|
|
|
except TypeError:
|
|
|
|
return 0
|
2002-06-01 11:18:47 -03:00
|
|
|
return wr in self.data
|
2001-04-16 14:34:48 -03:00
|
|
|
|
2002-06-01 11:18:47 -03:00
|
|
|
def __contains__(self, key):
|
|
|
|
try:
|
|
|
|
wr = ref(key)
|
|
|
|
except TypeError:
|
|
|
|
return 0
|
|
|
|
return wr in self.data
|
2002-07-16 18:35:23 -03:00
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
def items(self):
|
|
|
|
L = []
|
|
|
|
for key, value in self.data.items():
|
|
|
|
o = key()
|
|
|
|
if o is not None:
|
|
|
|
L.append((o, value))
|
|
|
|
return L
|
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def iteritems(self):
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr, value in self.data.iteritems():
|
|
|
|
key = wr()
|
|
|
|
if key is not None:
|
|
|
|
yield key, value
|
2001-05-02 02:43:09 -03:00
|
|
|
|
2006-05-02 03:53:59 -03:00
|
|
|
def iterkeyrefs(self):
|
|
|
|
"""Return an iterator that yields the weak references to the keys.
|
|
|
|
|
|
|
|
The references are not guaranteed to be 'live' at the time
|
|
|
|
they are used, so the result of calling the references needs
|
|
|
|
to be checked before being used. This can be used to avoid
|
|
|
|
creating references that will cause the garbage collector to
|
|
|
|
keep the keys around longer than needed.
|
|
|
|
|
|
|
|
"""
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr in self.data.iterkeys():
|
|
|
|
yield wr
|
2006-05-02 03:53:59 -03:00
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def iterkeys(self):
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for wr in self.data.iterkeys():
|
|
|
|
obj = wr()
|
|
|
|
if obj is not None:
|
|
|
|
yield obj
|
2004-08-19 18:32:06 -03:00
|
|
|
|
2013-12-05 06:03:45 -04:00
|
|
|
__iter__ = iterkeys
|
2001-05-02 02:43:09 -03:00
|
|
|
|
|
|
|
def itervalues(self):
|
2013-12-05 06:03:45 -04:00
|
|
|
with _IterationGuard(self):
|
|
|
|
for value in self.data.itervalues():
|
|
|
|
yield value
|
2001-05-02 02:43:09 -03:00
|
|
|
|
2006-05-02 03:53:59 -03:00
|
|
|
def keyrefs(self):
|
|
|
|
"""Return a list of weak references to the keys.
|
|
|
|
|
|
|
|
The references are not guaranteed to be 'live' at the time
|
|
|
|
they are used, so the result of calling the references needs
|
|
|
|
to be checked before being used. This can be used to avoid
|
|
|
|
creating references that will cause the garbage collector to
|
|
|
|
keep the keys around longer than needed.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.data.keys()
|
|
|
|
|
2001-04-16 14:34:48 -03:00
|
|
|
def keys(self):
|
|
|
|
L = []
|
2001-04-19 13:26:06 -03:00
|
|
|
for wr in self.data.keys():
|
|
|
|
o = wr()
|
2001-04-16 14:34:48 -03:00
|
|
|
if o is not None:
|
|
|
|
L.append(o)
|
|
|
|
return L
|
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
def popitem(self):
|
|
|
|
while 1:
|
|
|
|
key, value = self.data.popitem()
|
|
|
|
o = key()
|
|
|
|
if o is not None:
|
|
|
|
return o, value
|
|
|
|
|
2003-03-09 03:05:43 -04:00
|
|
|
def pop(self, key, *args):
|
|
|
|
return self.data.pop(ref(key), *args)
|
|
|
|
|
2004-05-27 15:16:25 -03:00
|
|
|
def setdefault(self, key, default=None):
|
2001-02-27 14:36:56 -04:00
|
|
|
return self.data.setdefault(ref(key, self._remove),default)
|
|
|
|
|
2004-03-04 04:25:44 -04:00
|
|
|
def update(self, dict=None, **kwargs):
|
2001-02-27 14:36:56 -04:00
|
|
|
d = self.data
|
2004-03-04 04:25:44 -04:00
|
|
|
if dict is not None:
|
|
|
|
if not hasattr(dict, "items"):
|
|
|
|
dict = type({})(dict)
|
|
|
|
for key, value in dict.items():
|
|
|
|
d[ref(key, self._remove)] = value
|
|
|
|
if len(kwargs):
|
|
|
|
self.update(kwargs)
|