Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.12 tracing regression: a conditional in a finally block will revisit the condition before exiting the block. #105658

Closed
nedbat opened this issue Jun 11, 2023 · 4 comments · Fixed by #109384
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@nedbat
Copy link
Member

nedbat commented Jun 11, 2023

Python 3.12 introduced a change in tracing behavior. Now a conditional in a finally block will revisit the condition before exiting the block.

Here is test.py:

import sys
print(sys.version)

def example(raise_exc: bool, enter_finally_cond: bool):
    try:
        if raise_exc:
            print("raising exception")
            raise Exception
        print("not raising exception")
    finally:
        if enter_finally_cond:
            print("in finally conditional")

    print("after finally")

try:
    example(raise_exc=True, enter_finally_cond=True)
except:
    pass

When traced under 3.11:

% python3.11 -m trace --trace test.py
 --- modulename: test, funcname: <module>
test.py(1): import sys
test.py(2): print(sys.version)
3.11.4 (main, Jun  7 2023, 08:42:37) [Clang 14.0.3 (clang-1403.0.22.14.1)]
test.py(4): def example(raise_exc: bool, enter_finally_cond: bool):
test.py(16): try:
test.py(17):     example(raise_exc=True, enter_finally_cond=True)
 --- modulename: test, funcname: example
test.py(5):     try:
test.py(6):         if raise_exc:
test.py(7):             print("raising exception")
raising exception
test.py(8):             raise Exception
test.py(11):         if enter_finally_cond:
test.py(12):             print("in finally conditional")
in finally conditional
test.py(18): except:
test.py(19):     pass

When traced under 3.12:

% python3.12 -m trace --trace test.py
 --- modulename: test, funcname: <module>
test.py(1): import sys
test.py(2): print(sys.version)
3.12.0b2 (main, Jun  7 2023, 08:47:18) [Clang 14.0.3 (clang-1403.0.22.14.1)]
test.py(4): def example(raise_exc: bool, enter_finally_cond: bool):
test.py(16): try:
test.py(17):     example(raise_exc=True, enter_finally_cond=True)
 --- modulename: test, funcname: example
test.py(5):     try:
test.py(6):         if raise_exc:
test.py(7):             print("raising exception")
raising exception
test.py(8):             raise Exception
test.py(11):         if enter_finally_cond:
test.py(12):             print("in finally conditional")
in finally conditional
test.py(11):         if enter_finally_cond:             # <******
test.py(18): except:
test.py(19):     pass

The extra line is marked with <******. There's no reason for the if statement to be traced again.

Linked PRs

@Eclips4
Copy link
Member

Eclips4 commented Jun 11, 2023

Confirmed on current main branch

@Eclips4
Copy link
Member

Eclips4 commented Jun 11, 2023

Bisected to #98001, @nedbat can you confirm that?

@Eclips4
Copy link
Member

Eclips4 commented Jun 11, 2023

Why change of location is needed here?

loc = location_of_last_executing_statement(s->v.Try.finalbody);

I've deleted it(same for compiler_try_star_finally) and seems this fixes the issue, but I don't sure that's correct solution.

@iritkatriel iritkatriel self-assigned this Jun 30, 2023
rmartin16 added a commit to rmartin16/briefcase that referenced this issue Sep 1, 2023
- python-dateutil uses the deprecated datetime.utcfromtimestamp(); its
  next release after 2.8.2 will no longer use this function.
- Missing coverage is being falsely reported for an exit from a finally
  block; the coverage exclusion can be removed with python/cpython#105658.
@iritkatriel iritkatriel added 3.12 bugs and security fixes 3.13 bugs and security fixes labels Sep 13, 2023
@iritkatriel
Copy link
Member

Sorry, this dropped off my radar. I made a PR to remove the line number from these instructions (which are virtual).

iritkatriel added a commit to iritkatriel/cpython that referenced this issue Sep 14, 2023
Yhg1s pushed a commit that referenced this issue Sep 14, 2023
…h a conditional block (#109384) (#109411)

gh-105658: fix excess trace events for except block ending with a conditional block (#109384)

(cherry picked from commit 4a54074)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
3 participants