Skip to content

pure-Python warn_explicit() passes wrong arg to WarningMessage #129843

Closed
@jwilk

Description

@jwilk

The pure-Python implementation of the warnings.warn_explicit() function does this:

msg = WarningMessage(message, category, filename, lineno, source)

But the 5th argument of WarningMessage is file (the file the message is supposed to be printed into), not source ("the destroyed object which emitted a ResourceWarning").

Here's how to reproduce the bug:

>>> import sys, importlib, warnings
>>> warnings.warn_explicit('eggs', UserWarning, 'eggs.py', 42, source=object())
eggs.py:42: UserWarning: eggs
Object allocated at (most recent call last):
  File "<stdin>", lineno 1
>>> # so far so good; now let's try the same with pure-Python implementation
>>> sys.modules['_warnings'] = None
>>> importlib.reload(warnings)
<module 'warnings' from '/usr/lib/python3.12/warnings.py'>
>>> warnings.warn_explicit('eggs', UserWarning, 'eggs.py', 42, source=object())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/warnings.py", line 413, in warn_explicit
    _showwarnmsg(msg)
  File "/usr/lib/python3.12/warnings.py", line 115, in _showwarnmsg
    _showwarnmsg_impl(msg)
  File "/usr/lib/python3.12/warnings.py", line 30, in _showwarnmsg_impl
    file.write(text)
    ^^^^^^^^^^
AttributeError: 'object' object has no attribute 'write'

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    easystdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions