GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Bug in linecache when raising exceptiongroup that contains `ZeroDivisionError` + other exception. · Issue #101517 · python/cpython

Bug in linecache when raising exceptiongroup that contains ZeroDivisionError + other exception. #101517

New issue
Copy link
New issue
Copy link
Closed
Closed
Bug in linecache when raising exceptiongroup that contains ZeroDivisionError + other exception.#101517
Copy link
Assignees
iritkatriel
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@jonathanslenders
jonathanslenders
opened on Feb 2, 2023
Issue body actions

Bug report

Consider this code:

from asyncio import run, TaskGroup

async def bad_function():
    raise ZeroDivisionError
async def bad_function2():
    raise KeyError

async def main():
    try:
        async with TaskGroup() as tg:
            tg.create_task(bad_function())
            tg.create_task(bad_function2())

    except* ZeroDivisionError as e:
        breakpoint()


run(main())

Run it on Python 3.11 or 3.12:

--Return--
Traceback (most recent call last):
  File "....py", line 18, in <module>
    run(main())
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/asyncio/base_events.py", line 650, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File ".....py", line -1, in main
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/bdb.py", line 94, in trace_dispatch
    return self.dispatch_return(frame, arg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/bdb.py", line 153, in dispatch_return
    self.user_return(frame, arg)
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/pdb.py", line 372, in user_return
    self.interaction(frame, None)
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/pdb.py", line 434, in interaction
    self.print_stack_entry(self.stack[self.curindex])
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/pdb.py", line 1554, in print_stack_entry
    self.format_stack_entry(frame_lineno, prompt_prefix))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/bdb.py", line 573, in format_stack_entry
    line = linecache.getline(filename, lineno, frame.f_globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jonathan/.pyenv/versions/3.11.0/lib/python3.11/linecache.py", line 31, in getline
    if 1 <= lineno <= len(lines):
TypeError: '<=' not supported between instances of 'int' and 'NoneType'

One of the exceptions has to be a ZeroDivisionError for it to happen as far as I can see.

Your environment

  • CPython versions tested on: 3.11, 3.12
  • Operating system and architecture: Linux, M1, arm64

Linked PRs

  • gh-101517: Fix missing linenumber for re-raise in except* #101663
  • gh-101517: Attach line number to the RERAISE at the end of a try-except* #101768
  • gh-101517: bdb should not lookup linecache with lineno=None #101787
  • [3.11] gh-101517: make bdb avoid looking up in linecache with lineno=None (GH-101787) #101793
  • [3.10] gh-101517: make bdb avoid looking up in linecache with lineno=None (GH-101787) #101794
  • gh-101517: propagate lasti with RERAISE 1 in except* handling to get currect line numbers #103529
  • gh-101517: Add regression test for entering pdb in try/except* block #103547
  • gh-101517: fix line number propagation in code generated for except* #103550
  • [3.11] gh-101517: fix line number propagation in code generated for except* (#103550) #103816

Activity

Next
  1. jonathanslenders
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 2, 2023
  2. arhadthedev
    added
    topic-asyncio
    on Feb 2, 2023
  3. ezio-melotti
    added this to asyncioon Feb 2, 2023
  4. github-project-automation
    moved this to Todo in asyncioon Feb 2, 2023
  5. gvanrossum commented on Feb 2, 2023

    @gvanrossum
    gvanrossum
    on Feb 2, 2023
    Member
    More actions

    Can you produce a repro without asyncio? I suspect you can get this by just raising and catching two errors and then raise an ExceptionGroup. @iritkatriel

  6. iritkatriel commented on Feb 2, 2023

    @iritkatriel
    iritkatriel
    on Feb 2, 2023
    Member
    More actions

    It's something to do with a breakpoint as the last thing in an except block.

    Without asyncio:

    excs = []
    try:
        raise ZeroDivisionError
    except Exception as e:
        excs.append(e)
    
    try:
        raise KeyError
    except Exception as e:
        excs.append(e)
    
    
    def main():
        try:
            raise ExceptionGroup("eg", excs)
        except* ZeroDivisionError as e:
            breakpoint()
            # pass    # <-- uncomment this and the problem goes away
    
    main()
    
    
  7. iritkatriel
    added
    3.11only security fixesonly security fixes
    3.12only security fixesonly security fixes
    and removed
    topic-asyncio
    on Feb 2, 2023
  8. AlexWaygood
    removed this from asyncioon Feb 2, 2023
  9. iritkatriel commented on Feb 2, 2023

    @iritkatriel
    iritkatriel
    on Feb 2, 2023
    Member
    More actions

    Something to do with frame initialisation? @markshannon @brandtbucher

    excs = []
    try:
        raise ZeroDivisionError
    except Exception as e:
        excs.append(e)
    
    try:
        raise KeyError
    except Exception as e:
        excs.append(e)
    
    def main():
        try:
            raise ExceptionGroup("eg", excs)
        except* ZeroDivisionError as e:
            #sys._getframe()     # <-- uncomment this and the problem goes away
            breakpoint()
    
    
    main()
    
    
  10. brandtbucher commented on Feb 2, 2023

    @brandtbucher
    brandtbucher
    on Feb 2, 2023
    Member
    More actions

    I don't see the problem going away when I add the sys._getframe() call...

  11. brandtbucher commented on Feb 2, 2023

    @brandtbucher
    brandtbucher
    on Feb 2, 2023
    Member
    More actions

    This does reproduce the issue for me, though:

    try:
        raise ExceptionGroup("eg", [KeyError(), ValueError()])
    except* KeyError:
        breakpoint()
        # pass    # <-- uncomment this and the problem goes away
  12. brandtbucher commented on Feb 2, 2023

    @brandtbucher
    brandtbucher
    on Feb 2, 2023
    Member
    More actions

    Note that this only reproduces if some exceptions are uncaught (the ValueError, in my above example... turning it into a KeyError makes the issue go away).

  13. iritkatriel commented on Feb 2, 2023

    @iritkatriel
    iritkatriel
    on Feb 2, 2023
    Member
    More actions

    I think it's some kind of heisenbug. I worked around the lineno being None in linecache.py:31, then it failed on None in another place later on, so I added a "print the frame" just before that line, and the problem went away.

  14. 32 remaining items

  15. gaogaotiantian commented on Apr 14, 2023

    @gaogaotiantian
    gaogaotiantian
    on Apr 14, 2023
    Member
    More actions

    How could pdb know it is "wrong"? It can be a real bummer when the debugger itself crashes, so I just want it to keep working as well as it can. Sometimes the debugger is being used to track down bugs in the compiler.

    Sure we can try to make the debugger work even if python is not working properly. I guess we can generate a warning for f_lineno is None but not crash - so the user know something is wrong, but it'll still work on some commands.

    Reacted by Irit Katriel, Guido van Rossum and Alex Waygood
  16. iritkatriel
    added 4 commits that reference this issue on Apr 14, 2023

    gh-101517: Add regression test for a lineno bug in try/except* impact…

    7c1b0a4

    pythongh-101517: propagate lasti with RERAISE 1 in except* handling t…

    5b54131

    pythongh-101517: fix line number propagation in code generated for ex…

    674f558

    pythongh-101517: fix line number propagation in code generated for ex…

    eb2b5a5
  17. iritkatriel commented on Apr 14, 2023

    @iritkatriel
    iritkatriel
    on Apr 14, 2023
    Member
    More actions

    PR is here: #103550

    I didn't use the warnings module, just print like in a few other places in this module (I'm assuming there's a good reason for that).

  18. carljm
    added 2 commits that reference this issue on Apr 17, 2023

    Merge branch 'main' into superopt

    5136459

    Merge branch 'superopt' into superopt_spec

    079c65a
  19. iritkatriel
    added a commit that references this issue on Apr 24, 2023

    gh-101517: fix line number propagation in code generated for except* (#…

    1c01f8d
  20. carljm
    added 2 commits that reference this issue on Apr 24, 2023

    Merge branch 'main' into superopt

    0de5bc6

    Merge branch 'superopt' into superopt_spec

    1972ca4
  21. iritkatriel
    added 2 commits that reference this issue on Apr 25, 2023

    pythongh-101517: fix line number propagation in code generated for ex…

    e62d5d9

    [3.11] gh-101517: fix line number propagation in code generated for e…

    93a9535
  22. iritkatriel commented on Apr 25, 2023

    @iritkatriel
    iritkatriel
    on Apr 25, 2023
    Member
    More actions

    This was fixed (in 3.11 as well). We will follow up on the more general problem in #103795.

  23. iritkatriel
    closed this as completedon Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @iritkatriel
    iritkatriel

Labels

3.11only security fixesonly security fixes3.12only security fixesonly security fixestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.