Skip to content

Commit 3eecdeb

Browse files
committed
Fix INSTRUMENT_FOR_ITER for list/tuple
1 parent f49a07b commit 3eecdeb

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Lib/test/test_monitoring.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,21 @@ def callback(code, instruction_offset):
21572157
sys.monitoring.restart_events()
21582158
sys.monitoring.set_events(0, 0)
21592159

2160+
def test_134879(self):
2161+
# gh-134789
2162+
# Specialized FOR_ITER not incrementing index
2163+
def foo():
2164+
t = 0
2165+
for i in [1,2,3,4]:
2166+
t += i
2167+
self.assertEqual(t, 10)
2168+
2169+
sys.monitoring.use_tool_id(0, "test")
2170+
self.addCleanup(sys.monitoring.free_tool_id, 0)
2171+
sys.monitoring.set_local_events(0, foo.__code__, E.BRANCH_LEFT | E.BRANCH_RIGHT)
2172+
foo()
2173+
sys.monitoring.set_local_events(0, foo.__code__, 0)
2174+
21602175

21612176
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
21622177

Python/bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,7 +3130,6 @@ dummy_func(
31303130
if (PyStackRef_IsTaggedInt(null_or_index)) {
31313131
next = _PyForIter_NextWithIndex(iter_o, null_or_index);
31323132
if (PyStackRef_IsNull(next)) {
3133-
null_or_index = PyStackRef_TagInt(-1);
31343133
JUMPBY(oparg + 1);
31353134
DISPATCH();
31363135
}
@@ -3191,6 +3190,7 @@ dummy_func(
31913190
JUMPBY(oparg + 1);
31923191
DISPATCH();
31933192
}
3193+
null_or_index = PyStackRef_IncrementTaggedIntNoOverflow(null_or_index);
31943194
INSTRUMENTED_JUMP(this_instr, next_instr, PY_MONITORING_EVENT_BRANCH_LEFT);
31953195
}
31963196
else {

Python/generated_cases.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)