Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ecaab83

Browse files
committed
Committing the patch in issue 2965, so that weakref dicts have a closer
interface to normal dictionaries. keys(), values() and items() still return iterators instead of views, but that can be fixed later (or not).
1 parent 6ecc5c1 commit ecaab83

3 files changed

Lines changed: 23 additions & 57 deletions

File tree

Doc/library/weakref.rst

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,9 @@ references that will cause the garbage collector to keep the keys around longer
152152
than needed.
153153

154154

155-
.. method:: WeakKeyDictionary.iterkeyrefs()
156-
157-
Return an :term:`iterator` that yields the weak references to the keys.
158-
159-
160155
.. method:: WeakKeyDictionary.keyrefs()
161156

162-
Return a list of weak references to the keys.
157+
Return an :term:`iterator` that yields the weak references to the keys.
163158

164159

165160
.. class:: WeakValueDictionary([dict])
@@ -176,18 +171,13 @@ than needed.
176171
magic" (as a side effect of garbage collection).
177172

178173
:class:`WeakValueDictionary` objects have the following additional methods.
179-
These method have the same issues as the :meth:`iterkeyrefs` and :meth:`keyrefs`
180-
methods of :class:`WeakKeyDictionary` objects.
181-
182-
183-
.. method:: WeakValueDictionary.itervaluerefs()
184-
185-
Return an :term:`iterator` that yields the weak references to the values.
174+
These method have the same issues as the and :meth:`keyrefs` method of
175+
:class:`WeakKeyDictionary` objects.
186176

187177

188178
.. method:: WeakValueDictionary.valuerefs()
189179

190-
Return a list of weak references to the values.
180+
Return an :term:`iterator` that yields the weak references to the values.
191181

192182

193183
.. class:: WeakSet([elements])
@@ -290,7 +280,7 @@ the referent is accessed::
290280
def __init__(self, ob, callback=None, **annotations):
291281
super(ExtendedRef, self).__init__(ob, callback)
292282
self.__counter = 0
293-
for k, v in annotations.iteritems():
283+
for k, v in annotations.items():
294284
setattr(self, k, v)
295285

296286
def __call__(self):

