https://github.com/python/cpython/commit/ce6a6371a23dc57ed4257eb102ebfb2827477abf
commit: ce6a6371a23dc57ed4257eb102ebfb2827477abf
branch: main
author: Mark Shannon <[email protected]>
committer: gpshead <[email protected]>
date: 2025-05-30T07:11:42-07:00
summary:
GH-134879: Fix `INSTRUMENT_FOR_ITER` for list/tuple (#134897)
Fix INSTRUMENT_FOR_ITER for list/tuple
files:
M Lib/test/test_monitoring.py
M Python/bytecodes.c
M Python/generated_cases.c.h
diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index 263e4e6f394155..a932ac80117d27 100644
--- a/Lib/test/test_monitoring.py
+++ b/Lib/test/test_monitoring.py
@@ -2157,6 +2157,21 @@ def callback(code, instruction_offset):
sys.monitoring.restart_events()
sys.monitoring.set_events(0, 0)
+ def test_134879(self):
+ # gh-134789
+ # Specialized FOR_ITER not incrementing index
+ def foo():
+ t = 0
+ for i in [1,2,3,4]:
+ t += i
+ self.assertEqual(t, 10)
+
+ sys.monitoring.use_tool_id(0, "test")
+ self.addCleanup(sys.monitoring.free_tool_id, 0)
+ sys.monitoring.set_local_events(0, foo.__code__, E.BRANCH_LEFT |
E.BRANCH_RIGHT)
+ foo()
+ sys.monitoring.set_local_events(0, foo.__code__, 0)
+
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index a1f8d3605283e3..f02e32fd1d312a 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -3130,7 +3130,6 @@ dummy_func(
if (PyStackRef_IsTaggedInt(null_or_index)) {
next = _PyForIter_NextWithIndex(iter_o, null_or_index);
if (PyStackRef_IsNull(next)) {
- null_or_index = PyStackRef_TagInt(-1);
JUMPBY(oparg + 1);
DISPATCH();
}
@@ -3191,6 +3190,7 @@ dummy_func(
JUMPBY(oparg + 1);
DISPATCH();
}
+ null_or_index =
PyStackRef_IncrementTaggedIntNoOverflow(null_or_index);
INSTRUMENTED_JUMP(this_instr, next_instr,
PY_MONITORING_EVENT_BRANCH_LEFT);
}
else {
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 5be2671700aee7..e3cd3b71a1de08 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -5759,9 +5759,7 @@
next = _PyForIter_NextWithIndex(iter_o, null_or_index);
stack_pointer = _PyFrame_GetStackPointer(frame);
if (PyStackRef_IsNull(next)) {
- null_or_index = PyStackRef_TagInt(-1);
JUMPBY(oparg + 1);
- stack_pointer[-1] = null_or_index;
DISPATCH();
}
null_or_index =
PyStackRef_IncrementTaggedIntNoOverflow(null_or_index);
@@ -7070,6 +7068,7 @@
JUMPBY(oparg + 1);
DISPATCH();
}
+ null_or_index =
PyStackRef_IncrementTaggedIntNoOverflow(null_or_index);
INSTRUMENTED_JUMP(this_instr, next_instr,
PY_MONITORING_EVENT_BRANCH_LEFT);
}
else {
@@ -7099,6 +7098,7 @@
DISPATCH();
}
}
+ stack_pointer[-1] = null_or_index;
stack_pointer[0] = next;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]