-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
warnings.catch_warnings is async-unsafe #91505
Comments
Using |
Sounds like a big project. |
Hi. The warning module stores pointers to the original warning functions and swaps that function in and out with the catch_warnings() context mnager. This is obviously a recipe for disaster either running with threads or just if mixing catch_warnings() with other APIs that also like to take over the warnings function. My suggestion is that all monkeypatching of global functions in the warnings module must be removed. I have a draft implementation I can push eventually, which uses contextvars to keep track of which catch_warnings() object is active and push the warning messages there. And that's it is. So far my branch is incomplete because it also needs to keep track of filters activated with catch_warnings(). Hopefully we can have a discussion about the implementation and potentially consider including the new implentation in the next major release. |
@joaoe Please describe your design and explain how it solves the problem. I'm very interested in hearing from you! |
Hi Here's a draft commit
Again, this is a draft, a suggestion. |
Hi @joaoe, I'm afraid I won't personally have time to review this any time soon. I hope that another core dev or triager might be interested. Maybe @kumaraditya303, @serhiy-storchaka, or @sobolevn ? Also, I recommend that you open a Draft PR -- that's easier for reviewers. The title of the draft PR should start with "gh-91505:" so it will be linked back to this issue. Good luck! |
@colesbury May I join the conversation ? I made a draft implementation, obviously needs discussion and review. |
@joaoe, yes that's why I posted here so that we can continue the discussion publicly. I don't think the discussion on Discord covered much new ground:
|
GraalPy just uses the warnings module as-is from CPython's stdlib, and implements the |
Despite technically being documented (https://docs.python.org/3/library/warnings.html#warnings.catch_warnings), this is very error-prone, surprising and not at all useful. The warnings module should probably use
contextvars
to manage the warning handler.The text was updated successfully, but these errors were encountered: