Skip to content

Commit 688bec4

Browse files
eikichi18Michael Howitzdataflake
authored
add test for except star (#237)
* add test for except star * fixed test with decorator skipif and standardize .gitignore file * add some comments * more clear syntax * removed typo * impreve test for except* * Update tests/transformer/test_try.py Co-authored-by: Michael Howitz <[email protected]> * updated CHANGES + decreased percentage of fail-under limit to 98.5 * updated CHANGES * add python 3.11 in setup.py * - reinstate accidentally removed change log entry [ci skip] Co-authored-by: Michael Howitz <[email protected]> Co-authored-by: Jens Vagelpohl <[email protected]> Co-authored-by: Jens Vagelpohl <[email protected]>
1 parent eed9d9d commit 688bec4

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

.meta.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ coverage-setenv = [
5757
]
5858

5959
[coverage]
60-
fail-under = 98.8
60+
fail-under = 98.5
6161

6262
[isort]
6363
additional-sources = "{toxinidir}/tests"

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Features
1414

1515
- Officially support Python 3.11.
1616

17+
- Add test for trystar syntax.
18+
1719

1820
5.2 (2021-11-19)
1921
----------------

src/RestrictedPython/Guards.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import builtins
1919

20+
from RestrictedPython._compat import IS_PY311_OR_GREATER
21+
2022

2123
safe_builtins = {}
2224

@@ -103,6 +105,9 @@
103105
'ZeroDivisionError',
104106
]
105107

108+
if IS_PY311_OR_GREATER:
109+
_safe_exceptions.append("ExceptionGroup")
110+
106111
for name in _safe_names:
107112
safe_builtins[name] = getattr(builtins, name)
108113

src/RestrictedPython/_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
IS_PY37_OR_GREATER = _version.major == 3 and _version.minor >= 7
77
IS_PY38_OR_GREATER = _version.major == 3 and _version.minor >= 8
88
IS_PY310_OR_GREATER = _version.major == 3 and _version.minor >= 10
9+
IS_PY311_OR_GREATER = _version.major == 3 and _version.minor >= 11
910

1011
IS_CPYTHON = platform.python_implementation() == 'CPython'

src/RestrictedPython/transformer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,10 @@ def visit_Try(self, node):
11271127
"""Allow `try` without restrictions."""
11281128
return self.node_contents_visit(node)
11291129

1130+
def visit_TryStar(self, node):
1131+
"""Allow `ExceptionGroup` without restrictions."""
1132+
return self.node_contents_visit(node)
1133+
11301134
def visit_ExceptHandler(self, node):
11311135
"""Protect exception handlers."""
11321136
node = self.node_contents_visit(node)

tests/transformer/test_try.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import pytest
2+
13
from RestrictedPython import compile_restricted_exec
4+
from RestrictedPython._compat import IS_PY311_OR_GREATER
25
from tests.helper import restricted_exec
36

47

@@ -47,6 +50,39 @@ def test_RestrictingNodeTransformer__visit_Try__2(
4750
])
4851

4952

53+
TRY_EXCEPT_STAR = """
54+
def try_except_star(m):
55+
try:
56+
m('try')
57+
raise ExceptionGroup("group", [IndentationError('f1'), ValueError(65)])
58+
except* IndentationError:
59+
m('IndentationError')
60+
except* ValueError:
61+
m('ValueError')
62+
except* RuntimeError:
63+
m('RuntimeError')
64+
"""
65+
66+
67+
@pytest.mark.skipif(
68+
not IS_PY311_OR_GREATER,
69+
reason="ExceptionGroup class was added in Python 3.11.",
70+
)
71+
def test_RestrictingNodeTransformer__visit_TryStar__1(mocker):
72+
"""It allows try-except* PEP 654 statements."""
73+
trace = mocker.stub()
74+
restricted_exec(TRY_EXCEPT_STAR)['try_except_star'](trace)
75+
76+
trace.assert_has_calls([
77+
mocker.call('try'),
78+
mocker.call('IndentationError'),
79+
mocker.call('ValueError')
80+
])
81+
82+
with pytest.raises(AssertionError):
83+
trace.assert_has_calls([mocker.call('RuntimeError')])
84+
85+
5086
TRY_FINALLY = """
5187
def try_finally(m):
5288
try:

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ commands =
9696
pytest --cov=src --cov=tests --cov-report= {posargs}
9797
coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
9898
coverage html
99-
coverage report -m --fail-under=98.8
99+
coverage report -m --fail-under=98.5
100100

101101
[coverage:run]
102102
branch = True

0 commit comments

Comments
 (0)