Skip to content

Commit f7e198c

Browse files
committed
gh-158121: Clarify monitoring regression tests and opcode restoration
Address review feedback by documenting the per-thread call records and multiline assignment, parameterizing both monitoring tool IDs, and placing the restoration comment before MODIFY_BYTECODE. Validation: Windows x64 Debug test_monitoring (99 tests); free-threaded Debug test_monitoring and test_free_threading.test_monitoring (113 tests); Ruff, patchcheck, and git diff --check.
1 parent a8142bd commit f7e198c

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎Lib/test/test_free_threading/test_monitoring.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ def func(x):
261261
code = func.__code__
262262
expected = [instr.offset for instr in dis.get_instructions(func)
263263
if instr.opname != "RESUME"]
264+
# Two threads, each with two lists of instruction offsets (one per call).
265+
# Check that disabling LINE preserves INSTRUCTION events in every
266+
# thread-local bytecode copy, including on subsequent calls.
264267
records = [[[], []] for _ in range(2)]
265268
local = threading.local()
266269
ready = Barrier(len(records) + 1)

‎Lib/test/test_monitoring.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,14 +1194,22 @@ class TestLineAndInstructionEvents(CheckEvents):
11941194

11951195
def test_disable_line_keeps_instruction_events(self):
11961196
for local in (False, True):
1197-
for line_tool in (TEST_TOOL, TEST_TOOL2):
1198-
with self.subTest(local=local, line_tool=line_tool):
1197+
# Use the same tool or different tools for LINE and INSTRUCTION.
1198+
for line_tool, instr_tool in (
1199+
(TEST_TOOL, TEST_TOOL),
1200+
(TEST_TOOL2, TEST_TOOL),
1201+
):
1202+
with self.subTest(local=local, line_tool=line_tool,
1203+
instr_tool=instr_tool):
11991204
self.check_disable_line_keeps_instruction_events(
1200-
local, line_tool)
1205+
local, line_tool, instr_tool)
12011206

1202-
def check_disable_line_keeps_instruction_events(self, local, line_tool):
1207+
def check_disable_line_keeps_instruction_events(self, local, line_tool,
1208+
instr_tool):
12031209
def func(x):
12041210
a = x + 1
1211+
# Split the assignment so line events also move backwards from
1212+
# the expression's line to the assignment's line.
12051213
b = (
12061214
a * 2
12071215
)
@@ -1222,9 +1230,9 @@ def line(code_arg, lineno):
12221230
if disable:
12231231
return sys.monitoring.DISABLE
12241232

1225-
sys.monitoring.register_callback(TEST_TOOL, E.INSTRUCTION, instruction)
1233+
sys.monitoring.register_callback(instr_tool, E.INSTRUCTION, instruction)
12261234
sys.monitoring.register_callback(line_tool, E.LINE, line)
1227-
events = {TEST_TOOL: E.INSTRUCTION}
1235+
events = {instr_tool: E.INSTRUCTION}
12281236
events[line_tool] = events.get(line_tool, 0) | E.LINE
12291237
try:
12301238
for tool, mask in events.items():
@@ -1254,7 +1262,7 @@ def line(code_arg, lineno):
12541262
for tool in events:
12551263
sys.monitoring.set_local_events(tool, code, 0)
12561264
sys.monitoring.set_events(tool, 0)
1257-
sys.monitoring.register_callback(TEST_TOOL, E.INSTRUCTION, None)
1265+
sys.monitoring.register_callback(instr_tool, E.INSTRUCTION, None)
12581266
sys.monitoring.register_callback(line_tool, E.LINE, None)
12591267
sys.monitoring.restart_events()
12601268

‎Python/instrumentation.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,9 @@ remove_line_tools(PyCodeObject * code, int offset, int tools)
879879
should_de_instrument = ((single_tool & tools) == single_tool);
880880
}
881881
if (should_de_instrument) {
882-
MODIFY_BYTECODE(code, de_instrument_line, monitoring, offset);
883882
/* Restore all thread-local bytecodes before updating the shared
884883
* original opcode. */
884+
MODIFY_BYTECODE(code, de_instrument_line, monitoring, offset);
885885
if (_PyCode_GetOriginalOpcode(monitoring->lines, offset) ==
886886
INSTRUMENTED_INSTRUCTION)
887887
{

0 commit comments

Comments
 (0)