python / python/cpython

logging.captureWarnings(True) doesn't show object allocation (-X tracemalloc)

Ouverte
#95,748 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

stdlib type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
36k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

When using logging.captureWarnings(True), the information about object allocation that you should get with -X tracemalloc gets lost.

It appears to be a discrepancy between Python and C implementation of the warnings module? source isn't passed to the created WarningMessage object in the Lib/warnings.py:
https://github.com/python/cpython/blob/a17cd47b614f8bc660788647a009a25e121221d7/Lib/warnings.py#L12
https://github.com/python/cpython/blob/a17cd47b614f8bc660788647a009a25e121221d7/Lib/warnings.py#L17
https://github.com/python/cpython/blob/a17cd47b614f8bc660788647a009a25e121221d7/Lib/warnings.py#L394
while it is in Python/_warnings.c:
https://github.com/python/cpython/blob/a17cd47b614f8bc660788647a009a25e121221d7/Python/_warnings.c#L593-L605

Reproduction steps:

  1. Create a file repro.py with contents:
import logging
import tempfile
import warnings

warnings.simplefilter("default", category=ResourceWarning)
logging.basicConfig()


print("--- UNCAPTURED WARNING ---")
file = tempfile.SpooledTemporaryFile(1)
file.write(b"foo")
file = None

print("\n--- CAPTURED WARNING ---")
logging.captureWarnings(True)
file = tempfile.SpooledTemporaryFile(1)
file.write(b"foo")
file = None
  1. Run python -X tracemalloc=5
  2. See that output only contains object allocation traceback for uncaptured warning:
--- UNCAPTURED WARNING ---
/tmp/test_tracemallo/repro.py:12: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
  file = None
Object allocated at (most recent call last):
  File "/tmp/test_tracemallo/repro.py", lineno 11
    file.write(b"foo")
  File "/usr/lib/python3.10/tempfile.py", lineno 911
    self._check(file)
  File "/usr/lib/python3.10/tempfile.py", lineno 807
    self.rollover()
  File "/usr/lib/python3.10/tempfile.py", lineno 812
    newfile = self._file = TemporaryFile(**self._TemporaryFileArgs)
  File "/usr/lib/python3.10/tempfile.py", lineno 760
    return _io.open(fd, mode, buffering=buffering,

--- CAPTURED WARNING ---
WARNING:py.warnings:/tmp/test_tracemallo/repro.py:18: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
  file = None

Your environment

  • CPython versions tested on: 3.7.13, 3.8.13, 3.9.13 (installed from deadsnakes ppa), 3.10.4 (system Python)
  • Operating system and architecture: Ubuntu 22.04
Linked PRs
  • gh-111451

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Comparez la construction de WarningMessage dans Lib/warnings.py avec le traitement correspondant dans Python/_warnings.c, puis exécutez la commande repro.py fournie avec -X tracemalloc=5. C’est terminé lorsque les avertissements redirigés via logging.captureWarnings(True) conservent la trace d’exécution de l’allocation de l’objet affichée pour les avertissements non capturés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
c, python
Domaine
backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.