Skip to content

Commit 7ab1b16

Browse files
authored
Merge pull request RustPython#4153 from jopemachine/add-call-operator
Add `operator.call`
2 parents a719955 + 16755fa commit 7ab1b16

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Lib/operator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
This is the pure Python implementation of the module.
1111
"""
1212

13-
__all__ = ['abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf',
13+
__all__ = ['abs', 'add', 'and_', 'attrgetter', 'call', 'concat', 'contains', 'countOf',
1414
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
1515
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
1616
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
@@ -221,6 +221,12 @@ def length_hint(obj, default=0):
221221
raise ValueError(msg)
222222
return val
223223

224+
# Other Operations ************************************************************#
225+
226+
def call(obj, /, *args, **kwargs):
227+
"""Same as obj(*args, **kwargs)."""
228+
return obj(*args, **kwargs)
229+
224230
# Generalized Lookup Objects **************************************************#
225231

226232
class attrgetter:
@@ -423,6 +429,7 @@ def ixor(a, b):
423429
__abs__ = abs
424430
__add__ = add
425431
__and__ = and_
432+
__call__ = call
426433
__floordiv__ = floordiv
427434
__index__ = index
428435
__inv__ = inv

Lib/test/test_operator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ def __iter__(self):
4545

4646

4747
class OperatorTestCase:
48+
def test___all__(self):
49+
operator = self.module
50+
actual_all = set(operator.__all__)
51+
computed_all = set()
52+
for name in vars(operator):
53+
if name.startswith('__'):
54+
continue
55+
value = getattr(operator, name)
56+
if value.__module__ in ('operator', '_operator'):
57+
computed_all.add(name)
58+
self.assertSetEqual(computed_all, actual_all)
59+
4860
def test_lt(self):
4961
operator = self.module
5062
self.assertRaises(TypeError, operator.lt)
@@ -518,6 +530,18 @@ def __length_hint__(self):
518530
with self.assertRaises(LookupError):
519531
operator.length_hint(X(LookupError))
520532

533+
def test_call(self):
534+
operator = self.module
535+
536+
def func(*args, **kwargs): return args, kwargs
537+
538+
self.assertEqual(operator.call(func), ((), {}))
539+
self.assertEqual(operator.call(func, 0, 1), ((0, 1), {}))
540+
self.assertEqual(operator.call(func, a=2, obj=3),
541+
((), {"a": 2, "obj": 3}))
542+
self.assertEqual(operator.call(func, 0, 1, a=2, obj=3),
543+
((0, 1), {"a": 2, "obj": 3}))
544+
521545
def test_dunder_is_original(self):
522546
operator = self.module
523547

vm/src/stdlib/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ mod _operator {
139139

140140
/// Return a @ b
141141
#[pyfunction]
142-
fn mat_mul(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult {
142+
fn matmul(a: PyObjectRef, b: PyObjectRef, vm: &VirtualMachine) -> PyResult {
143143
vm._matmul(&a, &b)
144144
}
145145

0 commit comments

Comments
 (0)