File tree Expand file tree Collapse file tree 7 files changed +50
-2
lines changed Expand file tree Collapse file tree 7 files changed +50
-2
lines changed Original file line number Diff line number Diff 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 ]
6363additional-sources = " {toxinidir}/tests"
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ Features
1414
1515- Officially support Python 3.11.
1616
17+ - Add test for trystar syntax.
18+
1719
18205.2 (2021-11-19)
1921----------------
Original file line number Diff line number Diff line change 1717
1818import builtins
1919
20+ from RestrictedPython ._compat import IS_PY311_OR_GREATER
21+
2022
2123safe_builtins = {}
2224
103105 'ZeroDivisionError' ,
104106]
105107
108+ if IS_PY311_OR_GREATER :
109+ _safe_exceptions .append ("ExceptionGroup" )
110+
106111for name in _safe_names :
107112 safe_builtins [name ] = getattr (builtins , name )
108113
Original file line number Diff line number Diff line change 66IS_PY37_OR_GREATER = _version .major == 3 and _version .minor >= 7
77IS_PY38_OR_GREATER = _version .major == 3 and _version .minor >= 8
88IS_PY310_OR_GREATER = _version .major == 3 and _version .minor >= 10
9+ IS_PY311_OR_GREATER = _version .major == 3 and _version .minor >= 11
910
1011IS_CPYTHON = platform .python_implementation () == 'CPython'
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1+ import pytest
2+
13from RestrictedPython import compile_restricted_exec
4+ from RestrictedPython ._compat import IS_PY311_OR_GREATER
25from 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+
5086TRY_FINALLY = """
5187def try_finally(m):
5288 try:
Original file line number Diff line number Diff 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]
102102branch = True
You can’t perform that action at this time.
0 commit comments