From d95b3c276199fc70a376f6ceaf982c9d8cf262e0 Mon Sep 17 00:00:00 2001 From: Gyubong Date: Fri, 9 Sep 2022 11:21:19 +0900 Subject: [PATCH 1/4] Add `opeator.call`, such that `operator.call(obj, *args, **kwargs) == obj(*args, **kwargs)` --- Lib/operator.py | 7 +++++++ Lib/test/test_operator.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Lib/operator.py b/Lib/operator.py index 241fdbb679..72105be05f 100644 --- a/Lib/operator.py +++ b/Lib/operator.py @@ -221,6 +221,12 @@ def length_hint(obj, default=0): raise ValueError(msg) return val +# Other Operations ************************************************************# + +def call(obj, /, *args, **kwargs): + """Same as obj(*args, **kwargs).""" + return obj(*args, **kwargs) + # Generalized Lookup Objects **************************************************# class attrgetter: @@ -423,6 +429,7 @@ def ixor(a, b): __abs__ = abs __add__ = add __and__ = and_ +__call__ = call __floordiv__ = floordiv __index__ = index __inv__ = inv diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index b9b8f15582..cf3439fe6f 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -518,6 +518,18 @@ def __length_hint__(self): with self.assertRaises(LookupError): operator.length_hint(X(LookupError)) + def test_call(self): + operator = self.module + + def func(*args, **kwargs): return args, kwargs + + self.assertEqual(operator.call(func), ((), {})) + self.assertEqual(operator.call(func, 0, 1), ((0, 1), {})) + self.assertEqual(operator.call(func, a=2, obj=3), + ((), {"a": 2, "obj": 3})) + self.assertEqual(operator.call(func, 0, 1, a=2, obj=3), + ((0, 1), {"a": 2, "obj": 3})) + def test_dunder_is_original(self): operator = self.module From 339de8c809122c0ba5ee0a2b95b96d4f361b3c21 Mon Sep 17 00:00:00 2001 From: Gyubong Date: Sat, 10 Sep 2022 18:26:33 +0900 Subject: [PATCH 2/4] Add missing `call` operator to `__all__` --- Lib/operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/operator.py b/Lib/operator.py index 72105be05f..30116c1189 100644 --- a/Lib/operator.py +++ b/Lib/operator.py @@ -10,7 +10,7 @@ This is the pure Python implementation of the module. """ -__all__ = ['abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', +__all__ = ['abs', 'add', 'and_', 'attrgetter', 'call', 'concat', 'contains', 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', From 927208c7f6b7d115f7c92e48ad66b85f3ab6fbca Mon Sep 17 00:00:00 2001 From: Gyubong Date: Sat, 10 Sep 2022 18:27:31 +0900 Subject: [PATCH 3/4] Update test_operator.py from CPython v3.12.0a0 --- Lib/test/test_operator.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index cf3439fe6f..b7e38c2334 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -45,6 +45,18 @@ def __iter__(self): class OperatorTestCase: + def test___all__(self): + operator = self.module + actual_all = set(operator.__all__) + computed_all = set() + for name in vars(operator): + if name.startswith('__'): + continue + value = getattr(operator, name) + if value.__module__ in ('operator', '_operator'): + computed_all.add(name) + self.assertSetEqual(computed_all, actual_all) + def test_lt(self): operator = self.module self.assertRaises(TypeError, operator.lt) From 16755fa3b895603be5dbae97d0637065c34f8ce1 Mon Sep 17 00:00:00 2001 From: Gyubong Date: Sat, 10 Sep 2022 18:28:24 +0900 Subject: [PATCH 4/4] Change function name `mat_mul` -> `matmul` --- vm/src/stdlib/operator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index a355f1d6b7..ee6c18b479 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -139,7 +139,7 @@ mod _operator { /// Return a @ b #[pyfunction] - fn mat_mul(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn matmul(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult { vm._matmul(&a, &b) }