python / python/cpython

Exceptions raised while tracing certain statements cannot be caught

Aberta
#148,278 4 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

3.13 3.14 3.15 interpreter-core type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Reproduza os exemplos na issue e inspecione sua saída de dis, concentrando-se nas entradas ausentes da tabela de exceções para pass, 42 e return. Compare esses casos com o tracing de instruções normais e examine o tratamento de NOP e o desenrolamento da pilha de blocos pelo compilador. Considera-se concluído quando os blocos except capturam as exceções geradas durante o tracing dessas instruções, com cobertura de regressão para os casos descritos.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
compilers
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.