8000 gh-109371: Fix monitoring with instruction events set (gh-109385) · python/cpython@412f5e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 412f5e8

Browse files
gh-109371: Fix monitoring with instruction events set (gh-109385)
1 parent 23f9f6f commit 412f5e8

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
@@ -664,7 +664,7 @@ instrument(PyCodeObject *code, int i)
664664
if (opcode == INSTRUMENTED_INSTRUCTION) {
665665
opcode_ptr = &code->_co_monitoring->per_instruction_opcodes[i];
666666
opcode = *opcode_ptr;
667-
CHECK(!is_instrumented(opcode));
667+
CHECK(opcode != INSTRUMENTED_INSTRUCTION && opcode != INSTRUMENTED_LINE);
668668
CHECK(opcode == _PyOpcode_Deopt[opcode]);
669669
}
670670
CHECK(opcode != 0);
@@ -1295,6 +1295,9 @@ initialize_tools(PyCodeObject *code)
12951295
if (opcode == INSTRUMENTED_LINE) {
12961296
opcode = code->_co_monitoring->lines[i].original_opcode;
12971297
}
1298+
if (opcode == INSTRUMENTED_INSTRUCTION) {
1299+
opcode = code->_co_monitoring->per_instruction_opcodes[i];
1300+
}
12981301
bool instrumented = is_instrumented(opcode);
12991302
if (instrumented) {
13001303
opcode = DE_INSTRUMENT[opcode];

0 commit comments

Comments
 (0)
0