python / python/cpython

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

Abierto
#149,211 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza con el script de reproducción de la issue y compara su ruta normal de advertencias con el comportamiento de limpieza del intérprete. Revisa el PR enlazado gh-150944 para ver el alcance propuesto; se considera terminado cuando el controlador personalizado warnings.showwarning se respeta de forma coherente durante la limpieza, con cobertura para el reproducer.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.