[Python-checkins] GH-104584: Fix test_capi.test_counter_optimizer() when run twice (#106171)

vstinner webhook-mailer at python.org
Tue Jun 27 22:41:24 EDT 2023


https://github.com/python/cpython/commit/adaacf26d3c407e311b453c71abc40672ee549df
commit: adaacf26d3c407e311b453c71abc40672ee549df
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2023-06-28T04:41:21+02:00
summary:

GH-104584: Fix test_capi.test_counter_optimizer() when run twice (#106171)

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.

files:
M Lib/test/test_capi/test_misc.py

diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py
index f2aa2a07783ee..5ee712323d219 100644
--- a/Lib/test/test_capi/test_misc.py
+++ b/Lib/test/test_capi/test_misc.py
@@ -2372,10 +2372,14 @@ def test_get_set_optimizer(self):
         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 @@ def loop():
     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):



More information about the Python-checkins mailing list