Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
'__ne__',
'__gt__',
'__ge__',
'__matmult__'
])


Expand Down Expand Up @@ -768,8 +769,8 @@ def visit_BitAnd(self, node):
return self.node_contents_visit(node)

def visit_MatMult(self, node):
"""Matrix multiplication (`@`) is currently not allowed."""
self.not_allowed(node)
"""Allow `@` expressions."""
return self.node_contents_visit(node)

def visit_BoolOp(self, node):
"""Allow bool operator without restrictions."""
Expand Down
18 changes: 13 additions & 5 deletions tests/transformer/operators/test_arithmetic_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ def test_FloorDiv():
assert restricted_eval('7 // 2') == 3


test_matmult = """\
class Vector:
def __init__(self, values):
self.values = values

def __matmul__(self, other):
return sum(x * y for x, y in zip(self.values, other.values))

result = Vector((8, 3, 5)) @ Vector((2, 7, 1))
"""

def test_MatMult():
result = compile_restricted_eval('(8, 3, 5) @ (2, 7, 1)')
assert result.errors == (
'Line None: MatMult statements are not allowed.',
)
assert result.code is None
assert restricted_eval(test_matmult) == 42