Skip to content

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

Closed

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 8, 2025
  2. picnixz commented on Feb 8, 2025

    @picnixz
    Member

    This indeed looks wrong. Do you want to send a PR fixing this? The patch is rather easy as you just need to write WarningMessage(message, category, filename, lineno, source=source) I think.

  3. jwilk commented on Feb 8, 2025

    @jwilk
    Author

    Do you want to send a PR fixing this?

    No.

  4. sergey-miryanov commented on Feb 8, 2025

    @sergey-miryanov
    Contributor

    I can take this if no one else wants it.

  5. picnixz commented on Feb 8, 2025

    @picnixz
    Member

    Go ahead Sergey!

  6. added a commit that references this issue on Feb 8, 2025
  7. picnixz commented on Feb 8, 2025

    @picnixz
    Member

    @srinivasreddy In the future, it would be nice not to make a PR when someone else has already suggested to do it. If they do not open one for a few days, it's also better to first ask them if they are still working on the task.

  8. srinivasreddy commented on Feb 8, 2025

    @srinivasreddy
    Contributor

    Apologies for raising the PR. If @sergey-miryanov wanted to raise PR, I am fine to close mine .

  9. sergey-miryanov commented on Feb 8, 2025

    @sergey-miryanov
    Contributor

    @srinivasreddy
    You already did. I'm okay with that.

  10. added a commit that references this issue on Mar 17, 2025
  11. added 2 commits that reference this issue on Mar 17, 2025
  12. added 2 commits that reference this issue on Mar 17, 2025
  13. picnixz commented on Mar 17, 2025

    @picnixz
    Member

    Thanks for the report & the fix!

  14. added a commit that references this issue on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions