Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
The pure-Python implementation of thewarnings.warn_explicit() function does this:
msg=WarningMessage(message,category,filename,lineno,source)
But the 5th argument ofWarningMessage isfile (the file the message is supposed to be printed into), notsource ("the destroyed object which emitted aResourceWarning").
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: eggsObject 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'