summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_optimizer.py
blob: b56bf3cfd9560eadc6d09d1434b97175cb470a7b (plain)
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
74
75
import _testinternalcapi
import unittest
import types


class TestRareEventCounters(unittest.TestCase):
    def test_set_class(self):
        class A:
            pass
        class B:
            pass
        a = A()

        orig_counter = _testinternalcapi.get_rare_event_counters()["set_class"]
        a.__class__ = B
        self.assertEqual(
            orig_counter + 1,
            _testinternalcapi.get_rare_event_counters()["set_class"]
        )

    def test_set_bases(self):
        class A:
            pass
        class B:
            pass
        class C(B):
            pass

        orig_counter = _testinternalcapi.get_rare_event_counters()["set_bases"]
        C.__bases__ = (A,)
        self.assertEqual(
            orig_counter + 1,
            _testinternalcapi.get_rare_event_counters()["set_bases"]
        )

    def test_set_eval_frame_func(self):
        orig_counter = _testinternalcapi.get_rare_event_counters()["set_eval_frame_func"]
        _testinternalcapi.set_eval_frame_record([])
        self.assertEqual(
            orig_counter + 1,
            _testinternalcapi.get_rare_event_counters()["set_eval_frame_func"]
        )
        _testinternalcapi.set_eval_frame_default()

    def test_builtin_dict(self):
        orig_counter = _testinternalcapi.get_rare_event_counters()["builtin_dict"]
        if isinstance(__builtins__, types.ModuleType):
            builtins = __builtins__.__dict__
        else:
            builtins = __builtins__
        builtins["FOO"] = 42
        self.assertEqual(
            orig_counter + 1,
            _testinternalcapi.get_rare_event_counters()["builtin_dict"]
        )
        del builtins["FOO"]

    def test_func_modification(self):
        def func(x=0):
            pass

        for attribute in (
            "__code__",
            "__defaults__",
            "__kwdefaults__"
        ):
            orig_counter = _testinternalcapi.get_rare_event_counters()["func_modification"]
            setattr(func, attribute, getattr(func, attribute))
            self.assertEqual(
                orig_counter + 1,
                _testinternalcapi.get_rare_event_counters()["func_modification"]
            )

if __name__ == "__main__":
    unittest.main()