Skip to content

Commit

Permalink
pythonGH-104584: Fix test_capi.test_counter_optimizer() when run twice
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed Jun 28, 2023
1 parent 161012f commit 24228b3
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions Lib/test/test_capi/test_misc.py
Expand Up @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit 24228b3

Please sign in to comment.