Skip to content

Commit

Permalink
Fix tests on Python 3.10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Sep 29, 2021
1 parent efb5a2a commit 20af489
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .meta.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ coverage-setenv = [
]

[coverage]
fail-under = 99.2
fail-under = 98.9

[manifest]
additional-rules = [
Expand Down
1 change: 1 addition & 0 deletions src/RestrictedPython/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
IS_PY36_OR_GREATER = _version.major == 3 and _version.minor >= 6
IS_PY37_OR_GREATER = _version.major == 3 and _version.minor >= 7
IS_PY38_OR_GREATER = _version.major == 3 and _version.minor >= 8
IS_PY310_OR_GREATER = _version.major == 3 and _version.minor >= 10

if IS_PY2: # pragma: PY2
basestring = basestring # NOQA: F821 # Python 2 only built-in function
Expand Down
17 changes: 13 additions & 4 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from RestrictedPython._compat import IS_PY2
from RestrictedPython._compat import IS_PY3
from RestrictedPython._compat import IS_PY38_OR_GREATER
from RestrictedPython._compat import IS_PY310_OR_GREATER
from tests.helper import restricted_eval

import platform
Expand Down Expand Up @@ -40,7 +41,9 @@ def test_compile__compile_restricted_invalid_mode_input():
def test_compile__invalid_syntax():
with pytest.raises(SyntaxError) as err:
compile_restricted(INVALID_ASSINGMENT, '<string>', 'exec')
if IS_PY38_OR_GREATER:
if IS_PY310_OR_GREATER:
assert "SyntaxError: cannot assign to literal here." in str(err.value)
elif IS_PY38_OR_GREATER:
assert "cannot assign to literal at statement:" in str(err.value)
else:
assert "can't assign to literal at statement:" in str(err.value)
Expand Down Expand Up @@ -120,9 +123,15 @@ def no_exec():
def test_compile__compile_restricted_exec__10(): # pragma: PY3
"""It is a SyntaxError to use the `exec` statement. (Python 3 only)"""
result = compile_restricted_exec(EXEC_STATEMENT)
assert (
'Line 2: SyntaxError: Missing parentheses in call to \'exec\' at '
'statement: "exec \'q = 1\'"',) == result.errors
if IS_PY310_OR_GREATER:
assert (
'Line 2: SyntaxError: Missing parentheses in call to \'exec\'. Did'
' you mean exec(...)? at statement: "exec \'q = 1\'"',
) == result.errors
else:
assert (
'Line 2: SyntaxError: Missing parentheses in call to \'exec\' at'
' statement: "exec \'q = 1\'"',) == result.errors


FUNCTION_DEF = """\
Expand Down
19 changes: 15 additions & 4 deletions tests/test_compile_restricted_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from RestrictedPython import PrintCollector
from RestrictedPython import safe_builtins
from RestrictedPython._compat import IS_PY38_OR_GREATER
from RestrictedPython._compat import IS_PY310_OR_GREATER
from types import FunctionType


Expand Down Expand Up @@ -210,9 +211,15 @@ def test_compile_restricted_function_handle_SyntaxError():
)

assert result.code is None
assert result.errors == (
"Line 1: SyntaxError: unexpected EOF while parsing at statement: 'a('",
)
if IS_PY310_OR_GREATER:
assert result.errors == (
"Line 1: SyntaxError: '(' was never closed at statement: 'a('",
)
else:
assert result.errors == (
"Line 1: SyntaxError: unexpected EOF while parsing at statement:"
" 'a('",
)


def test_compile_restricted_function_invalid_syntax():
Expand All @@ -230,7 +237,11 @@ def test_compile_restricted_function_invalid_syntax():
assert len(result.errors) == 1
error_msg = result.errors[0]

if IS_PY38_OR_GREATER:
if IS_PY310_OR_GREATER:
assert error_msg.startswith(
"Line 1: SyntaxError: cannot assign to literal here. Maybe "
)
elif IS_PY38_OR_GREATER:
assert error_msg.startswith(
"Line 1: SyntaxError: cannot assign to literal at statement:"
)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ commands =
pytest --cov=src --cov=tests --cov-report= {posargs}
coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
coverage html
coverage report -m --fail-under=99.2
coverage report -m --fail-under=98.9

[coverage:run]
branch = True
Expand Down

0 comments on commit 20af489

Please sign in to comment.