| 1 |
from pytest import raises, mark |
| 2 |
import sys |
| 3 |
|
| 4 |
def test_simple(): |
| 5 |
co = compile('1+2', '?', 'eval') |
| 6 |
assert eval(co) == 3 |
| 7 |
co = compile(memoryview(b'1+2'), '?', 'eval') |
| 8 |
assert eval(co) == 3 |
| 9 |
exc = raises(SyntaxError, compile, chr(0), '?', 'eval') |
| 10 |
assert str(exc.value) == "source code string cannot contain null bytes" |
| 11 |
compile("from __future__ import with_statement", "<test>", "exec") |
| 12 |
raises(SyntaxError, compile, '-', '?', 'eval') |
| 13 |
raises(SyntaxError, compile, '"\\xt"', '?', 'eval') |
| 14 |
raises(ValueError, compile, '1+2', '?', 'maybenot') |
| 15 |
raises(ValueError, compile, "\n", "<string>", "exec", 0xff) |
| 16 |
raises(TypeError, compile, '1+2', 12, 34) |
| 17 |
|
| 18 |
def test_error_message(): |
| 19 |
compile('# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
| 20 |
compile(b'\xef\xbb\xbf\n', 'dummy', 'exec') |
| 21 |
compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec') |
| 22 |
exc = raises(SyntaxError, compile, |
| 23 |
b'# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 24 |
assert 'fake' in str(exc.value) |
| 25 |
exc = raises(SyntaxError, compile, |
| 26 |
b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec') |
| 27 |
assert 'iso-8859-15' in str(exc.value) |
| 28 |
assert 'BOM' in str(exc.value) |
| 29 |
exc = raises(SyntaxError, compile, |
| 30 |
b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec') |
| 31 |
assert 'fake' in str(exc.value) |
| 32 |
assert 'BOM' in str(exc.value) |
| 33 |
|
| 34 |
def test_unicode(): |
| 35 |
try: |
| 36 |
compile(u'-', '?', 'eval') |
| 37 |
except SyntaxError as e: |
| 38 |
assert e.lineno == 1 |
| 39 |
|
| 40 |
def test_incorrect_escape_deprecation_bytes(): |
| 41 |
import warnings |
| 42 |
with warnings.catch_warnings(record=True) as l: |
| 43 |
warnings.simplefilter('always', category=DeprecationWarning) |
| 44 |
compile(r"b'\}'", '', 'exec') |
| 45 |
assert len(l) == 1 |
| 46 |
|
| 47 |
def test_unicode_encoding(): |
| 48 |
code = "# -*- coding: utf-8 -*-\npass\n" |
| 49 |
compile(code, "tmp", "exec") |
| 50 |
|
| 51 |
def test_bytes(): |
| 52 |
code = b"# -*- coding: utf-8 -*-\npass\n" |
| 53 |
compile(code, "tmp", "exec") |
| 54 |
c = compile(b"# coding: latin1\nfoo = 'caf\xe9'\n", "<string>", "exec") |
| 55 |
ns = {} |
| 56 |
exec(c, ns) |
| 57 |
assert ns['foo'] == 'café' |
| 58 |
assert eval(b"# coding: latin1\n'caf\xe9'\n") == 'café' |
| 59 |
|
| 60 |
def test_memoryview(): |
| 61 |
m = memoryview(b'2 + 1') |
| 62 |
co = compile(m, 'baz', 'eval') |
| 63 |
assert eval(co) == 3 |
| 64 |
assert eval(m) == 3 |
| 65 |
ns = {} |
| 66 |
exec(memoryview(b'r = 2 + 1'), ns) |
| 67 |
assert ns['r'] == 3 |
| 68 |
|
| 69 |
def test_recompile_ast(): |
| 70 |
import _ast |
| 71 |
|
| 72 |
co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST) |
| 73 |
raises(TypeError, compile, co1, '<ast>', 'eval') |
| 74 |
co2 = compile('1+1', '<string>', 'eval', _ast.PyCF_ONLY_AST) |
| 75 |
tree = compile(co2, '<ast>', 'eval') |
| 76 |
assert compile(co2, '<ast>', 'eval', _ast.PyCF_ONLY_AST) is co2 |
| 77 |
|
| 78 |
def test_leading_newlines(): |
| 79 |
src = """ |
| 80 |
def fn(): pass |
| 81 |
""" |
| 82 |
co = compile(src, 'mymod', 'exec') |
| 83 |
firstlineno = co.co_firstlineno |
| 84 |
assert firstlineno == 1 |
| 85 |
|
| 86 |
def test_null_bytes(): |
| 87 |
raises(SyntaxError, compile, '\x00', 'mymod', 'exec', 0) |
| 88 |
src = "#abc\x00def\n" |
| 89 |
raises(SyntaxError, compile, src, 'mymod', 'exec') |
| 90 |
raises(SyntaxError, compile, src, 'mymod', 'exec', 0) |
| 91 |
|
| 92 |
def test_assert_remove(): |
| 93 |
"""Test removal of the asserts with optimize=1.""" |
| 94 |
import ast |
| 95 |
|
| 96 |
code = """def f(): |
| 97 |
assert False |
| 98 |
""" |
| 99 |
tree = ast.parse(code) |
| 100 |
for to_compile in [code, tree]: |
| 101 |
compiled = compile(to_compile, "<test>", "exec", optimize=1) |
| 102 |
ns = {} |
| 103 |
exec(compiled, ns) |
| 104 |
ns['f']() |
| 105 |
|
| 106 |
def test_docstring_remove(): |
| 107 |
"""Test removal of docstrings with optimize=2.""" |
| 108 |
import ast |
| 109 |
import marshal |
| 110 |
|
| 111 |
code = """ |
| 112 |
'module_doc' |
| 113 |
|
| 114 |
def f(): |
| 115 |
'func_doc' |
| 116 |
|
| 117 |
class C: |
| 118 |
'class_doc' |
| 119 |
""" |
| 120 |
tree = ast.parse(code) |
| 121 |
for to_compile in [code, tree]: |
| 122 |
compiled = compile(to_compile, "<test>", "exec", optimize=2) |
| 123 |
|
| 124 |
ns = {} |
| 125 |
exec(compiled, ns) |
| 126 |
assert '__doc__' not in ns |
| 127 |
assert ns['f'].__doc__ is None |
| 128 |
assert ns['C'].__doc__ is None |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
marshalled = str(marshal.dumps(compiled)) |
| 133 |
assert 'module_doc' not in marshalled |
| 134 |
assert 'func_doc' not in marshalled |
| 135 |
assert 'class_doc' not in marshalled |
| 136 |
|
| 137 |
def test_build_class(): |
| 138 |
"""Test error message bad __prepare__""" |
| 139 |
|
| 140 |
class BadMeta(type): |
| 141 |
@classmethod |
| 142 |
def __prepare__(*args): |
| 143 |
return None |
| 144 |
|
| 145 |
def func(): |
| 146 |
class Foo(metaclass=BadMeta): |
| 147 |
pass |
| 148 |
|
| 149 |
excinfo = raises(TypeError, func) |
| 150 |
assert str(excinfo.value) == ( |
| 151 |
r"BadMeta.__prepare__() must return a mapping, not NoneType" |
| 152 |
) |
| 153 |
|
| 154 |
@mark.pypy_only |
| 155 |
def test_make_sure_namespace_in_class_is_moduledict(): |
| 156 |
import __pypy__ |
| 157 |
class A: |
| 158 |
assert __pypy__.strategy(locals()) == "ModuleDictStrategy" |
| 159 |
|
| 160 |
def test_compile_feature_version(): |
| 161 |
co = compile('1+2', '?', 'eval', _feature_version=-1) |
| 162 |
assert eval(co) == 3 |
| 163 |
|
| 164 |
co = compile('1+2', '?', 'eval', _feature_version=8) |
| 165 |
assert eval(co) == 3 |
| 166 |
|
| 167 |
|
| 168 |
@mark.pypy_only |
| 169 |
def test_ignore_cookie(): |
| 170 |
|
| 171 |
from _ast import PyCF_IGNORE_COOKIE |
| 172 |
src = """# coding: latin1 |
| 173 |
def fn(): return '%s' |
| 174 |
""" % chr(230) |
| 175 |
|
| 176 |
co = compile(src, "<string>", "exec", PyCF_IGNORE_COOKIE) |
| 177 |
ns = {} |
| 178 |
eval(co, ns) |
| 179 |
assert ns['fn']() == '%s' % chr(230) |
| 180 |
|
| 181 |
|
| 182 |
def test_weird_globals_builtins_eval(): |
| 183 |
class MyGlobals(dict): |
| 184 |
def __missing__(self, key): |
| 185 |
assert key != '__builtins__' |
| 186 |
return int(key.removeprefix("_number_")) |
| 187 |
|
| 188 |
code = "lambda: " + "+".join(f"_number_{i}" for i in range(100)) |
| 189 |
sum_100 = eval(code, MyGlobals()) |
| 190 |
expected = sum(range(100)) |
| 191 |
assert sum_100() == expected |
| 192 |
|
| 193 |
def test_exec_with_closure(): |
| 194 |
from types import CellType |
| 195 |
|
| 196 |
def function_without_closures(): |
| 197 |
return 3 * 5 |
| 198 |
|
| 199 |
result = 0 |
| 200 |
def make_closure_functions(): |
| 201 |
a = 2 |
| 202 |
b = 3 |
| 203 |
c = 5 |
| 204 |
def three_freevars(): |
| 205 |
nonlocal result |
| 206 |
nonlocal a |
| 207 |
nonlocal b |
| 208 |
result = a*b |
| 209 |
def four_freevars(): |
| 210 |
nonlocal result |
| 211 |
nonlocal a |
| 212 |
nonlocal b |
| 213 |
nonlocal c |
| 214 |
result = a*b*c |
| 215 |
return three_freevars, four_freevars |
| 216 |
three_freevars, four_freevars = make_closure_functions() |
| 217 |
|
| 218 |
|
| 219 |
result = 0 |
| 220 |
exec(three_freevars.__code__, |
| 221 |
three_freevars.__globals__, |
| 222 |
closure=three_freevars.__closure__) |
| 223 |
assert result == 6 |
| 224 |
|
| 225 |
|
| 226 |
result = 0 |
| 227 |
my_closure = (CellType(35), CellType(72), three_freevars.__closure__[2]) |
| 228 |
exec(three_freevars.__code__, |
| 229 |
three_freevars.__globals__, |
| 230 |
closure=my_closure) |
| 231 |
|
| 232 |
assert result == 2520 |
| 233 |
|
| 234 |
def test_exec_with_closure_errors(): |
| 235 |
from types import CellType |
| 236 |
def f(a, b): |
| 237 |
def g(): |
| 238 |
print(a, b) |
| 239 |
return a + b |
| 240 |
return g |
| 241 |
|
| 242 |
g = f(1, 2) |
| 243 |
|
| 244 |
locals = globals = {'a': 12, 'b': 23} |
| 245 |
with raises(TypeError) as info: |
| 246 |
exec(g.__code__, locals, globals) |
| 247 |
assert str(info.value) == "code object requires a closure of exactly length 2" |
| 248 |
|
| 249 |
my_closure = (CellType(35), CellType(72), CellType(100)) |
| 250 |
with raises(TypeError) as info: |
| 251 |
exec(g.__code__, locals, globals, closure=my_closure) |
| 252 |
assert str(info.value) == "code object requires a closure of exactly length 2" |
| 253 |
|
| 254 |
with raises(TypeError) as info: |
| 255 |
exec(g.__code__, locals, globals, closure=list(my_closure)) |
| 256 |
assert str(info.value) == "code object requires a closure of exactly length 2" |
| 257 |
|
| 258 |
def f(): |
| 259 |
pass |
| 260 |
with raises(TypeError) as info: |
| 261 |
exec(f.__code__, locals, globals, closure=my_closure) |
| 262 |
assert str(info.value) == "cannot use a closure with this code object" |
| 263 |
|
| 264 |
with raises(TypeError) as info: |
| 265 |
exec('print(1)', locals, globals, closure=my_closure) |
| 266 |
assert str(info.value) == "closure can only be used when source is a code object" |
| 267 |
|
| 268 |
my_closure = (35, CellType(72), CellType(100)) |
| 269 |
with raises(TypeError) as info: |
| 270 |
exec(g.__code__, locals, globals, closure=my_closure) |
| 271 |
assert str(info.value) == "code object requires a closure of exactly length 2" |
| 272 |
|
| 273 |
def test_exec_with_closure_dont_overwrite_cell_vars_from_locals(): |
| 274 |
def f(a, b): |
| 275 |
def g(): |
| 276 |
print(a, b) |
| 277 |
return a + b |
| 278 |
return g |
| 279 |
|
| 280 |
g = f(1, 2) |
| 281 |
|
| 282 |
locals = globals = {'a': 12, 'b': 23} |
| 283 |
print(exec(g.__code__, locals, globals, closure=g.__closure__)) |
| 284 |
print(g.__closure__) |
| 285 |
|
| 286 |
|
| 287 |
def test_allow_incomplete_input_decorator(): |
| 288 |
|
| 289 |
|
| 290 |
import codeop |
| 291 |
assert codeop.compile_command('@int') is None |