gh-103134: Update multiprocessing.managers.ListProxy and DictProxy (GH-103133)

This commit is contained in:
Roy Hyunjin Han 2024-05-20 10:28:36 -04:00 committed by GitHub
parent 1db4695644
commit bbb49888a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 10 deletions

View File

@ -1152,10 +1152,10 @@ class ValueProxy(BaseProxy):
BaseListProxy = MakeProxyType('BaseListProxy', ( BaseListProxy = MakeProxyType('BaseListProxy', (
'__add__', '__contains__', '__delitem__', '__getitem__', '__len__', '__add__', '__contains__', '__delitem__', '__getitem__', '__imul__',
'__mul__', '__reversed__', '__rmul__', '__setitem__', '__len__', '__mul__', '__reversed__', '__rmul__', '__setitem__',
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',
'reverse', 'sort', '__imul__' 'remove', 'reverse', 'sort',
)) ))
class ListProxy(BaseListProxy): class ListProxy(BaseListProxy):
def __iadd__(self, value): def __iadd__(self, value):
@ -1169,16 +1169,20 @@ class ListProxy(BaseListProxy):
_BaseDictProxy = MakeProxyType('DictProxy', ( _BaseDictProxy = MakeProxyType('DictProxy', (
'__contains__', '__delitem__', '__getitem__', '__iter__', '__len__', '__contains__', '__delitem__', '__getitem__', '__ior__', '__iter__',
'__setitem__', 'clear', 'copy', 'get', 'items', '__len__', '__or__', '__reversed__', '__ror__',
'__setitem__', 'clear', 'copy', 'fromkeys', 'get', 'items',
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values' 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'
)) ))
_BaseDictProxy._method_to_typeid_ = { _BaseDictProxy._method_to_typeid_ = {
'__iter__': 'Iterator', '__iter__': 'Iterator',
} }
class DictProxy(_BaseDictProxy): class DictProxy(_BaseDictProxy):
__class_getitem__ = classmethod(types.GenericAlias) def __ior__(self, value):
self._callmethod('__ior__', (value,))
return self
__class_getitem__ = classmethod(types.GenericAlias)
ArrayProxy = MakeProxyType('ArrayProxy', ( ArrayProxy = MakeProxyType('ArrayProxy', (
'__len__', '__getitem__', '__setitem__' '__len__', '__getitem__', '__setitem__'

View File

@ -6064,12 +6064,30 @@ class TestSyncManagerTypes(unittest.TestCase):
case.assertEqual(obj[0], 5) case.assertEqual(obj[0], 5)
case.assertEqual(obj.count(5), 1) case.assertEqual(obj.count(5), 1)
case.assertEqual(obj.index(5), 0) case.assertEqual(obj.index(5), 0)
obj += [7]
case.assertIsInstance(obj, multiprocessing.managers.ListProxy)
case.assertListEqual(list(obj), [5, 7])
obj *= 2
case.assertIsInstance(obj, multiprocessing.managers.ListProxy)
case.assertListEqual(list(obj), [5, 7, 5, 7])
double_obj = obj * 2
case.assertIsInstance(double_obj, list)
case.assertListEqual(list(double_obj), [5, 7, 5, 7, 5, 7, 5, 7])
double_obj = 2 * obj
case.assertIsInstance(double_obj, list)
case.assertListEqual(list(double_obj), [5, 7, 5, 7, 5, 7, 5, 7])
copied_obj = obj.copy()
case.assertIsInstance(copied_obj, list)
case.assertListEqual(list(copied_obj), [5, 7, 5, 7])
obj.extend(double_obj + copied_obj)
obj.sort() obj.sort()
obj.reverse() obj.reverse()
for x in obj: for x in obj:
pass pass
case.assertEqual(len(obj), 1) case.assertEqual(len(obj), 16)
case.assertEqual(obj.pop(0), 5) case.assertEqual(obj.pop(0), 7)
obj.clear()
case.assertEqual(len(obj), 0)
def test_list(self): def test_list(self):
o = self.manager.list() o = self.manager.list()
@ -6088,7 +6106,29 @@ class TestSyncManagerTypes(unittest.TestCase):
case.assertListEqual(list(obj.keys()), ['foo']) case.assertListEqual(list(obj.keys()), ['foo'])
case.assertListEqual(list(obj.values()), [5]) case.assertListEqual(list(obj.values()), [5])
case.assertDictEqual(obj.copy(), {'foo': 5}) case.assertDictEqual(obj.copy(), {'foo': 5})
case.assertTupleEqual(obj.popitem(), ('foo', 5)) obj |= {'bar': 6}
case.assertIsInstance(obj, multiprocessing.managers.DictProxy)
case.assertDictEqual(dict(obj), {'foo': 5, 'bar': 6})
x = reversed(obj)
case.assertIsInstance(x, type(iter([])))
case.assertListEqual(list(x), ['bar', 'foo'])
x = {'bar': 7, 'baz': 7} | obj
case.assertIsInstance(x, dict)
case.assertDictEqual(dict(x), {'foo': 5, 'bar': 6, 'baz': 7})
x = obj | {'bar': 7, 'baz': 7}
case.assertIsInstance(x, dict)
case.assertDictEqual(dict(x), {'foo': 5, 'bar': 7, 'baz': 7})
x = obj.fromkeys(['bar'], 6)
case.assertIsInstance(x, dict)
case.assertDictEqual(x, {'bar': 6})
x = obj.popitem()
case.assertIsInstance(x, tuple)
case.assertTupleEqual(x, ('bar', 6))
obj.setdefault('bar', 0)
obj.update({'bar': 7})
case.assertEqual(obj.pop('bar'), 7)
obj.clear()
case.assertEqual(len(obj), 0)
def test_dict(self): def test_dict(self):
o = self.manager.dict() o = self.manager.dict()

View File

@ -0,0 +1,6 @@
Add additional methods to :ref:`proxy objects <multiprocessing-proxy_objects>`
in the :mod:`!multiprocessing` module:
* :meth:`!clear` and :meth:`!copy` for proxies of :class:`list`
* :meth:`~dict.fromkeys`, ``reversed(d)``, ``d | {}``, ``{} | d``,
``d |= {'b': 2}`` for proxies of :class:`dict`