Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,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 @@ -33,8 +33,16 @@ def test_FloorDiv():


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
source_code = """
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))
"""
# Assuming restricted_eval can execute the source_code and return the value of 'result'
assert restricted_eval(source_code) == 42