summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_optimizer.py
diff options
context:
space:
mode:
authorMichael Droettboom <mdboom@gmail.com>2024-01-25 11:10:51 (GMT)
committerGitHub <noreply@github.com>2024-01-25 11:10:51 (GMT)
commitea3cd0498c443e93be441736c804258e93d21edd (patch)
treeab975dc0fe8c933fd2c197275a7d9fc6598aa04b /Lib/test/test_optimizer.py
parentc63c6142f9146e1e977f4c824c56e8979e6aca87 (diff)
downloadcpython-ea3cd0498c443e93be441736c804258e93d21edd.zip
cpython-ea3cd0498c443e93be441736c804258e93d21edd.tar.gz
cpython-ea3cd0498c443e93be441736c804258e93d21edd.tar.bz2
gh-114312: Collect stats for unlikely events (GH-114493)
Diffstat (limited to 'Lib/test/test_optimizer.py')
-rw-r--r--Lib/test/test_optimizer.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/Lib/test/test_optimizer.py b/Lib/test/test_optimizer.py
new file mode 100644
index 0000000..b56bf3c
--- /dev/null
+++ b/Lib/test/test_optimizer.py
@@ -0,0 +1,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()