Skip to content

[3.10] bpo-44570: Fix line tracing for forward jumps to duplicated tails #27067

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

Merged
Merged
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
4 changes: 2 additions & 2 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,8 @@ def test_pdb_issue_20766():
-> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
(Pdb) continue
pdb 1: <built-in function default_int_handler>
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
-> sess.set_trace(sys._getframe())
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
-> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
(Pdb) continue
pdb 2: <built-in function default_int_handler>
"""
Expand Down
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
32 changes: 13 additions & 19 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

typedef struct {
PyCodeObject *code; // The code object for the bounds. May be NULL.
int instr_prev; // Only valid if code != NULL.
PyCodeAddressRange bounds; // Only valid if code != NULL.
CFrame cframe;
} PyTraceInfo;
Expand Down Expand Up @@ -78,8 +77,8 @@ static void call_exc_trace(Py_tracefunc, PyObject *,
PyTraceInfo *trace_info);
static int maybe_call_line_trace(Py_tracefunc, PyObject *,
PyThreadState *, PyFrameObject *,
PyTraceInfo *);
static void maybe_dtrace_line(PyFrameObject *, PyTraceInfo *);
PyTraceInfo *, int);
static void maybe_dtrace_line(PyFrameObject *, PyTraceInfo *, int);
static void dtrace_function_entry(PyFrameObject *);
static void dtrace_function_return(PyFrameObject *);

Expand Down Expand Up @@ -1781,11 +1780,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}

tracing_dispatch:
{
int instr_prev = f->f_lasti;
f->f_lasti = INSTR_OFFSET();
NEXTOPARG();

if (PyDTrace_LINE_ENABLED())
maybe_dtrace_line(f, &trace_info);
maybe_dtrace_line(f, &trace_info, instr_prev);

/* line-by-line tracing support */

Expand All @@ -1799,7 +1800,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
err = maybe_call_line_trace(tstate->c_tracefunc,
tstate->c_traceobj,
tstate, f,
&trace_info);
&trace_info, instr_prev);
/* Reload possibly changed frame fields */
JUMPTO(f->f_lasti);
stack_pointer = f->f_valuestack+f->f_stackdepth;
Expand All @@ -1810,6 +1811,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
}
NEXTOPARG();
}
}

#ifdef LLTRACE
/* Instruction tracing */
Expand Down Expand Up @@ -4502,9 +4504,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
PUSH(val);
PUSH(exc);
JUMPTO(handler);
if (trace_info.cframe.use_tracing) {
trace_info.instr_prev = INT_MAX;
}
/* Resume normal execution */
f->f_state = FRAME_EXECUTING;
goto main_loop;
Expand Down Expand Up @@ -5455,7 +5454,6 @@ initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
{
if (trace_info->code != frame->f_code) {
trace_info->code = frame->f_code;
trace_info->instr_prev = -1;
_PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
}
}
Expand Down Expand Up @@ -5507,7 +5505,7 @@ _PyEval_CallTracing(PyObject *func, PyObject *args)
static int
maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
PyThreadState *tstate, PyFrameObject *frame,
PyTraceInfo *trace_info)
PyTraceInfo *trace_info, int instr_prev)
{
int result = 0;

Expand All @@ -5516,21 +5514,18 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
then call the trace function if we're tracing source lines.
*/
initialize_trace_info(trace_info, frame);
int lastline = trace_info->bounds.ar_line;
int lastline = _PyCode_CheckLineNumber(instr_prev*2, &trace_info->bounds);
int line = _PyCode_CheckLineNumber(frame->f_lasti*2, &trace_info->bounds);
if (line != -1 && frame->f_trace_lines) {
/* Trace backward edges or first instruction of a new line */
if (frame->f_lasti < trace_info->instr_prev ||
(line != lastline && frame->f_lasti*2 == trace_info->bounds.ar_start))
{
/* Trace backward edges or if line number has changed */
if (frame->f_lasti < instr_prev || line != lastline) {
result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
}
}
/* Always emit an opcode event if we're tracing all opcodes. */
if (frame->f_trace_opcodes) {
result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
}
trace_info->instr_prev = frame->f_lasti;
return result;
}

Expand Down Expand Up @@ -6475,7 +6470,7 @@ dtrace_function_return(PyFrameObject *f)
/* DTrace equivalent of maybe_call_line_trace. */
static void
maybe_dtrace_line(PyFrameObject *frame,
PyTraceInfo *trace_info)
PyTraceInfo *trace_info, int instr_prev)
{
const char *co_filename, *co_name;

Expand All @@ -6487,7 +6482,7 @@ maybe_dtrace_line(PyFrameObject *frame,
/* If the last instruction falls at the start of a line or if
it represents a jump backwards, update the frame's line
number and call the trace function. */
if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
if (line != frame->f_lineno || frame->f_lasti < instr_prev) {
if (line != -1) {
frame->f_lineno = line;
co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
Expand All @@ -6499,7 +6494,6 @@ maybe_dtrace_line(PyFrameObject *frame,
PyDTrace_LINE(co_filename, co_name, line);
}
}
trace_info->instr_prev = frame->f_lasti;
}


Expand Down