10BC0 Add `operator.call` by jopemachine · Pull Request #4153 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -423,6 +429,7 @@ def ixor(a, b):
__abs__ = abs
__add__ = add
__and__ = and_
__call__ = call
__floordiv__ = floordiv
__index__ = index
__inv__ = inv
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -518,6 +530,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

Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
0