]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39114: Fix tracing of except handlers with name binding (GH-17769)
authorPablo Galindo <Pablogsal@gmail.com>
Thu, 2 Jan 2020 11:38:44 +0000 (11:38 +0000)
committerGitHub <noreply@github.com>
Thu, 2 Jan 2020 11:38:44 +0000 (11:38 +0000)
When producing the bytecode of exception handlers with name binding (like `except Exception as e`) we need to produce a try-finally block to make sure that the name is deleted after the handler is executed to prevent cycles in the stack frame objects. The bytecode associated with this try-finally block does not have source lines associated and it was causing problems when the tracing functionality was running over it.

Lib/test/test_sys_settrace.py
Misc/NEWS.d/next/Core and Builtins/2019-12-31-18-25-45.bpo-39114.WG9alt.rst [new file with mode: 0644]
Python/ceval.c

index 0af015aa56bb4d391a5c2b1881ebb2ea3d110efe..a0d1122fad83be2ced14fe0dfe89086d6bd7e98f 100644 (file)
@@ -481,6 +481,51 @@ class TraceTestCase(unittest.TestCase):
             [(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')])
+
+    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/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 (file)
index 0000000..d742af9
--- /dev/null
@@ -0,0 +1,2 @@
+Fix incorrent line execution reporting in trace functions when tracing
+exception handlers with name binding. Patch by Pablo Galindo.
index 96ed329b0d99524f4b0592bd55a9ce2ccf0ab6f0..bd9454b2812ddf7cc4d2f79647342beeeaa5ba74 100644 (file)
@@ -3610,7 +3610,9 @@ exception_unwind:
                 PUSH(val);
                 PUSH(exc);
                 JUMPTO(handler);
-                if (_Py_TracingPossible(ceval)) {
+                if (_Py_TracingPossible(ceval) &&
+                    ((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;
                 }