2001-02-01 01:27:45 -04:00
|
|
|
"""Weak reference support for Python.
|
|
|
|
|
|
|
|
This module is an implementation of PEP 205:
|
|
|
|
|
|
|
|
http://python.sourceforge.net/peps/pep-0205.html
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
|
|
from _weakref import \
|
|
|
|
getweakrefcount, \
|
|
|
|
getweakrefs, \
|
|
|
|
ref, \
|
|
|
|
proxy, \
|
|
|
|
CallableProxyType, \
|
|
|
|
ProxyType, \
|
|
|
|
ReferenceType
|
|
|
|
|
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",
|
2001-03-01 00:27:19 -04:00
|
|
|
"WeakKeyDictionary", "ReferenceType", "ProxyType",
|
|
|
|
"CallableProxyType", "ProxyTypes", "WeakValueDictionary"]
|
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):
|
|
|
|
UserDict.UserDict.__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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
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):
|
|
|
|
return WeakValuedItemIterator(self)
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
return self.data.iterkeys()
|
|
|
|
__iter__ = iterkeys
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
return WeakValuedValueIterator(self)
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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):
|
|
|
|
return WeakKeyedItemIterator(self)
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
return WeakKeyedKeyIterator(self)
|
|
|
|
__iter__ = iterkeys
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
return self.data.itervalues()
|
|
|
|
|
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)
|
2001-04-19 13:26:06 -03:00
|
|
|
|
2001-02-27 14:36:56 -04:00
|
|
|
|
2004-08-13 04:12:45 -03:00
|
|
|
def WeakKeyedKeyIterator(weakdict):
|
|
|
|
for wr in weakdict.data.iterkeys():
|
|
|
|
obj = wr()
|
|
|
|
if obj is not None:
|
|
|
|
yield obj
|
|
|
|
|
|
|
|
def WeakKeyedItemIterator(weakdict):
|
|
|
|
for wr, value in weakdict.data.iteritems():
|
|
|
|
key = wr()
|
|
|
|
if key is not None:
|
|
|
|
yield key, value
|
|
|
|
|
|
|
|
def WeakValuedValueIterator(weakdict):
|
|
|
|
for wr in weakdict.data.itervalues():
|
|
|
|
obj = wr()
|
|
|
|
if obj is not None:
|
|
|
|
yield obj
|
|
|
|
|
|
|
|
def WeakValuedItemIterator(weakdict):
|
|
|
|
for wr in weakdict.data.itervalues():
|
|
|
|
value = wr()
|
|
|
|
if value is not None:
|
|
|
|
yield wr.key, value
|