Skip to content

gh-128384: Use contextvar for catch_warnings() - #128463

Closed
nascheme wants to merge 4 commits into
python:mainfrom
nascheme:catch_warnings_contextvar
Closed

nascheme wants to merge 4 commits into
python:mainfrom
nascheme:catch_warnings_contextvar

Conversation

@nascheme

@nascheme nascheme commented Jan 3, 2025 •

Copy link
Copy Markdown
Member

This changes warnings.catch_warnings() to use a contextvar for the new list of filters. If catch_warnings is not used, the list of filters is the module global warnings.filters. When the context manager is active, the effective list of filters is warnings.get_context()._filters.

This change is on top of two other unmerged PRs:


📚 Documentation preview 📚: https://cpython-previews--128463.org.readthedocs.build/

By default, inherit the context from the thread calling
`Thread.start()`.
Expose the mutex from _warnings.c and hold it when mutating the filters
list or the filters version.
@nascheme

Copy link
Copy Markdown
Member Author

Implemented in GH-130010.

@nascheme nascheme closed this Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant