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 = [
57
57
]
58
58
59
59
[coverage ]
60
- fail-under = 98.8
60
+ fail-under = 98.5
61
61
62
62
[isort ]
63
63
additional-sources = " {toxinidir}/tests"
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ Features
14
14
15
15
- Officially support Python 3.11.
16
16
17
+ - Add test for trystar syntax.
18
+
17
19
18
20
5.2 (2021-11-19)
19
21
----------------
Original file line number Diff line number Diff line change 17
17
18
18
import builtins
19
19
20
+ from RestrictedPython ._compat import IS_PY311_OR_GREATER
21
+
20
22
21
23
safe_builtins = {}
22
24
103
105
'ZeroDivisionError' ,
104
106
]
105
107
108
+ if IS_PY311_OR_GREATER :
109
+ _safe_exceptions .append ("ExceptionGroup" )
110
+
106
111
for name in _safe_names :
107
112
safe_builtins [name ] = getattr (builtins , name )
108
113
Original file line number Diff line number Diff line change 6
6
IS_PY37_OR_GREATER = _version .major == 3 and _version .minor >= 7
7
7
IS_PY38_OR_GREATER = _version .major == 3 and _version .minor >= 8
8
8
IS_PY310_OR_GREATER = _version .major == 3 and _version .minor >= 10
9
+ IS_PY311_OR_GREATER = _version .major == 3 and _version .minor >= 11
9
10
10
11
IS_CPYTHON = platform .python_implementation () == 'CPython'
Original file line number Diff line number Diff line change @@ -1127,6 +1127,10 @@ def visit_Try(self, node):
1127
1127
"""Allow `try` without restrictions."""
1128
1128
return self .node_contents_visit (node )
1129
1129
1130
+ def visit_TryStar (self , node ):
1131
+ """Allow `ExceptionGroup` without restrictions."""
1132
+ return self .node_contents_visit (node )
1133
+
1130
1134
def visit_ExceptHandler (self , node ):
1131
1135
"""Protect exception handlers."""
1132
1136
node = self .node_contents_visit (node )
Original file line number Diff line number Diff line change
1
+ import pytest
2
+
1
3
from RestrictedPython import compile_restricted_exec
4
+ from RestrictedPython ._compat import IS_PY311_OR_GREATER
2
5
from tests .helper import restricted_exec
3
6
4
7
@@ -47,6 +50,39 @@ def test_RestrictingNodeTransformer__visit_Try__2(
47
50
])
48
51
49
52
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
+
50
86
TRY_FINALLY = """
51
87
def try_finally(m):
52
88
try:
Original file line number Diff line number Diff line change @@ -96,7 +96,7 @@ commands =
96
96
pytest --cov =src --cov =tests --cov-report = {posargs}
97
97
coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
98
98
coverage html
99
- coverage report -m --fail-under =98.8
99
+ coverage report -m --fail-under =98.5
100
100
101
101
[coverage:run]
102
102
branch = True
You can’t perform that action at this time.
0 commit comments