Skip to content

Commit e0e5065

Browse files
bpo-34610: Fixed iterator of multiprocessing.managers.DictProxy. (GH-9113)
1 parent ddd1949 commit e0e5065

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Lib/multiprocessing/managers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,10 +1134,13 @@ def __imul__(self, value):
11341134

11351135

11361136
DictProxy = MakeProxyType('DictProxy', (
1137-
'__contains__', '__delitem__', '__getitem__', '__len__',
1137+
'__contains__', '__delitem__', '__getitem__', '__iter__', '__len__',
11381138
'__setitem__', 'clear', 'copy', 'get', 'has_key', 'items',
11391139
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'
11401140
))
1141+
DictProxy._method_to_typeid_ = {
1142+
'__iter__': 'Iterator',
1143+
}
11411144

11421145

11431146
ArrayProxy = MakeProxyType('ArrayProxy', (

Lib/test/_test_multiprocessing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,6 +2080,16 @@ def test_list(self):
20802080
a.append('hello')
20812081
self.assertEqual(f[0][:], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello'])
20822082

2083+
def test_list_iter(self):
2084+
a = self.list(list(range(10)))
2085+
it = iter(a)
2086+
self.assertEqual(list(it), list(range(10)))
2087+
self.assertEqual(list(it), []) # exhausted
2088+
# list modified during iteration
2089+
it = iter(a)
2090+
a[0] = 100
2091+
self.assertEqual(next(it), 100)
2092+
20832093
def test_list_proxy_in_list(self):
20842094
a = self.list([self.list(range(3)) for _i in range(3)])
20852095
self.assertEqual([inner[:] for inner in a], [[0, 1, 2]] * 3)
@@ -2110,6 +2120,19 @@ def test_dict(self):
21102120
self.assertEqual(sorted(d.values()), [chr(i) for i in indices])
21112121
self.assertEqual(sorted(d.items()), [(i, chr(i)) for i in indices])
21122122

2123+
def test_dict_iter(self):
2124+
d = self.dict()
2125+
indices = list(range(65, 70))
2126+
for i in indices:
2127+
d[i] = chr(i)
2128+
it = iter(d)
2129+
self.assertEqual(list(it), indices)
2130+
self.assertEqual(list(it), []) # exhausted
2131+
# dictionary changed size during iteration
2132+
it = iter(d)
2133+
d.clear()
2134+
self.assertRaises(RuntimeError, next, it)
2135+
21132136
def test_dict_proxy_nested(self):
21142137
pets = self.dict(ferrets=2, hamsters=4)
21152138
supplies = self.dict(water=10, feed=3)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed iterator of :class:`multiprocessing.managers.DictProxy`.

0 commit comments

Comments
 (0)