Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow @ expressions #269

Closed
wants to merge 3 commits into from
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:
rawwerks marked this conversation as resolved.
Show resolved Hide resolved
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'
rawwerks marked this conversation as resolved.
Show resolved Hide resolved
assert restricted_eval(source_code) == 42

Loading