Skip to content

Exceptions raised while tracing certain statements cannot be caught #148278

@brandtbucher

Description

@brandtbucher

Bug report

So I found a really odd bug today. except blocks don't catch exceptions raised when tracing certain statements that themselves "cannot raise". For example:

def f():
    breakpoint()
    try:
        pass  # If you raise while tracing this line, the exception isn't caught.
    except:
        ...
    try:
        42  # Ditto.
    except:
        ...
    try:
        return  # Ditto.
    except:
        ...

A couple of observations:

  • Exceptions raised while tracing other "normal" statements are handled the normal way.
  • The code for the exception handling is still present in these examples, even though it's statically unreachable.
  • If two or more of these "non-raising" statements occur together in the same block (like several pass statements in a row), only the last one will have the bug:
def f():
    breakpoint()
    try:
        pass  # No bug
        pass  # No bug
        pass  # Bug
    except:
        ...

I haven't invested any time into figuring out what the root cause is, I've just been poking at it with code examples. It's definitely a bug in the bytecode compiler though, not the interpreter (dis shows missing exception table entries). Probably something to do with how we emit exception tables for NOP or anything that unwinds the block stack, if I had to guess?

(This also affects 3.12, and maybe even earlier versions, but only tagging for 3.13 and newer since I don't think this is a "security" issue.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions