8000 [2.7] bpo-34610: Fixed iterator of multiprocessing.managers.DictProxy… · python/cpython@69d0bc1 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 69d0bc1

Browse files
[2.7] bpo-34610: Fixed iterator of multiprocessing.managers.DictProxy. (GH-9113). (GH-9500)
(cherry picked from commit e0e5065) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 6ec2981 commit 69d0bc1

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
@@ -1059,10 +1059,13 @@ def __imul__(self, value):
10591059

10601060

10611061
DictProxy = MakeProxyType('DictProxy', (
1062-
'__contains__', '__delitem__', '__getitem__', '__len__',
1062+
'__contains__', '__delitem__', '__getitem__', '__iter__', '__len__',
10631063
'__setitem__', 'clear', 'copy', 'get', 'has_key', 'items',
10641064
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'
10651065
))
1066+
DictProxy._method_to_typeid_ = {
1067+
'__iter__': 'Iterator',
1068+
}
10661069

10671070

10681071
ArrayProxy = MakeProxyType('ArrayProxy', (

Lib/test/test_multiprocessing.py

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

1138+
def test_list_iter(self):
1139+
a = self.list(range(10))
1140+
it = iter(a)
1141+
self.assertEqual(list(it), range(10))
1142+
self.assertEqual(list(it), []) # exhausted
1143+
# list modified during iteration
1144+
it = iter(a)
1145+
a[0] = 100
1146+
self.assertEqual(next(it), 100)
1147+
11381148
def test_dict(self):
11391149
d = self.dict()
11401150
indices = range(65, 70)
@@ -1145,6 +1155,19 @@ def test_dict(self):
11451155
self.assertEqual(sorted(d.values()), [chr(i) for i in indices])
11461156
self.assertEqual(sorted(d.items()), [(i, chr(i)) for i in indices])
11471157

1158+
def test_dict_iter(self):
1159+
d = self.dict()
1160+
indices = range(65, 70)
1161+
for i in indices:
1162+
d[i] = chr(i)
1163+
it = iter(d)
1164+
self.assertEqual(list(it), indices)
1165+
self.assertEqual(list(it), []) # exhausted
1166+
# dictionary changed size during iteration
1167+
it = iter(d)
1168+
d.clear()
1169+
self.assertRaises(RuntimeError, next, it)
1170+
11481171
def test_namespace(self):
11491172
n = self.Namespace()
11501173
n.name = 'Bob'
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)
0