1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
import pytest
from RestrictedPython import compile_restricted_exec
from RestrictedPython._compat import IS_PY311_OR_GREATER
from tests.helper import restricted_exec
TRY_EXCEPT = """
def try_except(m):
try:
m('try')
raise IndentationError('f1')
except IndentationError as error:
m('except')
"""
def test_RestrictingNodeTransformer__visit_Try__1(
mocker):
"""It allows try-except statements."""
trace = mocker.stub()
restricted_exec(TRY_EXCEPT)['try_except'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('except')
])
TRY_EXCEPT_ELSE = """
def try_except_else(m):
try:
m('try')
except:
m('except')
else:
m('else')
"""
def test_RestrictingNodeTransformer__visit_Try__2(
mocker):
"""It allows try-except-else statements."""
trace = mocker.stub()
restricted_exec(TRY_EXCEPT_ELSE)['try_except_else'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('else')
])
TRY_EXCEPT_STAR = """
def try_except_star(m):
try:
m('try')
raise ExceptionGroup("group", [IndentationError('f1'), ValueError(65)])
except* IndentationError:
m('IndentationError')
except* ValueError:
m('ValueError')
except* RuntimeError:
m('RuntimeError')
"""
@pytest.mark.skipif(
not IS_PY311_OR_GREATER,
reason="ExceptionGroup class was added in Python 3.11.",
)
def test_RestrictingNodeTransformer__visit_TryStar__1():
"""It denies try-except* PEP 654 statements."""
result = compile_restricted_exec(TRY_EXCEPT_STAR)
assert result.errors == (
'Line 3: TryStar statements are not allowed.',
)
assert result.code is None
TRY_FINALLY = """
def try_finally(m):
try:
m('try')
1 / 0
finally:
m('finally')
return
"""
def test_RestrictingNodeTransformer__visit_TryFinally__1(
mocker):
"""It allows try-finally statements."""
trace = mocker.stub()
restricted_exec(TRY_FINALLY)['try_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('finally')
])
TRY_EXCEPT_FINALLY = """
def try_except_finally(m):
try:
m('try')
1 / 0
except:
m('except')
finally:
m('finally')
"""
def test_RestrictingNodeTransformer__visit_TryFinally__2(
mocker):
"""It allows try-except-finally statements."""
trace = mocker.stub()
restricted_exec(TRY_EXCEPT_FINALLY)['try_except_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('except'),
mocker.call('finally')
])
TRY_EXCEPT_ELSE_FINALLY = """
def try_except_else_finally(m):
try:
m('try')
except:
m('except')
else:
m('else')
finally:
m('finally')
"""
def test_RestrictingNodeTransformer__visit_TryFinally__3(
mocker):
"""It allows try-except-else-finally statements."""
trace = mocker.stub()
restricted_exec(TRY_EXCEPT_ELSE_FINALLY)['try_except_else_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('else'),
mocker.call('finally')
])
BAD_TRY_EXCEPT = """
def except_using_bad_name():
try:
foo
except NameError as _leading_underscore:
# The name of choice (say, _write) is now assigned to an exception
# object. Hard to exploit, but conceivable.
pass
"""
def test_RestrictingNodeTransformer__visit_ExceptHandler__2():
"""It denies bad names in the except as statement."""
result = compile_restricted_exec(BAD_TRY_EXCEPT)
assert result.errors == (
'Line 5: "_leading_underscore" is an invalid variable name because '
'it starts with "_"',)
|