Kaydet (Commit) 7ac60955 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Decouple weakref containers from UserDict

and teach them to use ABC instead.

More work left to do later.  Still need to
modernize the API of the dictlike objects to
more closely match regulars dicts.
üst 93fa6086
...@@ -9,7 +9,7 @@ http://python.sourceforge.net/peps/pep-0205.html ...@@ -9,7 +9,7 @@ http://python.sourceforge.net/peps/pep-0205.html
# they are called this instead of "ref" to avoid name collisions with # they are called this instead of "ref" to avoid name collisions with
# the module-global ref() function imported from _weakref. # the module-global ref() function imported from _weakref.
import UserDict import collections
from _weakref import ( from _weakref import (
getweakrefcount, getweakrefcount,
...@@ -30,7 +30,7 @@ __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs", ...@@ -30,7 +30,7 @@ __all__ = ["ref", "proxy", "getweakrefcount", "getweakrefs",
"WeakSet"] "WeakSet"]
class WeakValueDictionary(UserDict.UserDict): class WeakValueDictionary(collections.MutableMapping):
"""Mapping class that references values weakly. """Mapping class that references values weakly.
Entries in the dictionary will be discarded when no strong Entries in the dictionary will be discarded when no strong
...@@ -48,7 +48,8 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -48,7 +48,8 @@ class WeakValueDictionary(UserDict.UserDict):
if self is not None: if self is not None:
del self.data[wr.key] del self.data[wr.key]
self._remove = remove self._remove = remove
UserDict.UserDict.__init__(self, *args, **kw) self.data = d = {}
d.update(*args, **kw)
def __getitem__(self, key): def __getitem__(self, key):
o = self.data[key]() o = self.data[key]()
...@@ -57,6 +58,12 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -57,6 +58,12 @@ class WeakValueDictionary(UserDict.UserDict):
else: else:
return o return o
def __delitem__(self, key):
del self.data[key]
def __len__(self):
return sum(wr() is not None for wr in self.data.values())
def __contains__(self, key): def __contains__(self, key):
try: try:
o = self.data[key]() o = self.data[key]()
...@@ -187,6 +194,7 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -187,6 +194,7 @@ class WeakValueDictionary(UserDict.UserDict):
L.append(o) L.append(o)
return L return L
collections.MutableMapping.register(WeakValueDictionary)
class KeyedRef(ref): class KeyedRef(ref):
"""Specialized reference that includes a key corresponding to the value. """Specialized reference that includes a key corresponding to the value.
...@@ -209,7 +217,7 @@ class KeyedRef(ref): ...@@ -209,7 +217,7 @@ class KeyedRef(ref):
super().__init__(ob, callback) super().__init__(ob, callback)
class WeakKeyDictionary(UserDict.UserDict): class WeakKeyDictionary(collections.MutableMapping):
""" Mapping class that references keys weakly. """ Mapping class that references keys weakly.
Entries in the dictionary will be discarded when there is no Entries in the dictionary will be discarded when there is no
...@@ -235,6 +243,9 @@ class WeakKeyDictionary(UserDict.UserDict): ...@@ -235,6 +243,9 @@ class WeakKeyDictionary(UserDict.UserDict):
def __getitem__(self, key): def __getitem__(self, key):
return self.data[ref(key)] return self.data[ref(key)]
def __len__(self):
return len(self.data)
def __repr__(self): def __repr__(self):
return "<WeakKeyDictionary at %s>" % id(self) return "<WeakKeyDictionary at %s>" % id(self)
...@@ -339,3 +350,5 @@ class WeakKeyDictionary(UserDict.UserDict): ...@@ -339,3 +350,5 @@ class WeakKeyDictionary(UserDict.UserDict):
d[ref(key, self._remove)] = value d[ref(key, self._remove)] = value
if len(kwargs): if len(kwargs):
self.update(kwargs) self.update(kwargs)
collections.MutableMapping.register(WeakKeyDictionary)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment