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.11 regression: tracing with-statement on exit from block #89872

Closed
nedbat opened this issue Nov 4, 2021 · 7 comments
Closed

3.11 regression: tracing with-statement on exit from block #89872

nedbat opened this issue Nov 4, 2021 · 7 comments
Assignees
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@nedbat
Copy link
Member

nedbat commented Nov 4, 2021

BPO 45709
Nosy @nedbat, @markshannon, @pablogsal
PRs
  • bpo-45709: Fix tracing when exception is handled. #29638
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/markshannon'
    closed_at = <Date 2021-11-21.15:24:51.922>
    created_at = <Date 2021-11-04.10:08:51.497>
    labels = ['interpreter-core', 'type-bug', '3.11']
    title = '3.11 regression: tracing with-statement on exit from block'
    updated_at = <Date 2021-11-21.15:24:51.921>
    user = 'https://github.com/nedbat'

    bugs.python.org fields:

    activity = <Date 2021-11-21.15:24:51.921>
    actor = 'pablogsal'
    assignee = 'Mark.Shannon'
    closed = True
    closed_date = <Date 2021-11-21.15:24:51.922>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2021-11-04.10:08:51.497>
    creator = 'nedbat'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45709
    keywords = ['3.11regression']
    message_count = 7.0
    messages = ['405668', '405669', '405673', '406581', '406590', '406683', '406708']
    nosy_count = 3.0
    nosy_names = ['nedbat', 'Mark.Shannon', 'pablogsal']
    pr_nums = ['29638']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45709'
    versions = ['Python 3.11']

    @nedbat
    Copy link
    Member Author

    nedbat commented Nov 4, 2021

    Python 3.11 seems to have reverted a behavior that was new in 3.10.0b1: exiting a with-statement re-visits the with line on the way out.

    --- %< bug2.py ----------------------

    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
    
    print(sys.version)
    sys.settrace(trace)
    
    import contextlib
    
    def f():
        with contextlib.nullcontext():
            1/0
    
    f()

    Running with 3.10 shows re-visiting the with:

        $ python3.10 bug2.py
        3.10.0 (default, Oct  4 2021, 17:22:29) [Clang 12.0.0 (clang-1200.0.32.29)]
        call 15: def f():
        line 16:     with contextlib.nullcontext():
        line 17:         1/0
        exce 17:         1/0
        line 16:     with contextlib.nullcontext():
        retu 17:         1/0
        Traceback (most recent call last):
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
            f()
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
            1/0
        ZeroDivisionError: division by zero

    3.11 does not:

        $ python3.11 bug2.py
        3.11.0a1 (default, Oct  6 2021, 07:21:05) [Clang 12.0.0 (clang-1200.0.32.29)]
        call 15: def f():
        line 16:     with contextlib.nullcontext():
        line 17:         1/0
        exce 17:         1/0
        retu 17:         1/0
        Traceback (most recent call last):
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
            f()
            ^^^
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
            1/0
            ~^~
        ZeroDivisionError: division by zero

    Versions before 3.10 also do not visit the with statement:

        $ python3.9 bug2.py
        3.9.7 (default, Sep  7 2021, 22:16:49)
        [Clang 12.0.0 (clang-1200.0.32.29)]
        call 15: def f():
        line 16:     with contextlib.nullcontext():
        line 17:         1/0
        exce 17:         1/0
        line 17:         1/0
        retu 17:         1/0
        Traceback (most recent call last):
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 19, in <module>
            f()
          File "/System/Volumes/Data/root/src/foo/bug1270/bug2.py", line 17, in f
            1/0
        ZeroDivisionError: division by zero

    Is this a bug in 3.11, or an intentional return to previous behavior?

    (BTW: there is no 3.11regression keyword available)

    @nedbat nedbat added 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Nov 4, 2021
    @nedbat
    Copy link
    Member Author

    nedbat commented Nov 4, 2021

    BTW, this is the coverage.py issue: nedbat/coveragepy#1270

    @markshannon
    Copy link
    Member

    Probably an oversight when converting to zero-overhead exceptions.

    @markshannon markshannon self-assigned this Nov 4, 2021
    @markshannon markshannon added the type-bug An unexpected behavior, bug, or error label Nov 4, 2021
    @markshannon markshannon self-assigned this Nov 4, 2021
    @markshannon
    Copy link
    Member

    Sorry about the delay in fixing this.

    @markshannon
    Copy link
    Member

    New changeset 337cb48 by Mark Shannon in branch 'main':
    bpo-45709: Fix tracing when exception is handled. (GH-29638)
    337cb48

    @pablogsal
    Copy link
    Member

    Mark, os something left to do here?

    @nedbat
    Copy link
    Member Author

    nedbat commented Nov 21, 2021

    I can confirm that this fixes the problem.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants