forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_exec.py
More file actions
73 lines (53 loc) · 1.43 KB
/
builtin_exec.py
File metadata and controls
73 lines (53 loc) · 1.43 KB
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
exec("def square(x):\n return x * x\n")
assert 16 == square(4) # noqa: F821
d = {}
exec("def square(x):\n return x * x\n", {}, d)
assert 16 == d["square"](4)
exec("assert 2 == x", {}, {"x": 2})
exec("assert 2 == x", {"x": 2}, {})
exec("assert 4 == x", {"x": 2}, {"x": 4})
exec("assert max(1, 2) == 2", {}, {})
exec("assert max(1, 5, square(5)) == 25", None)
# Local environment shouldn't replace global environment:
exec("assert max(1, 5, square(5)) == 25", None, {})
# Closures aren't available if local scope is replaced:
def g():
seven = "seven"
def f():
try:
exec("seven", None, {})
except NameError:
pass
else:
raise NameError("seven shouldn't be in scope")
f()
g()
try:
exec("", 1)
except TypeError:
pass
else:
raise TypeError("exec should fail unless globals is a dict or None")
g = globals()
g["x"] = 2
exec("x += 2")
assert x == 4 # noqa: F821
assert g["x"] == x # noqa: F821
exec("del x")
assert "x" not in g
assert "g" in globals()
assert "g" in locals()
exec("assert 'g' in globals()")
exec("assert 'g' in locals()")
exec("assert 'g' not in globals()", {})
exec("assert 'g' not in locals()", {})
del g
def f():
g = 1
assert "g" not in globals()
assert "g" in locals()
exec("assert 'g' not in globals()")
exec("assert 'g' in locals()")
exec("assert 'g' not in globals()", {})
exec("assert 'g' not in locals()", {})
f()