[issue44616] Incorrect tracing for "except" with variable

Ned Batchelder report at bugs.python.org
Mon Jul 12 18:51:39 EDT 2021


New submission from Ned Batchelder <ned at nedbatchelder.com>:

This construct isn't traced properly:

    except ExceptionName as var:
        if something:
            raise

Here's a reproducer:

-- 8< ---------------------------------
import linecache, sys

def trace(frame, event, arg):
    # The weird globals here is to avoid a NameError on shutdown...
    if frame.f_code.co_filename == globals().get("__file__"):
        lineno = frame.f_lineno
        print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
    return trace

def f(x):
    try:
        1/0
    except ZeroDivisionError as error:
        if x:
            raise
    return 12

print(sys.version)
sys.settrace(trace)

for x in [0, 1]:
    try:
        print(f(x))
    except:
        print("oops")
-----------------------------------

When run with 3.10.0b4, it produces this output:

  3.10.0b4 (default, Jul 11 2021, 13:51:53) [Clang 12.0.0 (clang-1200.0.32.29)]
  call 10: def f(x):
  line 11:     try:
  line 12:         1/0
  exce 12:         1/0
  line 13:     except ZeroDivisionError as error:
  line 14:         if x:
* line 15:             raise
  line 16:     return 12
  retu 16:     return 12
  12
  call 10: def f(x):
  line 11:     try:
  line 12:         1/0
  exce 12:         1/0
  line 13:     except ZeroDivisionError as error:
  line 14:         if x:
  line 15:             raise
  retu 15:             raise
  oops

The starred line claims that raise is being run, but it is not run at that point.

The variable on the except clause is important.  If you change that line to "except ZeroDivisionError:", then the output is correct:

  3.10.0b4 (default, Jul 11 2021, 13:51:53) [Clang 12.0.0 (clang-1200.0.32.29)]
  call 10: def f(x):
  line 11:     try:
  line 12:         1/0
  exce 12:         1/0
  line 13:     except ZeroDivisionError:
  line 14:         if x:
  line 16:     return 12
  retu 16:     return 12
  12
  call 10: def f(x):
  line 11:     try:
  line 12:         1/0
  exce 12:         1/0
  line 13:     except ZeroDivisionError:
  line 14:         if x:
  line 15:             raise
  retu 15:             raise
  oops

----------
components: Interpreter Core
keywords: 3.10regression
messages: 397365
nosy: Mark.Shannon, nedbat
priority: normal
severity: normal
status: open
title: Incorrect tracing for "except" with variable
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44616>
_______________________________________


More information about the Python-bugs-list mailing list