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/whatsnew/3.6.rst | |
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/whatsnew/3.6.rst')
-rw-r--r-- | Doc/whatsnew/3.6.rst | 34 |
1 files changed, 34 insertions, 0 deletions
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 ------- |