File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -1863,6 +1863,21 @@ def f(a=1, b=2):
1863
1863
self .assertEqual (call_data [0 ], (f , 1 ))
1864
1864
self .assertEqual (call_data [1 ], (f , sys .monitoring .MISSING ))
1865
1865
1866
+ def test_instruction_explicit_callback (self ):
1867
+ # gh-122247
1868
+ # Calling the instruction event callback explicitly should not
1869
+ # crash CPython
1870
+ def callback (code , instruction_offset ):
1871
+ pass
1872
+
1873
+ sys .monitoring .use_tool_id (0 , "test" )
1874
+ self .addCleanup (sys .monitoring .free_tool_id , 0 )
1875
+ sys .monitoring .register_callback (0 , sys .monitoring .events .INSTRUCTION , callback )
1876
+ sys .monitoring .set_events (0 , sys .monitoring .events .INSTRUCTION )
1877
+ callback (None , 0 ) # call the *same* handler while it is registered
1878
+ sys .monitoring .restart_events ()
1879
+ sys .monitoring .set_events (0 , 0 )
1880
+
1866
1881
1867
1882
class TestOptimizer (MonitoringTestBase , unittest .TestCase ):
1868
1883
Original file line number Diff line number Diff line change @@ -1344,14 +1344,14 @@ int
1344
1344
_Py_call_instrumentation_instruction (PyThreadState * tstate , _PyInterpreterFrame * frame , _Py_CODEUNIT * instr )
1345
1345
{
1346
1346
PyCodeObject * code = _PyFrame_GetCode (frame );
1347
- assert (debug_check_sanity (tstate -> interp , code ));
1348
1347
int offset = (int )(instr - _PyCode_CODE (code ));
1349
1348
_PyCoMonitoringData * instrumentation_data = code -> _co_monitoring ;
1350
1349
assert (instrumentation_data -> per_instruction_opcodes );
1351
1350
int next_opcode = instrumentation_data -> per_instruction_opcodes [offset ];
1352
1351
if (tstate -> tracing ) {
1353
1352
return next_opcode ;
1354
1353
}
1354
+ assert (debug_check_sanity (tstate -> interp , code ));
1355
1355
PyInterpreterState * interp = tstate -> interp ;
1356
1356
uint8_t tools = instrumentation_data -> per_instruction_tools != NULL ?
1357
1357
instrumentation_data -> per_instruction_tools [offset ] :
You can’t perform that action at this time.
0 commit comments