From 398fe55bea45b94e4b27fadcfc9e02e42f54d479 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 31 Dec 2019 08:52:43 +0000 Subject: [PATCH 1/2] bpo-39114: Fix tracing of except handlers with name binding --- Lib/test/test_sys_settrace.py | 24 +++++++++++++++++++ .../2019-12-31-18-25-45.bpo-39114.WG9alt.rst | 2 ++ Python/ceval.c | 3 ++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-12-31-18-25-45.bpo-39114.WG9alt.rst diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index 0af015aa56bb4d3..4eb0a5d1fe879ba 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -481,6 +481,30 @@ def func(): [(0, 'call'), (1, 'line')]) + def test_18_except_with_name(self): + def func(): + try: + try: + raise Exception + except Exception as e: + raise + x = "Something" + y = "Something" + except Exception: + pass + + self.run_and_compare(func, + [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (3, 'exception'), + (4, 'line'), + (5, 'line'), + (8, 'line'), + (9, 'line'), + (9, 'return')]) + class SkipLineEventsTraceTestCase(TraceTestCase): """Repeat the trace tests, but with per-line events skipped""" diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-12-31-18-25-45.bpo-39114.WG9alt.rst b/Misc/NEWS.d/next/Core and Builtins/2019-12-31-18-25-45.bpo-39114.WG9alt.rst new file mode 100644 index 000000000000000..d742af9d3262ea1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-12-31-18-25-45.bpo-39114.WG9alt.rst @@ -0,0 +1,2 @@ +Fix incorrent line execution reporting in trace functions when tracing +exception handlers with name binding. Patch by Pablo Galindo. diff --git a/Python/ceval.c b/Python/ceval.c index 96ed329b0d99524..268bfe8a69de2d8 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3610,7 +3610,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) PUSH(val); PUSH(exc); JUMPTO(handler); - if (_Py_TracingPossible(ceval)) { + if (_Py_TracingPossible(ceval) && + (f->f_lasti < instr_lb || f->f_lasti >= instr_ub)) { /* Make sure that we trace line after exception */ instr_prev = INT_MAX; } From 4238bad1720b35b2fbc17c895c15ab9707240a9a Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 31 Dec 2019 20:54:52 +0000 Subject: [PATCH 2/2] Fix case with simple finally --- Lib/test/test_sys_settrace.py | 21 +++++++++++++++++++++ Python/ceval.c | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index 4eb0a5d1fe879ba..a0d1122fad83be2 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -505,6 +505,27 @@ def func(): (9, 'line'), (9, 'return')]) + def test_19_except_with_finally(self): + def func(): + try: + try: + raise Exception + finally: + y = "Something" + except Exception: + b = 23 + + self.run_and_compare(func, + [(0, 'call'), + (1, 'line'), + (2, 'line'), + (3, 'line'), + (3, 'exception'), + (5, 'line'), + (6, 'line'), + (7, 'line'), + (7, 'return')]) + class SkipLineEventsTraceTestCase(TraceTestCase): """Repeat the trace tests, but with per-line events skipped""" diff --git a/Python/ceval.c b/Python/ceval.c index 268bfe8a69de2d8..bd9454b2812ddf7 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3611,7 +3611,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag) PUSH(exc); JUMPTO(handler); if (_Py_TracingPossible(ceval) && - (f->f_lasti < instr_lb || f->f_lasti >= instr_ub)) { + ((f->f_lasti < instr_lb || f->f_lasti >= instr_ub) || + !(f->f_lasti == instr_lb || f->f_lasti < instr_prev))) { /* Make sure that we trace line after exception */ instr_prev = INT_MAX; }