diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-03-19 00:03:51 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-03-19 00:03:51 (GMT) |
commit | 914cde89d4c94b0b9206d0fa22322a1142833a56 (patch) | |
tree | 7eed294f0da18437f719df470dbee278cfb40787 /Doc | |
parent | 1231a4615fd447f0988a72a134a1fc5e7d4e8d69 (diff) | |
download | cpython-914cde89d4c94b0b9206d0fa22322a1142833a56.zip cpython-914cde89d4c94b0b9206d0fa22322a1142833a56.tar.gz cpython-914cde89d4c94b0b9206d0fa22322a1142833a56.tar.bz2 |
On ResourceWarning, log traceback where the object was allocated
Issue #26567:
* Add a new function PyErr_ResourceWarning() function to pass the destroyed
object
* Add a source attribute to warnings.WarningMessage
* Add warnings._showwarnmsg() which uses tracemalloc to get the traceback where
source object was allocated.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/c-api/exceptions.rst | 8 | ||||
-rw-r--r-- | Doc/library/warnings.rst | 8 | ||||
-rw-r--r-- | Doc/whatsnew/3.6.rst | 34 |
3 files changed, 49 insertions, 1 deletions
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index 1e708a8..57f36ac 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -334,6 +334,14 @@ an error value). .. versionadded:: 3.2 +.. c:function:: int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...) + + Function similar to :c:func:`PyErr_WarnFormat`, but *category* is + :exc:`ResourceWarning` and pass *source* to :func:`warnings.WarningMessage`. + + .. versionadded:: 3.6 + + Querying the error indicator ============================ diff --git a/Doc/library/warnings.rst b/Doc/library/warnings.rst index 8a538ad..4ce88ab 100644 --- a/Doc/library/warnings.rst +++ b/Doc/library/warnings.rst @@ -319,7 +319,7 @@ Available Functions of the warning message). -.. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None) +.. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None) This is a low-level interface to the functionality of :func:`warn`, passing in explicitly the message, category, filename and line number, and optionally the @@ -335,6 +335,12 @@ Available Functions source for modules found in zipfiles or other non-filesystem import sources). + *source*, if supplied, is the destroyed object which emitted a + :exc:`ResourceWarning`. + + .. versionchanged:: 3.6 + Add the *source* parameter. + .. function:: showwarning(message, category, filename, lineno, file=None, line=None) diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst index cc63589..b709917 100644 --- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -258,6 +258,40 @@ urllib.robotparser (Contributed by Nikolay Bogoychev in :issue:`16099`.) +warnings +-------- + +A new optional *source* parameter has been added to the +:func:`warnings.warn_explicit` function: the destroyed object which emitted a +:exc:`ResourceWarning`. A *source* attribute has also been added to +:class:`warnings.WarningMessage` (contributed by Victor Stinner in +:issue:`26568` and :issue:`26567`). + +When a :exc:`ResourceWarning` warning is logged, the :mod:`tracemalloc` is now +used to try to retrieve the traceback where the detroyed object was allocated. + +Example with the script ``example.py``:: + + def func(): + f = open(__file__) + f = None + + func() + +Output of the command ``python3.6 -Wd -X tracemalloc=5 example.py``:: + + example.py:3: ResourceWarning: unclosed file <...> + f = None + Object allocated at (most recent call first): + File "example.py", lineno 2 + f = open(__file__) + File "example.py", lineno 5 + func() + +The "Object allocated at" traceback is new and only displayed if +:mod:`tracemalloc` is tracing Python memory allocations. + + zipfile ------- |