python / python/cpython

Default `warnings.showwarning` always used when interpreter session is cleaning up

Aberta
#149,211 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

Hey!

I'm not sure this is a bug, exactly, or just behaviour that is not documented well (or at all, despite me looking I couldn't really find anything on it), but I recently ran into it and was scratching my head for a good hour.

The "issue" if it even is one, is that the default warnings.showwarning handler is always called when the main scope for the interpreter is getting cleaned up, even if there is a custom showwarning hook.

I noticed this by having some resource leak messages in the __del__ calls for some objects, and noticed that they only trigger the proper handler if they are triggered in any scope under the main scope, but if done so in the main scope they always use the default handler.

The following code reproduces the behaviour:

import warnings

def _warning_handler(message, category, filename, lineno, output_file, line):
    warn = warnings.formatwarning(message, category, filename, lineno, line)
    print(f'😺: {warn}', file = output_file)

warnings.showwarning = _warning_handler

def meow():
    print(f'Warning handler is: {warnings.showwarning.__code__.co_filename}')
    warnings.warn('meow')

class Cat:
    def __del__(self):
        print(f'Warning handler is: {warnings.showwarning.__code__.co_filename}')
        warnings.warn('nya')

meow()

cat = Cat()

The output of this code is:

Warning handler is: /tmp/mvp.py
😺: /tmp/mvp.py:11: UserWarning: meow
  warnings.warn('meow')

Warning handler is: /tmp/mvp.py
/tmp/mvp.py:16: UserWarning: nya
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-150944

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

Comece pelo script de reprodução na issue e compare seu caminho normal de avisos com o comportamento de limpeza do interpretador. Revise o PR vinculado gh-150944 para verificar o escopo proposto; considera-se concluído quando o handler personalizado warnings.showwarning for respeitado de forma consistente durante a limpeza, com cobertura para o reproducer.

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

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
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.