Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
GH-104584: Fix test_capi.test_counter_optimizer() when run twice
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
commit 24228b321f27a73946c4739e1bb1c438c7e81be8
39 changes: 24 additions & 15 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
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