python / python/cpython

Exceptions raised while tracing certain statements cannot be caught

Aperta
#148,278 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.13 3.14 3.15 interpreter-core type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.)

Linked PRs
  • gh-148333

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riprodurre gli esempi nell’issue ed esaminare il relativo output di dis, concentrandosi sulle voci mancanti nella tabella delle eccezioni per pass, 42 e return. Confrontare questi casi con il tracing delle istruzioni normali ed esaminare la gestione di NOP da parte del compilatore e l’unwinding dello stack dei blocchi. Il lavoro è completato quando i blocchi except intercettano le eccezioni sollevate durante il tracing di queste istruzioni, con una copertura di regressione per i casi descritti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.