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.
This commit is contained in:
Victor Stinner 2023-06-28 04:41:21 +02:00 committed by GitHub
parent 2ac3eec103
commit adaacf26d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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):