Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_warnings.warn_explicit() should try to import the warnings module #70779

Closed
vstinner opened this issue Mar 19, 2016 · 2 comments
Closed

_warnings.warn_explicit() should try to import the warnings module #70779

vstinner opened this issue Mar 19, 2016 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

BPO 26592
Nosy @vstinner
Files
  • warnings_import.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2016-03-22.23:56:19.424>
    created_at = <Date 2016-03-19.13:34:14.782>
    labels = ['type-feature']
    title = '_warnings.warn_explicit() should try to import the warnings module'
    updated_at = <Date 2016-03-22.23:56:19.423>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2016-03-22.23:56:19.423>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-03-22.23:56:19.424>
    closer = 'vstinner'
    components = []
    creation = <Date 2016-03-19.13:34:14.782>
    creator = 'vstinner'
    dependencies = []
    files = ['42217']
    hgrepos = []
    issue_num = 26592
    keywords = ['patch']
    message_count = 2.0
    messages = ['262049', '262219']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26592'
    versions = ['Python 3.6']

    @vstinner
    Copy link
    Member Author

    The issue bpo-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.

    @vstinner vstinner added the type-feature A feature request or enhancement label Mar 19, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 22, 2016

    New changeset 53343d095f78 by Victor Stinner in branch 'default':
    _warnings.warn_explicit(): try to import warnings
    https://hg.python.org/cpython/rev/53343d095f78

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant