Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f70715

Browse files
[3.13] gh-122247: Move instruction instrumentation sanity check after tracing check (GH-122251) (GH-122812)
(cherry picked from commit 57d7c3e) Co-authored-by: Tian Gao <[email protected]>
1 parent 7ee7558 commit 1f70715

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_monitoring.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,21 @@ def f(a=1, b=2):
18411841
self.assertEqual(call_data[0], (f, 1))
18421842
self.assertEqual(call_data[1], (f, sys.monitoring.MISSING))
18431843

1844+
def test_instruction_explicit_callback(self):
1845+
# gh-122247
1846+
# Calling the instruction event callback explicitly should not
1847+
# crash CPython
1848+
def callback(code, instruction_offset):
1849+
pass
1850+
1851+
sys.monitoring.use_tool_id(0, "test")
1852+
self.addCleanup(sys.monitoring.free_tool_id, 0)
1853+
sys.monitoring.register_callback(0, sys.monitoring.events.INSTRUCTION, callback)
1854+
sys.monitoring.set_events(0, sys.monitoring.events.INSTRUCTION)
1855+
callback(None, 0) # call the *same* handler while it is registered
1856+
sys.monitoring.restart_events()
1857+
sys.monitoring.set_events(0, 0)
1858+
18441859

18451860
class TestOptimizer(MonitoringTestBase, unittest.TestCase):
18461861

Python/instrumentation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,14 +1344,14 @@ int
13441344
_Py_call_instrumentation_instruction(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr)
13451345
{
13461346
PyCodeObject *code = _PyFrame_GetCode(frame);
1347-
assert(debug_check_sanity(tstate->interp, code));
13481347
int offset = (int)(instr - _PyCode_CODE(code));
13491348
_PyCoMonitoringData *instrumentation_data = code->_co_monitoring;
13501349
assert(instrumentation_data->per_instruction_opcodes);
13511350
int next_opcode = instrumentation_data->per_instruction_opcodes[offset];
13521351
if (tstate->tracing) {
13531352
return next_opcode;
13541353
}
1354+
assert(debug_check_sanity(tstate->interp, code));
13551355
PyInterpreterState *interp = tstate->interp;
13561356
uint8_t tools = instrumentation_data->per_instruction_tools != NULL ?
13571357
instrumentation_data->per_instruction_tools[offset] :

0 commit comments

Comments
 (0)