ยปCore Development>Code coverage>Lib/test/test_dynamic.py

Python code coverage for Lib/test/test_dynamic.py

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