Skip to content

Commit 9d0a217

Browse files
authored
- Fix compile_restricted_function with SyntaxErrors that have no text (#199)
1 parent f1f1bdb commit 9d0a217

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docs/CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Changes
44
5.1a0 (unreleased)
55
------------------
66

7+
- Fix ``compile_restricted_function`` with SyntaxErrors that have no text
8+
(`#181 <https://github.com/zopefoundation/RestrictedPython/issues/181>`_)
9+
710
- Add support for the ``bytes`` and ``sorted`` builtins
811
(`#186 <https://github.com/zopefoundation/RestrictedPython/issues/186>`_)
912

src/RestrictedPython/compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def compile_restricted_function(
150150
lineno=v.lineno,
151151
type=v.__class__.__name__,
152152
msg=v.msg,
153-
statement=v.text.strip())
153+
statement=v.text.strip() if v.text else None)
154154
return CompileResult(
155155
code=None, errors=(error,), warnings=(), used_names=())
156156

tests/test_compile_restricted_function.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from RestrictedPython import compile_restricted_function
22
from RestrictedPython import PrintCollector
33
from RestrictedPython import safe_builtins
4+
from RestrictedPython._compat import IS_PY38_OR_GREATER
45
from types import FunctionType
56

67

@@ -212,3 +213,28 @@ def test_compile_restricted_function_handle_SyntaxError():
212213
assert result.errors == (
213214
"Line 1: SyntaxError: unexpected EOF while parsing at statement: 'a('",
214215
)
216+
217+
218+
def test_compile_restricted_function_invalid_syntax():
219+
p = ''
220+
body = '1=1'
221+
name = 'broken'
222+
223+
result = compile_restricted_function(
224+
p, # parameters
225+
body,
226+
name,
227+
)
228+
229+
assert result.code is None
230+
assert len(result.errors) == 1
231+
error_msg = result.errors[0]
232+
233+
if IS_PY38_OR_GREATER:
234+
assert error_msg.startswith(
235+
"Line 1: SyntaxError: cannot assign to literal at statement:"
236+
)
237+
else:
238+
assert error_msg.startswith(
239+
"Line 1: SyntaxError: can't assign to literal at statement:"
240+
)

0 commit comments

Comments
 (0)