@@ -1192,6 +1192,72 @@ def __call__(self, code, offset):
11921192class TestLineAndInstructionEvents (CheckEvents ):
11931193 maxDiff = None
11941194
1195+ def test_disable_line_keeps_instruction_events (self ):
1196+ for local in (False , True ):
1197+ for line_tool in (TEST_TOOL , TEST_TOOL2 ):
1198+ with self .subTest (local = local , line_tool = line_tool ):
1199+ self .check_disable_line_keeps_instruction_events (
1200+ local , line_tool )
1201+
1202+ def check_disable_line_keeps_instruction_events (self , local , line_tool ):
1203+ def func (x ):
1204+ a = x + 1
1205+ b = (
1206+ a * 2
1207+ )
1208+ return b
1209+
1210+ code = func .__code__
1211+ instructions = []
1212+ lines = []
1213+ disable = False
1214+
1215+ def instruction (code_arg , offset ):
1216+ if code_arg is code :
1217+ instructions .append (offset )
1218+
1219+ def line (code_arg , lineno ):
1220+ if code_arg is code :
1221+ lines .append (lineno )
1222+ if disable :
1223+ return sys .monitoring .DISABLE
1224+
1225+ sys .monitoring .register_callback (TEST_TOOL , E .INSTRUCTION , instruction )
1226+ sys .monitoring .register_callback (line_tool , E .LINE , line )
1227+ events = {TEST_TOOL : E .INSTRUCTION }
1228+ events [line_tool ] = events .get (line_tool , 0 ) | E .LINE
1229+ try :
1230+ for tool , mask in events .items ():
1231+ if local :
1232+ sys .monitoring .set_local_events (tool , code , mask )
1233+ else :
1234+ sys .monitoring .set_events (tool , mask )
1235+ func (1 )
1236+ expected_instructions = instructions [:]
1237+ expected_lines = lines [:]
1238+ self .assertEqual (expected_instructions , [
1239+ inst .offset for inst in dis .get_instructions (func )
1240+ if inst .opname != "RESUME"
1241+ ])
1242+ self .assertTrue (expected_lines )
1243+ disable = True
1244+ for restart in (False , True ):
1245+ if restart :
1246+ sys .monitoring .restart_events ()
1247+ for call in range (2 ):
1248+ instructions .clear ()
1249+ lines .clear ()
1250+ func (1 )
1251+ self .assertEqual (instructions , expected_instructions )
1252+ self .assertEqual (lines , expected_lines if call == 0 else [])
1253+ finally :
1254+ for tool in events :
1255+ sys .monitoring .set_local_events (tool , code , 0 )
1256+ sys .monitoring .set_events (tool , 0 )
1257+ sys .monitoring .register_callback (TEST_TOOL , E .INSTRUCTION , None )
1258+ sys .monitoring .register_callback (line_tool , E .LINE , None )
1259+ sys .monitoring .restart_events ()
1260+
11951261 def test_simple (self ):
11961262
11971263 def func1 ():
0 commit comments