diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index f2aa2a07783..5ee712323d2 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -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):