Skip to content

add test for except star #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/RestrictedPython/Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'EOFError',
'EnvironmentError',
'Exception',
'ExceptionGroup',
'FloatingPointError',
'FutureWarning',
'GeneratorExit',
Expand Down Expand Up @@ -107,7 +108,11 @@
safe_builtins[name] = getattr(builtins, name)

for name in _safe_exceptions:
safe_builtins[name] = getattr(builtins, name)
builtin = getattr(builtins, name, None)
# PR:237 add because import of class ExceptionGroup in python smaller
# than 3.11 cause an error
if builtin:
safe_builtins[name] = getattr(builtins, name)


# Wrappers provided by this module:
Expand Down
1 change: 1 addition & 0 deletions src/RestrictedPython/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,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
IS_PY311_OR_GREATER = _version.major == 3 and _version.minor >= 11

IS_CPYTHON = platform.python_implementation() == 'CPython'
4 changes: 4 additions & 0 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,10 @@ def visit_Try(self, node):
"""Allow `try` without restrictions."""
return self.node_contents_visit(node)

def visit_TryStar(self, node):
"""Allow `ExceptionGroup` without restrictions."""
return self.node_contents_visit(node)

def visit_ExceptHandler(self, node):
"""Protect exception handlers."""
node = self.node_contents_visit(node)
Expand Down
31 changes: 31 additions & 0 deletions tests/transformer/test_try.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest

from RestrictedPython import compile_restricted_exec
from RestrictedPython._compat import IS_PY311_OR_GREATER
from tests.helper import restricted_exec


Expand Down Expand Up @@ -47,6 +50,34 @@ def test_RestrictingNodeTransformer__visit_Try__2(
])


TRY_EXCEPT_STAR = """
def try_except_star(m):
try:
m('try')
raise ExceptionGroup("group", [IndentationError('f1'), ValueError(65)])
except* IndentationError:
m('IndentetionError')
except* ValueError:
m('ValueError')
"""


@pytest.mark.skipif(
not IS_PY311_OR_GREATER,
reason="ExceptionGroup class are added in Python 3.11.",
)
def test_RestrictingNodeTransformer__visit_Try__3(mocker):
"""It allows try-except statements."""
trace = mocker.stub()
restricted_exec(TRY_EXCEPT_STAR)['try_except_star'](trace)

trace.assert_has_calls([
mocker.call('try'),
mocker.call('IndentetionError'),
mocker.call('ValueError')
])


TRY_FINALLY = """
def try_finally(m):
try:
Expand Down