New submission from STINNER Victor:
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.
----------
files: warnings_import.patch
keywords: patch
messages: 262049
nosy: haypo
priority: normal
severity: normal
status: open
title: _warnings.warn_explicit() should try to import the warnings module
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file42217/warnings_import.patch
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26592>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com