X Tutup
The Wayback Machine - https://web.archive.org/web/20250530104922/https://github.com/python/cpython/issues/95748
Skip to content

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

Open
@Jackenmen

Description

@Jackenmen

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:

msg = WarningMessage(message, category, filename, lineno, file, line)

msg = WarningMessage(message, category, filename, lineno, None, line)

msg = WarningMessage(message, category, filename, lineno, source)

while it is in Python/_warnings.c:

cpython/Python/_warnings.c

Lines 593 to 605 in a17cd47

warnmsg_cls = GET_WARNINGS_ATTR(interp, WarningMessage, 0);
if (warnmsg_cls == NULL) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"unable to get warnings.WarningMessage");
}
goto error;
}
msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
filename, lineno_obj, Py_None, Py_None, source,
NULL);
Py_DECREF(warnmsg_cls);

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup