8000 pure-Python warn_explicit() passes wrong arg to WarningMessage · Issue #129843 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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

Closed
jwilk opened this issue Feb 8, 2025 · 8 comments
Closed

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

jwilk opened this issue Feb 8, 2025 · 8 comments
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jwilk
Copy link
jwilk commented Feb 8, 2025

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

@picnixz picnixz added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Feb 8, 2025
@picnixz
Copy link
Member
picnixz commented 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.

@picnixz picnixz added the easy label Feb 8, 2025
@jwilk
Copy link
Author
jwilk commented Feb 8, 2025

Do you want to send a PR fixing this?

No.

@sergey-miryanov
Copy link
Contributor

I can take this if no one else wants it.

@picnixz
Copy link
Member
picnixz commented Feb 8, 2025

Go ahead Sergey!

@picnixz
Copy link
Member
picnixz commented 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
Copy link
Contributor

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

@sergey-miryanov
Copy link
Contributor

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

srinivasreddy added a commit to srinivasreddy/cpython that referenced this issue Mar 17, 2025
picnixz pushed a commit that referenced this issue Mar 17, 2025
…#129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 17, 2025
…licit` (pythonGH-129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
(cherry picked from commit 80e00ec)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 17, 2025
…licit` (pythonGH-129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
(cherry picked from commit 80e00ec)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
picnixz pushed a commit that referenced this issue Mar 17, 2025
…plicit` (GH-129848) (#131349)

gh-129843: fix pure Python implementation of `warnings.warn_explicit` (GH-129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
(cherry picked from commit 80e00ec)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
picnixz pushed a commit that referenced this issue Mar 17, 2025
…plicit` (GH-129848) (#131350)

gh-129843: fix pure Python implementation of `warnings.warn_explicit` (GH-129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
(cherry picked from commit 80e00ec)

Co-authored-by: Srinivas Reddy Thatiparthy (తాటిపర్తి శ్రీనివాస్  రెడ్డి) <thatiparthysreenivas@gmail.com>
@picnixz
Copy link
Member
picnixz commented Mar 17, 2025

Thanks for the report & the fix!

@picnixz picnixz closed this as completed Mar 17, 2025
plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
…licit` (python#129848)
6ACC


The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…licit` (python#129848)

The pure Python implementation of `warnings.warn_explicit` constructs a `WarningMessage`
with an incorrect source (it incorrectly sets the WarningMessage's line to the given `source`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants
0