Kaydet (Commit) 9d2c85de authored tarafından Fred Drake's avatar Fred Drake

Change WeakDictionary to WeakValueDictionary in a couple more places.

WeakValueDictionary.copy(),
WeakKeyDictionary.copy():  Actually return the copy!
üst 5f850ab4
...@@ -31,8 +31,9 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -31,8 +31,9 @@ class WeakValueDictionary(UserDict.UserDict):
# We inherit the constructor without worrying about the input # We inherit the constructor without worrying about the input
# dictionary; since it uses our .update() method, we get the right # dictionary; since it uses our .update() method, we get the right
# checks (if the other dictionary is a WeakDictionary, objects are # checks (if the other dictionary is a WeakValueDictionary,
# unwrapped on the way out, and we always wrap on the way in). # objects are unwrapped on the way out, and we always wrap on the
# way in).
def __getitem__(self, key): def __getitem__(self, key):
o = self.data.get(key)() o = self.data.get(key)()
...@@ -42,7 +43,7 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -42,7 +43,7 @@ class WeakValueDictionary(UserDict.UserDict):
return o return o
def __repr__(self): def __repr__(self):
return "<WeakDictionary at %s>" % id(self) return "<WeakValueDictionary at %s>" % id(self)
def __setitem__(self, key, value): def __setitem__(self, key, value):
def remove(o, data=self.data, key=key): def remove(o, data=self.data, key=key):
...@@ -50,11 +51,12 @@ class WeakValueDictionary(UserDict.UserDict): ...@@ -50,11 +51,12 @@ class WeakValueDictionary(UserDict.UserDict):
self.data[key] = ref(value, remove) self.data[key] = ref(value, remove)
def copy(self): def copy(self):
new = WeakDictionary() new = WeakValueDictionary()
for key, ref in self.data.items(): for key, ref in self.data.items():
o = ref() o = ref()
if o is not None: if o is not None:
new[key] = o new[key] = o
return new
def get(self, key, default): def get(self, key, default):
try: try:
...@@ -139,6 +141,7 @@ class WeakKeyDictionary(UserDict.UserDict): ...@@ -139,6 +141,7 @@ class WeakKeyDictionary(UserDict.UserDict):
o = key() o = key()
if o is not None: if o is not None:
new[o] = value new[o] = value
return new
def get(self, key, default): def get(self, key, default):
return self.data.get(ref(key),default) return self.data.get(ref(key),default)
......
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