Skip to content

bpo-44570: Fix line tracing for forwards jumps to duplicated tails #27066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,41 @@ def func_return():
(-8, 'return'),
(1, 'return')])

def test_flow_converges_on_same_line(self):

def foo(x):
if x:
try:
1/(x - 1)
except ZeroDivisionError:
pass
return x

def func():
for i in range(2):
foo(i)

self.run_and_compare(func,
[(0, 'call'),
(1, 'line'),
(2, 'line'),
(-8, 'call'),
(-7, 'line'),
(-2, 'line'),
(-2, 'return'),
(1, 'line'),
(2, 'line'),
(-8, 'call'),
(-7, 'line'),
(-6, 'line'),
(-5, 'line'),
(-5, 'exception'),
(-4, 'line'),
(-3, 'line'),
(-2, 'line'),
(-2, 'return'),
(1, 'line'),
(1, 'return')])

class SkipLineEventsTraceTestCase(TraceTestCase):
"""Repeat the trace tests, but with per-line events skipped"""
Expand Down
25 changes: 13 additions & 12 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,16 @@ eval_frame_handle_pending(PyThreadState *tstate)
return 0;
}

static void
initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
{
PyCodeObject *code = _PyFrame_GetCode(frame);
if (trace_info->code != code) {
trace_info->code = code;
_PyCode_InitAddressRange(code, &trace_info->bounds);
}
}


/* Computed GOTOs, or
the-optimization-commonly-but-improperly-known-as-"threaded code"
Expand Down Expand Up @@ -1453,6 +1463,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
co = (PyCodeObject *)specials[FRAME_SPECIALS_CODE_OFFSET];

if (cframe.use_tracing) {
initialize_trace_info(&tstate->trace_info, f);
if (tstate->c_tracefunc != NULL) {
/* tstate->c_tracefunc, if defined, is a
function that will be called on *every* entry
Expand Down Expand Up @@ -5401,16 +5412,6 @@ call_trace_protected(Py_tracefunc func, PyObject *obj,
}
}

static void
initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
{
PyCodeObject *code = _PyFrame_GetCode(frame);
if (trace_info->code != code) {
trace_info->code = code;
_PyCode_InitAddressRange(code, &trace_info->bounds);
}
}

static int
call_trace(Py_tracefunc func, PyObject *obj,
PyThreadState *tstate, PyFrameObject *frame,
Expand Down Expand Up @@ -5468,9 +5469,9 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
int lastline = _PyCode_CheckLineNumber(instr_prev*2, &tstate->trace_info.bounds);
int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &tstate->trace_info.bounds);
if (line != -1 && frame->f_trace_lines) {
/* Trace backward edges or first instruction of a new line */
/* Trace backward edges or if line number has changed */
if (frame->f_lasti < instr_prev ||
(line != lastline && frame->f_lasti*2 == tstate->trace_info.bounds.ar_start))
(line != lastline))
{
result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
}
Expand Down