File: test_inspect.py

package info (click to toggle)
restrictedpython 8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: python: 4,043; makefile: 193
file content (60 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download
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
from RestrictedPython import compile_restricted_exec


def test_get_inspect_frame_on_generator():
    source_code = """
generator = (statement.gi_frame for _ in (1,))
generator_element = [elem for elem in generator][0]

"""
    result = compile_restricted_exec(source_code)
    assert result.errors == (
        'Line 2: "gi_frame" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
    )


def test_get_inspect_frame_back_on_generator():
    source_code = """
generator = (statement.gi_frame.f_back.f_back for _ in (1,))
generator_element = [elem for elem in generator][0]

"""
    result = compile_restricted_exec(source_code)
    assert result.errors == (
        'Line 2: "f_back" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
        'Line 2: "f_back" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
        'Line 2: "gi_frame" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
    )


def test_call_inspect_frame_on_generator():
    source_code = """
generator = None
frame = None

def test():
    global generator, frame
    frame = g.gi_frame.f_back.f_back
    yield frame

generator = test()
generator.send(None)
os = frame.f_builtins.get('__import__')('os')

result = os.listdir('/')
"""
    result = compile_restricted_exec(source_code)
    assert result.errors == (
        'Line 7: "f_back" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
        'Line 7: "f_back" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
        'Line 7: "gi_frame" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
        'Line 12: "f_builtins" is a restricted name, '
        'that is forbidden to access in RestrictedPython.',
    )