From e0e5065daef36dafe10a46eaa8b7800274d73062 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 17 Sep 2018 14:24:01 +0300 Subject: [PATCH] bpo-34610: Fixed iterator of multiprocessing.managers.DictProxy. (GH-9113) --- Lib/multiprocessing/managers.py | 5 +++- Lib/test/_test_multiprocessing.py | 23 +++++++++++++++++++ .../2018-09-08-12-57-07.bpo-34610.wmoP5j.rst | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-09-08-12-57-07.bpo-34610.wmoP5j.rst diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py index 3f263802bc..dbed993a38 100644 --- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1134,10 +1134,13 @@ class ListProxy(BaseListProxy): DictProxy = MakeProxyType('DictProxy', ( - '__contains__', '__delitem__', '__getitem__', '__len__', + '__contains__', '__delitem__', '__getitem__', '__iter__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'has_key', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values' )) +DictProxy._method_to_typeid_ = { + '__iter__': 'Iterator', + } ArrayProxy = MakeProxyType('ArrayProxy', ( diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index a5509ce913..bb9eb240a8 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -2080,6 +2080,16 @@ class _TestContainers(BaseTestCase): a.append('hello') self.assertEqual(f[0][:], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'hello']) + def test_list_iter(self): + a = self.list(list(range(10))) + it = iter(a) + self.assertEqual(list(it), list(range(10))) + self.assertEqual(list(it), []) # exhausted + # list modified during iteration + it = iter(a) + a[0] = 100 + self.assertEqual(next(it), 100) + def test_list_proxy_in_list(self): a = self.list([self.list(range(3)) for _i in range(3)]) self.assertEqual([inner[:] for inner in a], [[0, 1, 2]] * 3) @@ -2110,6 +2120,19 @@ class _TestContainers(BaseTestCase): self.assertEqual(sorted(d.values()), [chr(i) for i in indices]) self.assertEqual(sorted(d.items()), [(i, chr(i)) for i in indices]) + def test_dict_iter(self): + d = self.dict() + indices = list(range(65, 70)) + for i in indices: + d[i] = chr(i) + it = iter(d) + self.assertEqual(list(it), indices) + self.assertEqual(list(it), []) # exhausted + # dictionary changed size during iteration + it = iter(d) + d.clear() + self.assertRaises(RuntimeError, next, it) + def test_dict_proxy_nested(self): pets = self.dict(ferrets=2, hamsters=4) supplies = self.dict(water=10, feed=3) diff --git a/Misc/NEWS.d/next/Library/2018-09-08-12-57-07.bpo-34610.wmoP5j.rst b/Misc/NEWS.d/next/Library/2018-09-08-12-57-07.bpo-34610.wmoP5j.rst new file mode 100644 index 0000000000..bffb355ea2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-08-12-57-07.bpo-34610.wmoP5j.rst @@ -0,0 +1 @@ +Fixed iterator of :class:`multiprocessing.managers.DictProxy`. -- 2.40.0