You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pure-Python warn_explicit() passes wrong arg to WarningMessage #129843
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'
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.
@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.
Activity
picnixz commentedon Feb 8, 2025
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.jwilk commentedon Feb 8, 2025
No.
sergey-miryanov commentedon Feb 8, 2025
I can take this if no one else wants it.
picnixz commentedon Feb 8, 2025
Go ahead Sergey!
pythongh-129843: Fix the wrong argument passed - converting positional
picnixz commentedon Feb 8, 2025
@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.
srinivasreddy commentedon Feb 8, 2025
Apologies for raising the PR. If @sergey-miryanov wanted to raise PR, I am fine to close mine .
sergey-miryanov commentedon Feb 8, 2025
@srinivasreddy
You already did. I'm okay with that.
Merge branch 'main' into pythongh-129843
gh-129843: fix pure Python implementation of `warnings.warn_explicit` (…
pythongh-129843: fix pure Python implementation of `warnings.warn_exp…
pythongh-129843: fix pure Python implementation of `warnings.warn_exp…
[3.13] gh-129843: fix pure Python implementation of `warnings.warn_ex…
[3.12] gh-129843: fix pure Python implementation of `warnings.warn_ex…
picnixz commentedon Mar 17, 2025
Thanks for the report & the fix!
pythongh-129843: fix pure Python implementation of `warnings.warn_exp…