Skip to content

Exception handling ignored in minimal while loop #108214

Open
@pan324

Description

@pan324

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker,
    and am confident this bug has not been reported before

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux, Windows

Output from running 'python -VV' on the command line:

No response

A clear and concise description of the bug:

Consider the following codeblock that works correctly when interrupted by ctrl+c.

try:
    while 1: 
        pass
except KeyboardInterrupt:
    print("Exception handling works!")
Exception handling works!

Now we push the pass into the same line and suddenly our try-except is ignored entirely:

try:
    while 1: pass
except KeyboardInterrupt:
    print("Exception handling works!")
Traceback (most recent call last):
  File "/home/panta/issue.py", line 2, in <module>
    while 1: pass
KeyboardInterrupt

This was quite a surprise to me because I naively expected both approaches to emit the same bytecode (except with different line info).

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only 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