1 | n/a | # Test the most dynamic corner cases of Python's runtime semantics. |
---|
2 | n/a | |
---|
3 | n/a | import builtins |
---|
4 | n/a | import unittest |
---|
5 | n/a | |
---|
6 | n/a | from test.support import swap_item, swap_attr |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | class RebindBuiltinsTests(unittest.TestCase): |
---|
10 | n/a | |
---|
11 | n/a | """Test all the ways that we can change/shadow globals/builtins.""" |
---|
12 | n/a | |
---|
13 | n/a | def configure_func(self, func, *args): |
---|
14 | n/a | """Perform TestCase-specific configuration on a function before testing. |
---|
15 | n/a | |
---|
16 | n/a | By default, this does nothing. Example usage: spinning a function so |
---|
17 | n/a | that a JIT will optimize it. Subclasses should override this as needed. |
---|
18 | n/a | |
---|
19 | n/a | Args: |
---|
20 | n/a | func: function to configure. |
---|
21 | n/a | *args: any arguments that should be passed to func, if calling it. |
---|
22 | n/a | |
---|
23 | n/a | Returns: |
---|
24 | n/a | Nothing. Work will be performed on func in-place. |
---|
25 | n/a | """ |
---|
26 | n/a | pass |
---|
27 | n/a | |
---|
28 | n/a | def test_globals_shadow_builtins(self): |
---|
29 | n/a | # Modify globals() to shadow an entry in builtins. |
---|
30 | n/a | def foo(): |
---|
31 | n/a | return len([1, 2, 3]) |
---|
32 | n/a | self.configure_func(foo) |
---|
33 | n/a | |
---|
34 | n/a | self.assertEqual(foo(), 3) |
---|
35 | n/a | with swap_item(globals(), "len", lambda x: 7): |
---|
36 | n/a | self.assertEqual(foo(), 7) |
---|
37 | n/a | |
---|
38 | n/a | def test_modify_builtins(self): |
---|
39 | n/a | # Modify the builtins module directly. |
---|
40 | n/a | def foo(): |
---|
41 | n/a | return len([1, 2, 3]) |
---|
42 | n/a | self.configure_func(foo) |
---|
43 | n/a | |
---|
44 | n/a | self.assertEqual(foo(), 3) |
---|
45 | n/a | with swap_attr(builtins, "len", lambda x: 7): |
---|
46 | n/a | self.assertEqual(foo(), 7) |
---|
47 | n/a | |
---|
48 | n/a | def test_modify_builtins_while_generator_active(self): |
---|
49 | n/a | # Modify the builtins out from under a live generator. |
---|
50 | n/a | def foo(): |
---|
51 | n/a | x = range(3) |
---|
52 | n/a | yield len(x) |
---|
53 | n/a | yield len(x) |
---|
54 | n/a | self.configure_func(foo) |
---|
55 | n/a | |
---|
56 | n/a | g = foo() |
---|
57 | n/a | self.assertEqual(next(g), 3) |
---|
58 | n/a | with swap_attr(builtins, "len", lambda x: 7): |
---|
59 | n/a | self.assertEqual(next(g), 7) |
---|
60 | n/a | |
---|
61 | n/a | def test_modify_builtins_from_leaf_function(self): |
---|
62 | n/a | # Verify that modifications made by leaf functions percolate up the |
---|
63 | n/a | # callstack. |
---|
64 | n/a | with swap_attr(builtins, "len", len): |
---|
65 | n/a | def bar(): |
---|
66 | n/a | builtins.len = lambda x: 4 |
---|
67 | n/a | |
---|
68 | n/a | def foo(modifier): |
---|
69 | n/a | l = [] |
---|
70 | n/a | l.append(len(range(7))) |
---|
71 | n/a | modifier() |
---|
72 | n/a | l.append(len(range(7))) |
---|
73 | n/a | return l |
---|
74 | n/a | self.configure_func(foo, lambda: None) |
---|
75 | n/a | |
---|
76 | n/a | self.assertEqual(foo(bar), [7, 4]) |
---|
77 | n/a | |
---|
78 | n/a | def test_cannot_change_globals_or_builtins_with_eval(self): |
---|
79 | n/a | def foo(): |
---|
80 | n/a | return len([1, 2, 3]) |
---|
81 | n/a | self.configure_func(foo) |
---|
82 | n/a | |
---|
83 | n/a | # Note that this *doesn't* change the definition of len() seen by foo(). |
---|
84 | n/a | builtins_dict = {"len": lambda x: 7} |
---|
85 | n/a | globals_dict = {"foo": foo, "__builtins__": builtins_dict, |
---|
86 | n/a | "len": lambda x: 8} |
---|
87 | n/a | self.assertEqual(eval("foo()", globals_dict), 3) |
---|
88 | n/a | |
---|
89 | n/a | self.assertEqual(eval("foo()", {"foo": foo}), 3) |
---|
90 | n/a | |
---|
91 | n/a | def test_cannot_change_globals_or_builtins_with_exec(self): |
---|
92 | n/a | def foo(): |
---|
93 | n/a | return len([1, 2, 3]) |
---|
94 | n/a | self.configure_func(foo) |
---|
95 | n/a | |
---|
96 | n/a | globals_dict = {"foo": foo} |
---|
97 | n/a | exec("x = foo()", globals_dict) |
---|
98 | n/a | self.assertEqual(globals_dict["x"], 3) |
---|
99 | n/a | |
---|
100 | n/a | # Note that this *doesn't* change the definition of len() seen by foo(). |
---|
101 | n/a | builtins_dict = {"len": lambda x: 7} |
---|
102 | n/a | globals_dict = {"foo": foo, "__builtins__": builtins_dict, |
---|
103 | n/a | "len": lambda x: 8} |
---|
104 | n/a | |
---|
105 | n/a | exec("x = foo()", globals_dict) |
---|
106 | n/a | self.assertEqual(globals_dict["x"], 3) |
---|
107 | n/a | |
---|
108 | n/a | def test_cannot_replace_builtins_dict_while_active(self): |
---|
109 | n/a | def foo(): |
---|
110 | n/a | x = range(3) |
---|
111 | n/a | yield len(x) |
---|
112 | n/a | yield len(x) |
---|
113 | n/a | self.configure_func(foo) |
---|
114 | n/a | |
---|
115 | n/a | g = foo() |
---|
116 | n/a | self.assertEqual(next(g), 3) |
---|
117 | n/a | with swap_item(globals(), "__builtins__", {"len": lambda x: 7}): |
---|
118 | n/a | self.assertEqual(next(g), 3) |
---|
119 | n/a | |
---|
120 | n/a | def test_cannot_replace_builtins_dict_between_calls(self): |
---|
121 | n/a | def foo(): |
---|
122 | n/a | return len([1, 2, 3]) |
---|
123 | n/a | self.configure_func(foo) |
---|
124 | n/a | |
---|
125 | n/a | self.assertEqual(foo(), 3) |
---|
126 | n/a | with swap_item(globals(), "__builtins__", {"len": lambda x: 7}): |
---|
127 | n/a | self.assertEqual(foo(), 3) |
---|
128 | n/a | |
---|
129 | n/a | def test_eval_gives_lambda_custom_globals(self): |
---|
130 | n/a | globals_dict = {"len": lambda x: 7} |
---|
131 | n/a | foo = eval("lambda: len([])", globals_dict) |
---|
132 | n/a | self.configure_func(foo) |
---|
133 | n/a | |
---|
134 | n/a | self.assertEqual(foo(), 7) |
---|
135 | n/a | |
---|
136 | n/a | |
---|
137 | n/a | if __name__ == "__main__": |
---|
138 | n/a | unittest.main() |
---|