From f288c34585232932ad4a1a834ccf7aa3f1cf132e Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 2 Oct 2018 21:26:00 +0200 Subject: [PATCH] fix error if a SyntaxError is raised, but no text is provided --- src/RestrictedPython/compile.py | 2 +- tests/test_compile.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/RestrictedPython/compile.py b/src/RestrictedPython/compile.py index 391d4b5..aee78f6 100644 --- a/src/RestrictedPython/compile.py +++ b/src/RestrictedPython/compile.py @@ -60,7 +60,7 @@ def _compile_restricted_mode( lineno=v.lineno, type=v.__class__.__name__, msg=v.msg, - statement=v.text.strip() + statement=v.text.strip() if v.text else None )) if c_ast: policy_instance = policy( diff --git a/tests/test_compile.py b/tests/test_compile.py index 8d48ec0..5a05703 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -31,6 +31,17 @@ def test_compile__compile_restricted_invalid_mode_input(): compile_restricted("pass", '', 'invalid') +INVALID_ASSINGMENT = """ +1 = 2 +""" + + +def test_compile__invalid_syntax(): + with pytest.raises(SyntaxError) as err: + compile_restricted(INVALID_ASSINGMENT, '', 'exec') + assert "can't assign to literal at statement:" in str(err.value) + + @pytest.mark.parametrize(*c_exec) def test_compile__compile_restricted_exec__1(c_exec): """It returns a CompileResult on success."""