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

Aberta
#95,748 3 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
3/5
Tempo estimado
1-2 dias
Facilidade para iniciantes
25/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Estagnada
Stack de tecnologia
c, python
Domínio
backend

Direção de pesquisa

Compare a construção de WarningMessage em Lib/warnings.py com o tratamento correspondente em Python/_warnings.c e execute o comando repro.py fornecido com -X tracemalloc=5. Está concluído quando os avisos encaminhados por logging.captureWarnings(True) preservarem o traceback de alocação do objeto exibido para avisos não capturados.

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

Descrição

stdlib type-bug

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
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Merge médio
1d 9h
PRs com merge (30d)
558

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.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

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