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,
|
|
|
|
ReferenceType)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2010-03-29 17:04:23 -03:00
|
|
|
from _weakrefset import WeakSet
|
|
|
|
|
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
|
|
|
|
2004-07-02 15:57:45 -03:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
def remove(wr, selfref=ref(self)):
|
|
|
|
self = selfref()
|
|
|
|
if self is not None:
|
|
|
|
del self.data[wr.key]
|
|
|
|
self._remove = remove
|
2005-06-04 06:20:03 -03:00
|
|
|
UserDict.UserDict.__init__(self, *args, **kw)
|
2004-07-02 15:57:45 -03:00
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def __getitem__(self, key):
|
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
|
|
|
|
|
2004-08-19 18:32:06 -03:00
|
|
|
def __contains__(self, key):
|
|
|
|
try:
|
|
|
|
o = self.data[key]()
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
return o is not None
|
|
|
|
|
|
|
|
def has_key(self, key):
|
|
|
|
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):
|
2004-07-02 15:57:45 -03:00
|
|
|
self.data[key] = KeyedRef(value, self._remove, key)
|
2001-02-01 01:27:45 -04:00
|
|
|
|
|
|
|
def copy(self):
|
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
|
|
|
|
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):
|
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):
|
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):
|
2004-08-19 18:32:06 -03:00
|
|
|
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):
|
|
|
|
return self.data.iterkeys()
|
2004-08-19 18:32:06 -03:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.data.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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.data.itervalues()
|
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def itervalues(self):
|
2004-08-19 18:32:06 -03:00
|
|
|
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):
|
|
|
|
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):
|
|
|
|
try:
|
|
|
|
o = self.data.pop(key)()
|
|
|
|
except KeyError:
|
|
|
|
if args:
|
|
|
|
return args[0]
|
|
|
|
raise
|
|
|
|
if o is None:
|
|
|
|
raise KeyError, key
|
|
|
|
else:
|
|
|
|
return o
|
|
|
|
|
2004-05-27 15:16:25 -03:00
|
|
|
def setdefault(self, key, default=None):
|
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:
|
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:
|
2001-04-19 13:26:06 -03:00
|
|
|
return wr()
|
2001-02-01 01:27:45 -04:00
|
|
|
|
2004-03-04 04:25:44 -04:00
|
|
|
def update(self, dict=None, **kwargs):
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.data.values()
|
|
|
|
|
2001-02-01 01:27:45 -04:00
|
|
|
def values(self):
|
|
|
|
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:
|
|
|
|
del self.data[k]
|
2001-02-27 14:36:56 -04:00
|
|
|
self._remove = remove
|
2002-06-10 17:00:52 -03:00
|
|
|
if dict is not None: self.update(dict)
|
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):
|
2004-08-19 18:32:06 -03:00
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.data.iterkeys()
|
|
|
|
|
2001-05-02 02:43:09 -03:00
|
|
|
def iterkeys(self):
|
2004-08-19 18:32:06 -03:00
|
|
|
for wr in self.data.iterkeys():
|
|
|
|
obj = wr()
|
|
|
|
if obj is not None:
|
|
|
|
yield obj
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.iterkeys()
|
2001-05-02 02:43:09 -03:00
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
return self.data.itervalues()
|
|
|
|
|
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)
|