This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2016-03-19.13:34:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1458394454.81.0.10940568277.issue26592@psf.upfronthosting.co.za>
In-reply-to
Content
The issue #26567 enhanced the warnings module to pass the destroyed object on ResourceWarning. The implementation to log the traceback where the object was allocated is non trivial, so I chose to only implement it in Python.

Problem: _warnings.warn_explicit() is lazy, it tries to get the warnings module from sys.modules, but it uses its own logger if the warnings is not imported yet.

As a consequence, the traceback where the object was allocated is only logged if tracemalloc is tracing Python memory allocations *and* if the warnings module is already imported.

I suggest to modify _warnings.warn_explicit() to import the warnings if it's not imported yet.

There are maybe issues to import a module during Python finalization? A comprise is to not try to import when Python finalization has started.

Example:
---
#import warnings
import tracemalloc
tracemalloc.start(10)
f = open("/etc/issue")
f = None
---

Current output:
---
x.py:5: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
  f = None
---

Output if you uncomment the "import warnings":
---
x.py:5: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
  f = None
Object allocated at (most recent call first):
  File "x.py", lineno 4
    f = open("/etc/issue")
---

Attached patch tries to import warnings if source object is set but not after the Pythin finalization has started.
History
Date User Action Args
2016-03-19 13:34:14vstinnersetrecipients: + vstinner
2016-03-19 13:34:14vstinnersetmessageid: <1458394454.81.0.10940568277.issue26592@psf.upfronthosting.co.za>
2016-03-19 13:34:14vstinnerlinkissue26592 messages
2016-03-19 13:34:14vstinnercreate