Lib/test/test_weakref.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ def test_weak_values(self):
790790
self.assertEqual(weakref.getweakrefcount(o), 1)
791791
self.assert_(o is dict[o.arg],
792792
"wrong object returned by weak dict!")
793-
items1 = dict.items()
794-
items2 = dict.copy().items()
793+
items1 = list(dict.items())
794+
items2 = list(dict.copy().items())
795795
items1.sort()
796796
items2.sort()
797797
self.assertEqual(items1, items2,
@@ -856,8 +856,8 @@ def test_weak_keyed_iters(self):
856856

857857
# Test iterkeyrefs()
858858
objects2 = list(objects)
859-
self.assertEqual(len(list(dict.iterkeyrefs())), len(objects))
860-
for wr in dict.iterkeyrefs():
859+
self.assertEqual(len(list(dict.keyrefs())), len(objects))
860+
for wr in dict.keyrefs():
861861
ob = wr()
862862
self.assert_(ob in dict)
863863
self.assert_(ob in dict)
@@ -892,28 +892,28 @@ def test_weak_valued_iters(self):
892892

893893
def check_iters(self, dict):
894894
# item iterator:
895-
items = dict.items()
895+
items = list(dict.items())
896896
for item in dict.items():
897897
items.remove(item)
898-
self.assert_(len(items) == 0, "items() did not touch all items")
898+
self.assertFalse(items, "items() did not touch all items")
899899

900900
# key iterator, via __iter__():
901901
keys = list(dict.keys())
902902
for k in dict:
903903
keys.remove(k)
904-
self.assert_(len(keys) == 0, "__iter__() did not touch all keys")
904+
self.assertFalse(keys, "__iter__() did not touch all keys")
905905

906906
# key iterator, via iterkeys():
907907
keys = list(dict.keys())
908908
for k in dict.keys():
909909
keys.remove(k)
910-
self.assert_(len(keys) == 0, "iterkeys() did not touch all keys")
910+
self.assertFalse(keys, "iterkeys() did not touch all keys")
911911

912912
# value iterator:
913913
values = list(dict.values())
914914
for v in dict.values():
915915
values.remove(v)
916-
self.assert_(len(values) == 0,
916+
self.assertFalse(values,
917917
"itervalues() did not touch all values")
918918

919919
def test_make_weak_keyed_dict_from_dict(self):
@@ -1030,7 +1030,7 @@ def test_weak_keyed_delitem(self):
10301030
self.assertEqual(len(d), 2)
10311031
del d[o1]
10321032
self.assertEqual(len(d), 1)
1033-
self.assertEqual(d.keys(), [o2])
1033+
self.assertEqual(list(d.keys()), [o2])
10341034

10351035
def test_weak_valued_delitem(self):
10361036
d = weakref.WeakValueDictionary()
@@ -1041,7 +1041,7 @@ def test_weak_valued_delitem(self):
10411041
self.assertEqual(len(d), 2)
10421042
del d['something']
10431043
self.assertEqual(len(d), 1)
1044-
self.assert_(d.items() == [('something else', o2)])
1044+
self.assert_(list(d.items()) == [('something else', o2)])
10451045

10461046
def test_weak_keyed_bad_delitem(self):
10471047
d = weakref.WeakKeyDictionary()
@@ -1082,7 +1082,7 @@ def __eq__(self, other):
10821082
d[o] = o.value
10831083
del o # now the only strong references to keys are in objs
10841084
# Find the order in which iterkeys sees the keys.
1085-
objs = d.keys()
1085+
objs = list(d.keys())
10861086
# Reverse it, so that the iteration implementation of __delitem__
10871087
# has to keep looping to find the first object we delete.
10881088
objs.reverse()

Lib/weakref.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ def items(self):
106106
L.append((key, o))
107107
return L
108108

109-
def iteritems(self):
109+
def items(self):
110110
for wr in self.data.values():
111111
value = wr()
112112
if value is not None:
113113
yield wr.key, value
114114

115-
def iterkeys(self):
115+
def keys(self):
116116
return iter(self.data.keys())
117117

118118
def __iter__(self):
@@ -130,7 +130,7 @@ def itervaluerefs(self):
130130
"""
131131
return self.data.values()
132132

133-
def itervalues(self):
133+
def values(self):
134134
for wr in self.data.values():
135135
obj = wr()
136136
if obj is not None:
@@ -186,14 +186,6 @@ def valuerefs(self):
186186
"""
187187
return self.data.values()
188188

189-
def values(self):
190-
L = []
191-
for wr in self.data.values():
192-
o = wr()
193-
if o is not None:
194-
L.append(o)
195-
return L
196-
197189

198190
class KeyedRef(ref):
199191
"""Specialized reference that includes a key corresponding to the value.
@@ -270,20 +262,12 @@ def __contains__(self, key):
270262
return wr in self.data
271263

272264
def items(self):
273-
L = []
274-
for key, value in self.data.items():
275-
o = key()
276-
if o is not None:
277-
L.append((o, value))
278-
return L
279-
280-
def iteritems(self):
281265
for wr, value in self.data.items():
282266
key = wr()
283267
if key is not None:
284268
yield key, value
285269

286-
def iterkeyrefs(self):
270+
def keyrefs(self):
287271
"""Return an iterator that yields the weak references to the keys.
288272
289273
The references are not guaranteed to be 'live' at the time
@@ -295,7 +279,7 @@ def iterkeyrefs(self):
295279
"""
296280
return self.data.keys()
297281

298-
def iterkeys(self):
282+
def keys(self):
299283
for wr in self.data.keys():
300284
obj = wr()
301285
if obj is not None:
@@ -304,7 +288,7 @@ def iterkeys(self):
304288
def __iter__(self):
305289
return iter(self.keys())
306290

307-
def itervalues(self):
291+
def values(self):
308292
return iter(self.data.values())
309293

310294
def keyrefs(self):
@@ -319,14 +303,6 @@ def keyrefs(self):
319303
"""
320304
return self.data.keys()
321305

322-
def keys(self):
323-
L = []
324-
for wr in self.data.keys():
325-
o = wr()
326-
if o is not None:
327-
L.append(o)
328-
return L
329-
330306
def popitem(self):
331307
while 1:
332308
key, value = self.data.popitem()

0 commit comments

Comments
 (0)