]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-104584: Fix test_capi.test_counter_optimizer() when run twice (#106171)
authorVictor Stinner <vstinner@python.org>
Wed, 28 Jun 2023 02:41:21 +0000 (04:41 +0200)
committerGitHub <noreply@github.com>
Wed, 28 Jun 2023 02:41:21 +0000 (04:41 +0200)
test_counter_optimizer() and test_long_loop() of test_capi now create
a new function at each call. Otherwise, the optimizer counters are
not the expected values when the test is run more than once.

Lib/test/test_capi/test_misc.py

index f2aa2a07783eec254d99c6f3a4a64b059e5d7eed..5ee712323d219a280e1917f52432b0547b991b58 100644 (file)
@@ -2372,10 +2372,14 @@ class TestOptimizerAPI(unittest.TestCase):
         self.assertEqual(_testinternalcapi.get_optimizer(), None)
 
     def test_counter_optimizer(self):
-
-        def loop():
-            for _ in range(1000):
-                pass
+        # Generate a new function at each call
+        ns = {}
+        exec(textwrap.dedent("""
+            def loop():
+                for _ in range(1000):
+                    pass
+        """), ns, ns)
+        loop = ns['loop']
 
         for repeat in range(5):
             opt = _testinternalcapi.get_counter_optimizer()
@@ -2388,18 +2392,23 @@ class TestOptimizerAPI(unittest.TestCase):
     def test_long_loop(self):
         "Check that we aren't confused by EXTENDED_ARG"
 
-        def nop():
-            pass
+        # Generate a new function at each call
+        ns = {}
+        exec(textwrap.dedent("""
+            def nop():
+                pass
 
-        def long_loop():
-            for _ in range(10):
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
-                nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+            def long_loop():
+                for _ in range(10):
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+                    nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
+        """), ns, ns)
+        long_loop = ns['long_loop']
 
         opt = _testinternalcapi.get_counter_optimizer()
         with self.temporary_optimizer(opt):