Skip to content

Commit 32d80de

Browse files
GH-96864: Check for error between line and opcode events (GH-96880)
(cherry picked from commit c10e33a) Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
1 parent fc1cbe7 commit 32d80de

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/test/test_sys_settrace.py

+14
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,20 @@ def g(frame, event, arg):
17211721
finally:
17221722
sys.settrace(existing)
17231723

1724+
def test_line_event_raises_before_opcode_event(self):
1725+
exception = ValueError("BOOM!")
1726+
def trace(frame, event, arg):
1727+
if event == "line":
1728+
raise exception
1729+
frame.f_trace_opcodes = True
1730+
return trace
1731+
def f():
1732+
pass
1733+
with self.assertRaises(ValueError) as caught:
1734+
sys.settrace(trace)
1735+
f()
1736+
self.assertIs(caught.exception, exception)
1737+
17241738

17251739
# 'Jump' tests: assigning to frame.f_lineno within a trace function
17261740
# moves the execution position - it's how debuggers implement a Jump
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a possible assertion failure, fatal error, or :exc:`SystemError` if a
2+
line tracing event raises an exception while opcode tracing is enabled.

Python/ceval.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6933,7 +6933,7 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
69336933
}
69346934
}
69356935
/* Always emit an opcode event if we're tracing all opcodes. */
6936-
if (f->f_trace_opcodes) {
6936+
if (f->f_trace_opcodes && result == 0) {
69376937
result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
69386938
}
69396939
return result;

0 commit comments

Comments
 (0)