• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / RestrictedPython / 11235097670

08 Oct 2024 12:09PM UTC coverage: 98.768% (-0.1%) from 98.863%
11235097670

Pull #289

github

dataflake
- remove all pre-Python 38 compatibility code
Pull Request #289: Support Python 3.13

371 of 391 branches covered (94.88%)

16 of 17 new or added lines in 3 files covered. (94.12%)

27 existing lines in 4 files now uncovered.

2486 of 2517 relevant lines covered (98.77%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.2
/tests/test_compile_restricted_function.py
1
from types import FunctionType
1✔
2

3
from RestrictedPython import PrintCollector
1✔
4
from RestrictedPython import compile_restricted_function
1✔
5
from RestrictedPython import safe_builtins
1✔
6
from RestrictedPython._compat import IS_PY310_OR_GREATER
1✔
7

8

9
def test_compile_restricted_function():
1✔
10
    p = ''
1✔
11
    body = """
1✔
12
print("Hello World!")
13
return printed
14
"""
15
    name = "hello_world"
1✔
16
    global_symbols = []
1✔
17

18
    result = compile_restricted_function(
1✔
19
        p,  # parameters
20
        body,
21
        name,
22
        filename='<string>',
23
        globalize=global_symbols
24
    )
25

26
    assert result.code is not None
1✔
27
    assert result.errors == ()
1✔
28

29
    safe_globals = {
1✔
30
        '__name__': 'script',
31
        '_getattr_': getattr,
32
        '_print_': PrintCollector,
33
        '__builtins__': safe_builtins,
34
    }
35
    safe_locals = {}
1✔
36
    exec(result.code, safe_globals, safe_locals)
1✔
37
    hello_world = safe_locals['hello_world']
1✔
38
    assert type(hello_world) is FunctionType
1✔
39
    assert hello_world() == 'Hello World!\n'
1✔
40

41

42
def test_compile_restricted_function_func_wrapped():
1✔
43
    p = ''
1✔
44
    body = """
1✔
45
print("Hello World!")
46
return printed
47
"""
48
    name = "hello_world"
1✔
49
    global_symbols = []
1✔
50

51
    result = compile_restricted_function(
1✔
52
        p,  # parameters
53
        body,
54
        name,
55
        filename='<string>',
56
        globalize=global_symbols
57
    )
58

59
    assert result.code is not None
1✔
60
    assert result.errors == ()
1✔
61
    safe_globals = {
1✔
62
        '__name__': 'script',
63
        '_getattr_': getattr,
64
        '_print_': PrintCollector,
65
        '__builtins__': safe_builtins,
66
    }
67

68
    func = FunctionType(result.code, safe_globals)
1✔
69
    func()
1✔
70
    assert 'hello_world' in safe_globals
1✔
71
    hello_world = safe_globals['hello_world']
1✔
72
    assert hello_world() == 'Hello World!\n'
1✔
73

74

75
def test_compile_restricted_function_with_arguments():
1✔
76
    p = 'input1, input2'
1✔
77
    body = """
1✔
78
print(input1 + input2)
79
return printed
80
"""
81
    name = "hello_world"
1✔
82
    global_symbols = []
1✔
83

84
    result = compile_restricted_function(
1✔
85
        p,  # parameters
86
        body,
87
        name,
88
        filename='<string>',
89
        globalize=global_symbols
90
    )
91

92
    assert result.code is not None
1✔
93
    assert result.errors == ()
1✔
94

95
    safe_globals = {
1✔
96
        '__name__': 'script',
97
        '_getattr_': getattr,
98
        '_print_': PrintCollector,
99
        '__builtins__': safe_builtins,
100
    }
101
    safe_locals = {}
1✔
102
    exec(result.code, safe_globals, safe_locals)
1✔
103
    hello_world = safe_locals['hello_world']
1✔
104
    assert type(hello_world) is FunctionType
1✔
105
    assert hello_world('Hello ', 'World!') == 'Hello World!\n'
1✔
106

107

108
def test_compile_restricted_function_can_access_global_variables():
1✔
109
    p = ''
1✔
110
    body = """
1✔
111
print(input)
112
return printed
113
"""
114
    name = "hello_world"
1✔
115
    global_symbols = ['input']
1✔
116

117
    result = compile_restricted_function(
1✔
118
        p,  # parameters
119
        body,
120
        name,
121
        filename='<string>',
122
        globalize=global_symbols
123
    )
124

125
    assert result.code is not None
1✔
126
    assert result.errors == ()
1✔
127

128
    safe_globals = {
1✔
129
        '__name__': 'script',
130
        '_getattr_': getattr,
131
        'input': 'Hello World!',
132
        '_print_': PrintCollector,
133
        '__builtins__': safe_builtins,
134
    }
135
    safe_locals = {}
1✔
136
    exec(result.code, safe_globals, safe_locals)
1✔
137
    hello_world = safe_locals['hello_world']
1✔
138
    assert type(hello_world) is FunctionType
1✔
139
    assert hello_world() == 'Hello World!\n'
1✔
140

141

142
def test_compile_restricted_function_pretends_the_code_is_executed_in_a_global_scope():  # NOQA: E501
1✔
143
    p = ''
1✔
144
    body = """output = output + 'bar'"""
1✔
145
    name = "hello_world"
1✔
146
    global_symbols = ['output']
1✔
147

148
    result = compile_restricted_function(
1✔
149
        p,  # parameters
150
        body,
151
        name,
152
        filename='<string>',
153
        globalize=global_symbols
154
    )
155

156
    assert result.code is not None
1✔
157
    assert result.errors == ()
1✔
158

159
    safe_globals = {
1✔
160
        '__name__': 'script',
161
        'output': 'foo',
162
        '__builtins__': {},
163
    }
164
    safe_locals = {}
1✔
165
    exec(result.code, safe_globals, safe_locals)
1✔
166
    hello_world = safe_locals['hello_world']
1✔
167
    assert type(hello_world) is FunctionType
1✔
168
    hello_world()
1✔
169
    assert safe_globals['output'] == 'foobar'
1✔
170

171

172
def test_compile_restricted_function_allows_invalid_python_identifiers_as_function_name():  # NOQA: E501
1✔
173
    p = ''
1✔
174
    body = """output = output + 'bar'"""
1✔
175
    name = "<foo>.bar.__baz__"
1✔
176
    global_symbols = ['output']
1✔
177

178
    result = compile_restricted_function(
1✔
179
        p,  # parameters
180
        body,
181
        name,
182
        filename='<string>',
183
        globalize=global_symbols
184
    )
185

186
    assert result.code is not None
1✔
187
    assert result.errors == ()
1✔
188

189
    safe_globals = {
1✔
190
        '__name__': 'script',
191
        'output': 'foo',
192
        '__builtins__': {},
193
    }
194
    safe_locals = {}
1✔
195
    exec(result.code, safe_globals, safe_locals)
1✔
196
    generated_function = tuple(safe_locals.values())[0]
1✔
197
    assert type(generated_function) is FunctionType
1✔
198
    generated_function()
1✔
199
    assert safe_globals['output'] == 'foobar'
1✔
200

201

202
def test_compile_restricted_function_handle_SyntaxError():
1✔
203
    p = ''
1✔
204
    body = """a("""
1✔
205
    name = "broken"
1✔
206

207
    result = compile_restricted_function(
1✔
208
        p,  # parameters
209
        body,
210
        name,
211
    )
212

213
    assert result.code is None
1✔
214
    if IS_PY310_OR_GREATER:
1!
215
        assert result.errors == (
1✔
216
            "Line 1: SyntaxError: '(' was never closed at statement: 'a('",
217
        )
218
    else:
UNCOV
219
        assert result.errors == (
×
220
            "Line 1: SyntaxError: unexpected EOF while parsing at statement:"
221
            " 'a('",
222
        )
223

224

225
def test_compile_restricted_function_invalid_syntax():
1✔
226
    p = ''
1✔
227
    body = '1=1'
1✔
228
    name = 'broken'
1✔
229

230
    result = compile_restricted_function(
1✔
231
        p,  # parameters
232
        body,
233
        name,
234
    )
235

236
    assert result.code is None
1✔
237
    assert len(result.errors) == 1
1✔
238
    error_msg = result.errors[0]
1✔
239

240
    if IS_PY310_OR_GREATER:
1!
241
        assert error_msg.startswith(
1✔
242
            "Line 1: SyntaxError: cannot assign to literal here. Maybe "
243
        )
244
    else:
UNCOV
245
        assert error_msg.startswith(
×
246
            "Line 1: SyntaxError: cannot assign to literal at statement:"
247
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc