Index: Doc/library/weakref.rst =================================================================== --- Doc/library/weakref.rst (revision 63647) +++ Doc/library/weakref.rst (working copy) @@ -152,16 +152,11 @@ than needed. -.. method:: WeakKeyDictionary.iterkeyrefs() +.. method:: WeakKeyDictionary.keyrefs() Return an :term:`iterator` that yields the weak references to the keys. -.. method:: WeakKeyDictionary.keyrefs() - - Return a list of weak references to the keys. - - .. class:: WeakValueDictionary([dict]) Mapping class that references values weakly. Entries in the dictionary will be @@ -176,20 +171,15 @@ magic" (as a side effect of garbage collection). :class:`WeakValueDictionary` objects have the following additional methods. -These method have the same issues as the :meth:`iterkeyrefs` and :meth:`keyrefs` -methods of :class:`WeakKeyDictionary` objects. +These method have the same issues as the and :meth:`keyrefs` method of +:class:`WeakKeyDictionary` objects. -.. method:: WeakValueDictionary.itervaluerefs() +.. method:: WeakValueDictionary.valuerefs() Return an :term:`iterator` that yields the weak references to the values. -.. method:: WeakValueDictionary.valuerefs() - - Return a list of weak references to the values. - - .. class:: WeakSet([elements]) Set class that keeps weak references to its elements. An element will be @@ -290,7 +280,7 @@ def __init__(self, ob, callback=None, **annotations): super(ExtendedRef, self).__init__(ob, callback) self.__counter = 0 - for k, v in annotations.iteritems(): + for k, v in annotations.items(): setattr(self, k, v) def __call__(self): Index: Lib/weakref.py =================================================================== --- Lib/weakref.py (revision 63647) +++ Lib/weakref.py (working copy) @@ -106,13 +106,13 @@ L.append((key, o)) return L - def iteritems(self): + def items(self): for wr in self.data.values(): value = wr() if value is not None: yield wr.key, value - def iterkeys(self): + def keys(self): return iter(self.data.keys()) def __iter__(self): @@ -130,7 +130,7 @@ """ return self.data.values() - def itervalues(self): + def values(self): for wr in self.data.values(): obj = wr() if obj is not None: @@ -186,15 +186,7 @@ """ return self.data.values() - def values(self): - L = [] - for wr in self.data.values(): - o = wr() - if o is not None: - L.append(o) - return L - class KeyedRef(ref): """Specialized reference that includes a key corresponding to the value. @@ -270,20 +262,12 @@ return wr in self.data def items(self): - L = [] - for key, value in self.data.items(): - o = key() - if o is not None: - L.append((o, value)) - return L - - def iteritems(self): for wr, value in self.data.items(): key = wr() if key is not None: yield key, value - def iterkeyrefs(self): + def keyrefs(self): """Return an iterator that yields the weak references to the keys. The references are not guaranteed to be 'live' at the time @@ -295,7 +279,7 @@ """ return self.data.keys() - def iterkeys(self): + def keys(self): for wr in self.data.keys(): obj = wr() if obj is not None: @@ -304,7 +288,7 @@ def __iter__(self): return iter(self.keys()) - def itervalues(self): + def values(self): return iter(self.data.values()) def keyrefs(self): @@ -319,14 +303,6 @@ """ return self.data.keys() - def keys(self): - L = [] - for wr in self.data.keys(): - o = wr() - if o is not None: - L.append(o) - return L - def popitem(self): while 1: key, value = self.data.popitem() Index: Lib/test/test_weakref.py =================================================================== --- Lib/test/test_weakref.py (revision 63647) +++ Lib/test/test_weakref.py (working copy) @@ -752,8 +752,8 @@ self.assertEqual(weakref.getweakrefcount(o), 1) self.assert_(o is dict[o.arg], "wrong object returned by weak dict!") - items1 = dict.items() - items2 = dict.copy().items() + items1 = list(dict.items()) + items2 = list(dict.copy().items()) items1.sort() items2.sort() self.assertEqual(items1, items2, @@ -818,8 +818,8 @@ # Test iterkeyrefs() objects2 = list(objects) - self.assertEqual(len(list(dict.iterkeyrefs())), len(objects)) - for wr in dict.iterkeyrefs(): + self.assertEqual(len(list(dict.keyrefs())), len(objects)) + for wr in dict.keyrefs(): ob = wr() self.assert_(ob in dict) self.assert_(ob in dict) @@ -854,28 +854,28 @@ def check_iters(self, dict): # item iterator: - items = dict.items() + items = list(dict.items()) for item in dict.items(): items.remove(item) - self.assert_(len(items) == 0, "items() did not touch all items") + self.assertFalse(items, "items() did not touch all items") # key iterator, via __iter__(): keys = list(dict.keys()) for k in dict: keys.remove(k) - self.assert_(len(keys) == 0, "__iter__() did not touch all keys") + self.assertFalse(keys, "__iter__() did not touch all keys") # key iterator, via iterkeys(): keys = list(dict.keys()) for k in dict.keys(): keys.remove(k) - self.assert_(len(keys) == 0, "iterkeys() did not touch all keys") + self.assertFalse(keys, "iterkeys() did not touch all keys") # value iterator: values = list(dict.values()) for v in dict.values(): values.remove(v) - self.assert_(len(values) == 0, + self.assertFalse(values, "itervalues() did not touch all values") def test_make_weak_keyed_dict_from_dict(self): @@ -992,7 +992,7 @@ self.assertEqual(len(d), 2) del d[o1] self.assertEqual(len(d), 1) - self.assertEqual(d.keys(), [o2]) + self.assertEqual(list(d.keys()), [o2]) def test_weak_valued_delitem(self): d = weakref.WeakValueDictionary() @@ -1003,7 +1003,7 @@ self.assertEqual(len(d), 2) del d['something'] self.assertEqual(len(d), 1) - self.assert_(d.items() == [('something else', o2)]) + self.assert_(list(d.items()) == [('something else', o2)]) def test_weak_keyed_bad_delitem(self): d = weakref.WeakKeyDictionary() @@ -1044,7 +1044,7 @@ d[o] = o.value del o # now the only strong references to keys are in objs # Find the order in which iterkeys sees the keys. - objs = d.keys() + objs = list(d.keys()) # Reverse it, so that the iteration implementation of __delitem__ # has to keep looping to find the first object we delete. objs.reverse()