[New-bugs-announce] [issue39114] Python 3.9.0a2 changed how finally/return is traced

Ned Batchelder report at bugs.python.org
Sat Dec 21 08:40:45 EST 2019


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

The way trace function reports return-finally has changed in Python 3.9.0a2. I don't know if this change is intentional or not.

(BTW: I want to put a 3.9regression keyword on this, but it doesn't exist.)

Consider this code:

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

def trace(frame, event, arg):
    lineno = frame.f_lineno
    print("{} {}: {}".format(event[:4], lineno, linecache.getline(__file__, lineno).rstrip()))
    return trace

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

a = []
def finally_return():
    try:
        return 14
    finally:
        a.append(16)
assert finally_return() == 14
assert a == [16]
--- 8< ----------------------------------------------------


(My habit is to use line numbers in the lines themselves to help keep things straight.)

In Python 3.7 (and before), the last traces are line 14, line 16, return 16.

In Python 3.8, the last traces are line 14, line 16, line 14, return 14.

In Python 3.9a1, the traces are the same as 3.8.

In Python 3.9a2, the traces are now line 14, line 16, line 14, line 16, return 16.

This doesn't make sense to me: why does it bounce back and forth?

Full output from different versions of Python:

% /usr/local/pythonz/pythons/CPython-3.7.1/bin/python3.7 bpo.py
3.7.1 (default, Oct 20 2018, 18:25:32)
[Clang 10.0.0 (clang-1000.11.45.2)]
call 12: def finally_return():
line 13:     try:
line 14:         return 14
line 16:         a.append(16)
retu 16:         a.append(16)
% /usr/local/pythonz/pythons/CPython-3.8.1/bin/python3.8 bpo.py
3.8.1 (default, Dec 19 2019, 08:38:38)
[Clang 10.0.0 (clang-1000.10.44.4)]
call 12: def finally_return():
line 13:     try:
line 14:         return 14
line 16:         a.append(16)
line 14:         return 14
retu 14:         return 14
% /usr/local/pythonz/pythons/CPython-3.9.0a1/bin/python3.9 bpo.py
3.9.0a1 (default, Nov 20 2019, 18:52:14)
[Clang 10.0.0 (clang-1000.10.44.4)]
call 12: def finally_return():
line 13:     try:
line 14:         return 14
line 16:         a.append(16)
line 14:         return 14
retu 14:         return 14
% /usr/local/pythonz/pythons/CPython-3.9.0a2/bin/python3.9 bpo.py
3.9.0a2 (default, Dec 19 2019, 08:42:29)
[Clang 10.0.0 (clang-1000.10.44.4)]
call 12: def finally_return():
line 13:     try:
line 14:         return 14
line 16:         a.append(16)
line 14:         return 14
line 16:         a.append(16)
retu 16:         a.append(16)

----------
messages: 358771
nosy: nedbat
priority: normal
severity: normal
status: open
title: Python 3.9.0a2 changed how finally/return is traced
type: behavior
versions: Python 3.9

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


More information about the New-bugs-announce mailing list