| 1 |
"""Test the exec statement functionality. |
| 2 |
|
| 3 |
New for PyPy - Could be incorporated into CPython regression tests. |
| 4 |
""" |
| 5 |
import pytest |
| 6 |
|
| 7 |
def test_string(): |
| 8 |
g = {} |
| 9 |
l = {} |
| 10 |
exec("a = 3", g, l) |
| 11 |
assert l['a'] == 3 |
| 12 |
|
| 13 |
def test_localfill(): |
| 14 |
g = {} |
| 15 |
exec("a = 3", g) |
| 16 |
assert g['a'] == 3 |
| 17 |
|
| 18 |
def test_builtinsupply(): |
| 19 |
g = {} |
| 20 |
exec("pass", g) |
| 21 |
assert '__builtins__' in g |
| 22 |
|
| 23 |
def test_invalidglobal(): |
| 24 |
with pytest.raises(TypeError): |
| 25 |
exec('pass', 1) |
| 26 |
|
| 27 |
def test_invalidlocal(): |
| 28 |
with pytest.raises(TypeError): |
| 29 |
exec('pass', {}, 2) |
| 30 |
|
| 31 |
def test_codeobject(): |
| 32 |
co = compile("a = 3", '<string>', 'exec') |
| 33 |
g = {} |
| 34 |
l = {} |
| 35 |
exec(co, g, l) |
| 36 |
assert l['a'] == 3 |
| 37 |
|
| 38 |
def test_implicit(): |
| 39 |
a = 4 |
| 40 |
exec("a = 3") |
| 41 |
assert a == 4 |
| 42 |
|
| 43 |
def test_tuplelocals(): |
| 44 |
g = {} |
| 45 |
l = {} |
| 46 |
exec("a = 3", g, l) |
| 47 |
assert l['a'] == 3 |
| 48 |
|
| 49 |
def test_tupleglobals(): |
| 50 |
g = {} |
| 51 |
exec("a = 3", g) |
| 52 |
assert g['a'] == 3 |
| 53 |
|
| 54 |
def test_exceptionfallthrough(): |
| 55 |
with pytest.raises(TypeError): |
| 56 |
exec('raise TypeError', {}) |
| 57 |
|
| 58 |
def test_global_stmt(): |
| 59 |
g = {} |
| 60 |
l = {} |
| 61 |
co = compile("global a; a=5", '', 'exec') |
| 62 |
|
| 63 |
|
| 64 |
exec(co, g, l) |
| 65 |
assert l == {} |
| 66 |
assert g['a'] == 5 |
| 67 |
|
| 68 |
def test_specialcase_free_load(): |
| 69 |
def f(): |
| 70 |
exec('a=3') |
| 71 |
return a |
| 72 |
|
| 73 |
with raises(NameError): |
| 74 |
f() |
| 75 |
|
| 76 |
def test_specialcase_free_load2(): |
| 77 |
exec("""if 1: |
| 78 |
def f(a): |
| 79 |
exec('a=3') |
| 80 |
return a |
| 81 |
x = f(4)\n""") |
| 82 |
assert eval("x") == 4 |
| 83 |
|
| 84 |
def test_nested_names_are_not_confused(): |
| 85 |
def get_nested_class(): |
| 86 |
method_and_var = "var" |
| 87 |
class Test(object): |
| 88 |
def method_and_var(self): |
| 89 |
return "method" |
| 90 |
def test(self): |
| 91 |
return method_and_var |
| 92 |
def actual_global(self): |
| 93 |
return str("global") |
| 94 |
def str(self): |
| 95 |
return str(self) |
| 96 |
return Test() |
| 97 |
t = get_nested_class() |
| 98 |
assert t.actual_global() == "global" |
| 99 |
assert t.test() == 'var' |
| 100 |
assert t.method_and_var() == 'method' |
| 101 |
|
| 102 |
def test_exec_load_name(): |
| 103 |
d = {'x': 2} |
| 104 |
exec("""if 1: |
| 105 |
def f(): |
| 106 |
save = x |
| 107 |
exec("x=3") |
| 108 |
return x,save |
| 109 |
\n""", d) |
| 110 |
res = d['f']() |
| 111 |
assert res == (2, 2) |
| 112 |
|
| 113 |
def test_space_bug(): |
| 114 |
d = {} |
| 115 |
exec("x=5 ", d) |
| 116 |
assert d['x'] == 5 |
| 117 |
|
| 118 |
def test_synerr(): |
| 119 |
with pytest.raises(SyntaxError): |
| 120 |
exec("1 2") |
| 121 |
|
| 122 |
def test_mapping_as_locals(): |
| 123 |
class M(object): |
| 124 |
def __getitem__(self, key): |
| 125 |
return key |
| 126 |
def __setitem__(self, key, value): |
| 127 |
self.result[key] = value |
| 128 |
def setdefault(self, key, value): |
| 129 |
assert key == '__builtins__' |
| 130 |
m = M() |
| 131 |
m.result = {} |
| 132 |
exec("x=m", {}, m) |
| 133 |
assert m.result == {'x': 'm'} |
| 134 |
with raises(TypeError): |
| 135 |
exec("y=n", m) |
| 136 |
with raises(TypeError): |
| 137 |
eval("m", m) |
| 138 |
|
| 139 |
def test_filename(): |
| 140 |
with pytest.raises(SyntaxError) as excinfo: |
| 141 |
exec("'unmatched_quote") |
| 142 |
assert excinfo.value.filename == '<string>' |
| 143 |
with pytest.raises(SyntaxError) as excinfo: |
| 144 |
eval("'unmatched_quote") |
| 145 |
assert excinfo.value.filename == '<string>' |
| 146 |
|
| 147 |
def test_exec_and_name_lookups(): |
| 148 |
ns = {} |
| 149 |
exec("""def f(): |
| 150 |
exec('x=1', globals()) |
| 151 |
return x\n""", ns) |
| 152 |
|
| 153 |
f = ns['f'] |
| 154 |
assert f() == 1 |
| 155 |
|
| 156 |
def test_exec_unicode(): |
| 157 |
|
| 158 |
s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'" |
| 159 |
|
| 160 |
u = s.decode('utf-8') |
| 161 |
ns = {} |
| 162 |
exec(u, ns) |
| 163 |
x = ns['x'] |
| 164 |
assert len(x) == 6 |
| 165 |
assert ord(x[0]) == 0x0439 |
| 166 |
assert ord(x[1]) == 0x0446 |
| 167 |
assert ord(x[2]) == 0x0443 |
| 168 |
assert ord(x[3]) == 0x043a |
| 169 |
assert ord(x[4]) == 0x0435 |
| 170 |
assert ord(x[5]) == 0x043d |
| 171 |
|
| 172 |
def test_eval_unicode(): |
| 173 |
u = "'%s'" % chr(0x1234) |
| 174 |
v = eval(u) |
| 175 |
assert v == chr(0x1234) |
| 176 |
|
| 177 |
def test_compile_bytes(): |
| 178 |
s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'" |
| 179 |
c = compile(s, '<input>', 'exec') |
| 180 |
ns = {} |
| 181 |
exec(c, ns) |
| 182 |
x = ns['x'] |
| 183 |
assert len(x) == 6 |
| 184 |
assert ord(x[0]) == 0x0439 |
| 185 |
|
| 186 |
def test_issue3297(): |
| 187 |
c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec") |
| 188 |
d = {} |
| 189 |
exec(c, d) |
| 190 |
assert d['a'] == d['b'] |
| 191 |
assert len(d['a']) == len(d['b']) |
| 192 |
assert ascii(d['a']) == ascii(d['b']) |
| 193 |
|
| 194 |
def test_exec_nonlocal(): |
| 195 |
x = 0 |
| 196 |
def set_x(value): |
| 197 |
nonlocal x |
| 198 |
x = value |
| 199 |
set_x(1) |
| 200 |
assert x == 1 |
| 201 |
exec('set_x(2)') |
| 202 |
assert x == 2 |