Skip to content

gh-109371: Fix monitoring with instruction events set #109385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,22 @@ def test_two_with_disable(self):
self.assertEqual(sys.monitoring._all_events(), {})
sys.monitoring.restart_events()

def test_with_instruction_event(self):
"""Test that the second tool can set events with instruction events set by the first tool."""
def f():
pass
code = f.__code__

try:
self.assertEqual(sys.monitoring._all_events(), {})
sys.monitoring.set_local_events(TEST_TOOL, code, E.INSTRUCTION | E.LINE)
sys.monitoring.set_local_events(TEST_TOOL2, code, E.LINE)
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.set_events(TEST_TOOL2, 0)
self.assertEqual(sys.monitoring._all_events(), {})


class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):

def test_lines_single(self):
Expand Down
13 changes: 12 additions & 1 deletion Lib/test/test_sys_setprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ def __del__(self):
sys.setprofile(foo)
self.assertEqual(sys.getprofile(), bar)


def test_same_object(self):
def foo(*args):
...
Expand All @@ -448,6 +447,18 @@ def foo(*args):
del foo
sys.setprofile(sys.getprofile())

def test_profile_after_trace_opcodes(self):
def f():
...

sys._getframe().f_trace_opcodes = True
prev_trace = sys.gettrace()
sys.settrace(lambda *args: None)
f()
sys.settrace(prev_trace)
sys.setprofile(lambda *args: None)
f()


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deopted instructions correctly for tool initialization and modified the incorrect assertion in instrumentation, when a previous tool already sets INSTRUCTION events
5 changes: 4 additions & 1 deletion Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ instrument(PyCodeObject *code, int i)
if (opcode == INSTRUMENTED_INSTRUCTION) {
opcode_ptr = &code->_co_monitoring->per_instruction_opcodes[i];
opcode = *opcode_ptr;
CHECK(!is_instrumented(opcode));
CHECK(opcode != INSTRUMENTED_INSTRUCTION && opcode != INSTRUMENTED_LINE);
CHECK(opcode == _PyOpcode_Deopt[opcode]);
}
CHECK(opcode != 0);
Expand Down Expand Up @@ -1295,6 +1295,9 @@ initialize_tools(PyCodeObject *code)
if (opcode == INSTRUMENTED_LINE) {
opcode = code->_co_monitoring->lines[i].original_opcode;
}
if (opcode == INSTRUMENTED_INSTRUCTION) {
opcode = code->_co_monitoring->per_instruction_opcodes[i];
}
bool instrumented = is_instrumented(opcode);
if (instrumented) {
opcode = DE_INSTRUMENT[opcode];
Expand Down