From a9c4a1f29c0297c346c046bcd803664fc2f8da9f Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Thu, 29 Feb 2024 16:52:18 +0100 Subject: [PATCH] enable single mode --- src/RestrictedPython/transformer.py | 4 ++++ tests/test_compile.py | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/RestrictedPython/transformer.py b/src/RestrictedPython/transformer.py index 66ae50f..9a205cc 100644 --- a/src/RestrictedPython/transformer.py +++ b/src/RestrictedPython/transformer.py @@ -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) diff --git a/tests/test_compile.py b/tests/test_compile.py index 3dacd73..b25370a 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -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="", mode="single") + locals = {} + exec(code, {}, locals) + assert locals["x"] == 24 PRINT_EXAMPLE = """