Skip to content

Commit

Permalink
enable single mode
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Feb 29, 2024
1 parent 97d96b0 commit a9c4a1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ def visit_NameConstant(self, node):
"""
return self.node_contents_visit(node)

def visit_Interactive(self, node):
"""Allow single mode without restrictions."""
return self.node_contents_visit(node)

def visit_List(self, node):
"""Allow list literals without restrictions."""
return self.node_contents_visit(node)
Expand Down
23 changes: 17 additions & 6 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,24 @@ def test_compile__compile_restricted_eval__used_names():
assert result.used_names == {'a': True, 'b': True, 'x': True, 'func': True}


def test_compile__compile_restricted_csingle():
def test_compile__compile_restricted_csingle_allowed():
"""It compiles code as an Interactive."""
result = compile_restricted_single('4 * 6')
assert result.code is None
assert result.errors == (
'Line None: Interactive statements are not allowed.',
)
result = compile_restricted_single('x = 4 * 6')

assert result.errors == ()
assert result.warnings == []
assert result.code is not None
locals = {}
exec(result.code, {}, locals)
assert locals["x"] == 24


def test_compile__compile_restricted_csingle_allowed2():
"""It compiles code as an Interactive."""
code = compile_restricted('x = 4 * 6', filename="<string>", mode="single")
locals = {}
exec(code, {}, locals)
assert locals["x"] == 24


PRINT_EXAMPLE = """
Expand Down

0 comments on commit a9c4a1f

Please sign in to comment.