Skip to content

Commit 9412a87

Browse files
[3.12] gh-109371: Fix monitoring with instruction events set (gh-109385) (#109542)
gh-109371: Fix monitoring with instruction events set (gh-109385) (cherry picked from commit 412f5e8) Co-authored-by: Tian Gao <[email protected]>
1 parent 2401b98 commit 9412a87

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

Lib/test/test_monitoring.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,22 @@ def test_two_with_disable(self):
501501
self.assertEqual(sys.monitoring._all_events(), {})
502502
sys.monitoring.restart_events()
503503

504+
def test_with_instruction_event(self):
505+
"""Test that the second tool can set events with instruction events set by the first tool."""
506+
def f():
507+
pass
508+
code = f.__code__
509+
510+
try:
511+
self.assertEqual(sys.monitoring._all_events(), {})
512+
sys.monitoring.set_local_events(TEST_TOOL, code, E.INSTRUCTION | E.LINE)
513+
sys.monitoring.set_local_events(TEST_TOOL2, code, E.LINE)
514+
finally:
515+
sys.monitoring.set_events(TEST_TOOL, 0)
516+
sys.monitoring.set_events(TEST_TOOL2, 0)
517+
self.assertEqual(sys.monitoring._all_events(), {})
518+
519+
504520
class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):
505521

506522
def test_lines_single(self):

Lib/test/test_sys_setprofile.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ def __del__(self):
439439
sys.setprofile(foo)
440440
self.assertEqual(sys.getprofile(), bar)
441441

442-
443442
def test_same_object(self):
444443
def foo(*args):
445444
...
@@ -448,6 +447,18 @@ def foo(*args):
448447
del foo
449448
sys.setprofile(sys.getprofile())
450449

450+
def test_profile_after_trace_opcodes(self):
451+
def f():
452+
...
453+
454+
sys._getframe().f_trace_opcodes = True
455+
prev_trace = sys.gettrace()
456+
sys.settrace(lambda *args: None)
457+
f()
458+
sys.settrace(prev_trace)
459+
sys.setprofile(lambda *args: None)
460+
f()
461+
451462

452463
if __name__ == "__main__":
453464
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deopted instructions correctly for tool initialization and modified the incorrect assertion in instrumentation, when a previous tool already sets INSTRUCTION events

Python/instrumentation.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ instrument(PyCodeObject *code, int i)
647647
if (opcode == INSTRUMENTED_INSTRUCTION) {
648648
opcode_ptr = &code->_co_monitoring->per_instruction_opcodes[i];
649649
opcode = *opcode_ptr;
650-
CHECK(!is_instrumented(opcode));
650+
CHECK(opcode != INSTRUMENTED_INSTRUCTION && opcode != INSTRUMENTED_LINE);
651651
CHECK(opcode == _PyOpcode_Deopt[opcode]);
652652
}
653653
CHECK(opcode != 0);
@@ -1252,6 +1252,9 @@ initialize_tools(PyCodeObject *code)
12521252
if (opcode == INSTRUMENTED_LINE) {
12531253
opcode = code->_co_monitoring->lines[i].original_opcode;
12541254
}
1255+
if (opcode == INSTRUMENTED_INSTRUCTION) {
1256+
opcode = code->_co_monitoring->per_instruction_opcodes[i];
1257+
}
12551258
bool instrumented = is_instrumented(opcode);
12561259
if (instrumented) {
12571260
opcode = DE_INSTRUMENT[opcode];

0 commit comments

Comments
 (0)