python / python/cpython

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

Aperta
#149,211 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

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

Inizia dallo script di riproduzione nell’issue e confronta il suo normale percorso degli avvisi con il comportamento di pulizia dell’interprete. Esamina la PR collegata gh-150944 per vedere l’ambito proposto; il lavoro è completato quando il gestore personalizzato warnings.showwarning viene rispettato in modo coerente durante la pulizia, con una copertura per il reproducer.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.