]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44616: Mark all clean up instructions at end of named exception block as artifici...
authorMark Shannon <mark@hotpy.org>
Wed, 14 Jul 2021 10:43:56 +0000 (11:43 +0100)
committerGitHub <noreply@github.com>
Wed, 14 Jul 2021 10:43:56 +0000 (11:43 +0100)
(cherry picked from commit e5862f79c16e28f1ec51d179698739a9b2d8c1d2)

Lib/test/test_sys_settrace.py
Python/compile.c

index 09d0adca2ee7a139f14abedf24ee51f80749c3ea..adbb5b56dc8391dcf6f43a337b20814bba0c53bc 100644 (file)
@@ -1077,6 +1077,29 @@ class TraceTestCase(unittest.TestCase):
              (1, 'line'),
              (1, 'return')])
 
+    def test_no_tracing_of_named_except_cleanup(self):
+
+        def func():
+            x = 0
+            try:
+                1/x
+            except ZeroDivisionError as error:
+                if x:
+                    raise
+            return "done"
+
+        self.run_and_compare(func,
+        [(0, 'call'),
+            (1, 'line'),
+            (2, 'line'),
+            (3, 'line'),
+            (3, 'exception'),
+            (4, 'line'),
+            (5, 'line'),
+            (7, 'line'),
+            (7, 'return')])
+
+
 class SkipLineEventsTraceTestCase(TraceTestCase):
     """Repeat the trace tests, but with per-line events skipped"""
 
index 7dc04923af88112fa69d040f9a001ef43217f64d..78d5fbe03ffa38843fb9036537a07b6b3e23ae84 100644 (file)
@@ -3220,10 +3220,10 @@ compiler_try_except(struct compiler *c, stmt_ty s)
             /* second # body */
             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
-            ADDOP(c, POP_BLOCK);
-            ADDOP(c, POP_EXCEPT);
             /* name = None; del name; # Mark as artificial */
             c->u->u_lineno = -1;
+            ADDOP(c, POP_BLOCK);
+            ADDOP(c, POP_EXCEPT);
             ADDOP_LOAD_CONST(c, Py_None);
             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
             compiler_nameop(c, handler->v.ExceptHandler.name, Del);
@@ -3254,7 +3254,6 @@ compiler_try_except(struct compiler *c, stmt_ty s)
                 return 0;
             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
-            /* name = None; del name; # Mark as artificial */
             c->u->u_lineno = -1;
             ADDOP(c, POP_EXCEPT);
             ADDOP_JUMP(c, JUMP_FORWARD, end);