Skip to content

Commit adaacf2

Browse files
authored
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.
1 parent 2ac3eec commit adaacf2

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Lib/test/test_capi/test_misc.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -2372,10 +2372,14 @@ def test_get_set_optimizer(self):
23722372
self.assertEqual(_testinternalcapi.get_optimizer(), None)
23732373

23742374
def test_counter_optimizer(self):
2375-
2376-
def loop():
2377-
for _ in range(1000):
2378-
pass
2375+
# Generate a new function at each call
2376+
ns = {}
2377+
exec(textwrap.dedent("""
2378+
def loop():
2379+
for _ in range(1000):
2380+
pass
2381+
"""), ns, ns)
2382+
loop = ns['loop']
23792383

23802384
for repeat in range(5):
23812385
opt = _testinternalcapi.get_counter_optimizer()
@@ -2388,18 +2392,23 @@ def loop():
23882392
def test_long_loop(self):
23892393
"Check that we aren't confused by EXTENDED_ARG"
23902394

2391-
def nop():
2392-
pass
2395+
# Generate a new function at each call
2396+
ns = {}
2397+
exec(textwrap.dedent("""
2398+
def nop():
2399+
pass
23932400
2394-
def long_loop():
2395-
for _ in range(10):
2396-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2397-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2398-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2399-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2400-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2401-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2402-
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2401+
def long_loop():
2402+
for _ in range(10):
2403+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2404+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2405+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2406+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2407+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2408+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2409+
nop(); nop(); nop(); nop(); nop(); nop(); nop(); nop();
2410+
"""), ns, ns)
2411+
long_loop = ns['long_loop']
24032412

24042413
opt = _testinternalcapi.get_counter_optimizer()
24052414
with self.temporary_optimizer(opt):

0 commit comments

Comments
 (0)