Skip to content

gh-128384: Use a context variable for warnings.catch_warnings #130010

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

Merged
merged 47 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ba99f2e
Make _contextvars a builtin module.
nascheme Dec 28, 2024
6d00c2a
Add 'context' parameter to Thread.
nascheme Dec 21, 2024
a868fe9
Tweak blurb markup.
nascheme Feb 7, 2025
16fa2c3
Doc markup fix.
nascheme Feb 7, 2025
f0ccc8d
Use contextvar for catch_warnings().
nascheme Dec 27, 2024
bcadd20
Add blurb.
nascheme Feb 11, 2025
0cfe578
Add "_warnings_context" as identifier.
nascheme Feb 11, 2025
928c2df
Fix test_support for context var filters.
nascheme Feb 11, 2025
65751d9
Regenerate 'configure' script.
nascheme Feb 11, 2025
09e72b8
Rename flag to `thread_inherit_context`.
nascheme Feb 11, 2025
75f1b38
Merge branch 'main' into thread_inherit_context
nascheme Feb 11, 2025
872920d
Regenerate 'configure' script.
nascheme Feb 11, 2025
5b6b59e
Merge branch 'thread_inherit_context' into gh-128384-warnings-contextvar
nascheme Feb 11, 2025
9e955dc
Use separate flag `thread_safe_warnings`.
nascheme Feb 12, 2025
3a56c64
Add doc for ``thread_safe_warnings`` flag.
nascheme Feb 12, 2025
daa3d52
Create _py_warnings.py for Python implementation.
nascheme Feb 18, 2025
83419e4
Add _warnings_context to _warnings module.
nascheme Feb 18, 2025
15443e8
Remove '_warnings_context' global string.
nascheme Feb 18, 2025
983e7ee
Rename flag to 'context_aware_warnings'.
nascheme Feb 18, 2025
e5e660c
Use catch_warnings() for module under test.
nascheme Feb 18, 2025
e054a57
Don't pass 'module' to catch_warnings().
nascheme Feb 18, 2025
2466cec
Correct error in warnings module docs.
nascheme Feb 19, 2025
165a573
Use PyObject_GetAttr.
nascheme Feb 19, 2025
af8728d
Typo fix for docstring.
nascheme Feb 19, 2025
1c02b7e
Avoid DECREF calls on None.
nascheme Feb 19, 2025
dba89e0
Add warnings.py module (missed in previous commit).
nascheme Feb 19, 2025
2c48fc5
Add comment about why 'context' is passed in test.
nascheme Feb 19, 2025
53eb72d
Revise "decimal' docs, adding note about flag.
nascheme Feb 21, 2025
9cb6f73
Merge branch 'thread_inherit_context' into gh-128384-warnings-contextvar
nascheme Feb 21, 2025
f79daaa
Fix race in filter_search().
nascheme Feb 25, 2025
5ca1d39
Add note to free-threading howto.
nascheme Mar 11, 2025
9420a9d
Merge branch 'main' into gh-128384-warnings-contextvar
nascheme Mar 11, 2025
bad0fdb
Doc fixes for missing refs.
nascheme Mar 12, 2025
bb35c2e
Add "_py_warnings" to stdlib_module_names.h.
nascheme Mar 12, 2025
adf32cb
Improve error text in Python/_warnings.c
nascheme Mar 17, 2025
47d0b6f
Avoid unused-variable warning in _warnings.c.
nascheme Mar 17, 2025
b880dd1
Minor code improvement to _warnings.c.
nascheme Mar 17, 2025
5a1115b
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 17, 2025
ae701e3
Merge branch 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 18, 2025
153c6b1
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Mar 26, 2025
9220223
Add extra unit tests.
nascheme Mar 27, 2025
c2c90cb
Use asyncio events to get deterministic execution.
nascheme Mar 27, 2025
c1def22
Merge 'origin/main' into gh-128384-warnings-contextvar
nascheme Apr 9, 2025
4e461d9
Add unit test for asyncio tasks.
nascheme Apr 9, 2025
543927b
Update Doc/howto/free-threading-python.rst
nascheme Apr 9, 2025
4f11910
Use asyncio.gather() rather than create_task().
nascheme Apr 9, 2025
42d157b
Call resetwarnings() in a few places.
nascheme Apr 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use separate flag thread_safe_warnings.
  • Loading branch information
nascheme committed Feb 12, 2025
commit 9e955dc723d60d1f0d1017781fa098579f83d1bb
41 changes: 23 additions & 18 deletions Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -635,17 +635,18 @@
Thread-safety of Context Managers
---------------------------------

The behavior of :class:`catch_warnings` context manager depends on the value

Check warning on line 638 in Doc/library/warnings.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: sys.flags.thread_safe_warnings [ref.data]
of the :data:`sys.flags.inherit_context` flag. Being thread-safe means that
behavior is predictable in a multi-threaded program. For free-threaded
builds, the flag defaults to true, and false otherwise.
of the :data:`sys.flags.thread_safe_warnings` flag. If the flag is true, the
context manager behaves in a thread-safe fashion and otherwise not. Being
thread-safe means that behavior is predictable in a multi-threaded program.
For free-threaded builds, the flag defaults to true, and false otherwise.

If the :data:`~sys.flags.inherit_context` flag is false, then
:class:`catch_warnings` will manipulate the global attributes of the
If the :data:`~sys.flags.thread_safe_warnings` flag is false, then

Check warning on line 644 in Doc/library/warnings.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: sys.flags.thread_safe_warnings [ref.data]
:class:`catch_warnings` will modify the global attributes of the
:mod:`warnings` module. This is not thread-safe. If two or more threads use
the context manager at the same time, the behavior is undefined.

If the flag is true, :class:`catch_warnings` will not manipulate global
If the flag is true, :class:`catch_warnings` will not modify global
attributes and will instead use a :class:`~contextvars.ContextVar` to
store the newly established warning filtering state. A context variable
provides thread-local storage and it makes the use of :class:`catch_warnings`
Expand All @@ -661,21 +662,25 @@
property in the context variable. In this case, the :func:`showwarning`
function will not be restored when exiting the context handler.

The :data:`~sys.flags.inherit_context` flag can be set the :option:`-X
inherit_context <-X>` command-line option or by the
:envvar:`PYTHON_INHERIT_CONTEXT` environment variable.
The :data:`~sys.flags.thread_safe_warnings` flag can be set the :option:`-X

Check warning on line 665 in Doc/library/warnings.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: sys.flags.thread_safe_warnings [ref.data]
thread_safe_warnings<-X>` command-line option or by the
:envvar:`PYTHON_THREAD_SAFE_WARNINGS` environment variable.

.. note::

When the :data:`~sys.flags.inherit_context` flag true, it also causes
threads created by :class:`threading.Thread` to start with a copy of
the context variables from the thread starting it. This means that
context established by using :class:`catch_warnings` in one thread
will also apply to new threads started by it. If the new thread creates
a new context with :class:`catch_warnings`, that context only applies to
that thread.
It is likely that most programs that desire thread-safe

Check warning on line 671 in Doc/library/warnings.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: warnings.filters [ref.data]
behaviour of the warnings module will also want to set the
:data:`~sys.flags.thread_inherit_context` flag to true. That flag
causes threads created by :class:`threading.Thread` to start
with a copy of the context variables from the thread starting
it. When true, the context established by :class:`catch_warnings`
in one thread will also apply to new threads started by it. If false,
new threads will start with an empty warnings context variable,
meaning that only the filters in :data:`warnings.filters` will be
active.

.. versionchanged:: 3.14

Added the :data:`sys.flags.inherit_context` flag and the use of a context
variable for :class:`catch_warnings` if the flag is true.
Added the :data:`sys.flags.thread_safe_warnings` flag and the use of a

Check warning on line 684 in Doc/library/warnings.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: sys.flags.thread_safe_warnings [ref.data]
context variable for :class:`catch_warnings` if the flag is true. Previous
versions of Python acted as if the flag was always set to false.
18 changes: 18 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ Miscellaneous options

.. versionadded:: 3.14

* :samp:`-X thread_safe_warnings={0,1}` causes the
:class:`warnings.catch_warnings` context manager to use a
:class:`~contextvars.ContextVar` to store warnings filter state. If
unset, the value of this option defaults to ``1`` on free-threaded builds
and to ``0`` otherwise. See also :envvar:`PYTHON_THREAD_SAFE_WARNINGS`.

.. versionadded:: 3.14

It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.

Expand Down Expand Up @@ -1240,6 +1248,16 @@ conflict.

.. versionadded:: 3.14

.. envvar:: PYTHON_THREAD_SAFE_WARNINGS

If set to ``1`` then the :class:`warnings.catch_warnings` context
manager will use a :class:`~contextvars.ContextVar` to store warnings
filter state. If unset, this variable defaults to ``1`` on
free-threaded builds and to ``0`` otherwise. See :option:`-X
thread_safe_warnings<-X>`.

.. versionadded:: 3.14

Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ typedef struct PyConfig {
int safe_path;
int int_max_str_digits;
int thread_inherit_context;
int thread_safe_warnings;
#ifdef __APPLE__
int use_system_logger;
#endif
Expand Down
6 changes: 5 additions & 1 deletion Lib/test/test_capi/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_config_get(self):
("hash_seed", int, None),
("home", str | None, None),
("thread_inherit_context", int, None),
("thread_safe_warnings", int, None),
("import_time", bool, None),
("inspect", bool, None),
("install_signal_handlers", bool, None),
Expand Down Expand Up @@ -171,7 +172,7 @@ def test_config_get_sys_flags(self):
("warn_default_encoding", "warn_default_encoding", False),
("safe_path", "safe_path", False),
("int_max_str_digits", "int_max_str_digits", False),
# "gil" and "thread_inherit_context" are tested below
# "gil", "thread_inherit_context" and "thread_safe_warnings" are tested below
):
with self.subTest(flag=flag, name=name, negate=negate):
value = config_get(name)
Expand All @@ -191,6 +192,9 @@ def test_config_get_sys_flags(self):
expected_inherit_context = 1 if support.Py_GIL_DISABLED else 0
self.assertEqual(sys.flags.thread_inherit_context, expected_inherit_context)

expected_safe_warnings = 1 if support.Py_GIL_DISABLED else 0
self.assertEqual(sys.flags.thread_safe_warnings, expected_safe_warnings)

def test_config_get_non_existent(self):
# Test PyConfig_Get() on non-existent option name
config_get = _testcapi.config_get
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@
PLATSTDLIB_LANDMARK = (f'{sys.platlibdir}/python{VERSION_MAJOR}.'
f'{VERSION_MINOR}{ABI_THREAD}/lib-dynload')

if support.Py_GIL_DISABLED:
DEFAULT_THREAD_INHERIT_CONTEXT = 1
else:
DEFAULT_THREAD_INHERIT_CONTEXT = 0
DEFAULT_THREAD_INHERIT_CONTEXT = 1 if support.Py_GIL_DISABLED else 0
DEFAULT_THREAD_SAFE_WARNINGS = 1 if support.Py_GIL_DISABLED else 0

# If we are running from a build dir, but the stdlib has been installed,
# some tests need to expect different results.
Expand Down Expand Up @@ -591,6 +589,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'perf_profiling': 0,
'import_time': False,
'thread_inherit_context': DEFAULT_THREAD_INHERIT_CONTEXT,
'thread_safe_warnings': DEFAULT_THREAD_SAFE_WARNINGS,
'code_debug_ranges': True,
'show_ref_count': False,
'dump_refs': False,
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1845,9 +1845,10 @@ def test_pythontypes(self):
# symtable entry
# XXX
# sys.flags
# FIXME: The +2 is for the 'gil' and 'thread_inherit_context' flags and
# will not be necessary once gh-122575 is fixed
check(sys.flags, vsize('') + self.P * (2 + len(sys.flags)))
# FIXME: The +3 is for the 'gil', 'thread_inherit_context' and
# 'thread_safe_warnings' flags and will not be necessary once
# gh-122575 is fixed
check(sys.flags, vsize('') + self.P * (3 + len(sys.flags)))

def test_asyncgen_hooks(self):
old = sys.get_asyncgen_hooks()
Expand Down
2 changes: 1 addition & 1 deletion Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# If true, catch_warnings() will use a context var to hold the modified
# filters list. Otherwise, catch_warnings() will operate on the 'filters'
# global of the warnings module.
_use_context = sys.flags.inherit_context
_use_context = sys.flags.thread_safe_warnings

class _Context:
def __init__(self, filters):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Make :class:`warnings.catch_warnings` use a context variable for holding the
warning filtering state if the :data:`sys.flags.inherit_context` flag is set
to true. This makes using the context manager thread-safe in multi-threaded
programs. The flag is true by default in free-threaded builds and is
otherwise false. The default value of the flag can be overridden by the the
:option:`-X inherit_context <-X>` command-line option or by the
:envvar:`PYTHON_INHERIT_CONTEXT` environment variable.
Make :class:`warnings.catch_warnings` use a context variable for holding
the warning filtering state if the :data:`sys.flags.thread_safe_warnings`
flag is set to true. This makes using the context manager thread-safe in
multi-threaded programs. The flag is true by default in free-threaded builds
and is otherwise false. The value of the flag can be overridden by the
the :option:`-X thread_safe_warnings <-X>` command-line option or by the
:envvar:`PYTHON_THREAD_SAFE_WARNINGS` environment variable.
47 changes: 46 additions & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
SPEC(hash_seed, ULONG, READ_ONLY, NO_SYS),
SPEC(home, WSTR_OPT, READ_ONLY, NO_SYS),
SPEC(thread_inherit_context, INT, READ_ONLY, NO_SYS),
SPEC(thread_safe_warnings, INT, READ_ONLY, NO_SYS),
SPEC(import_time, BOOL, READ_ONLY, NO_SYS),
SPEC(install_signal_handlers, BOOL, READ_ONLY, NO_SYS),
SPEC(isolated, BOOL, READ_ONLY, NO_SYS), // sys.flags.isolated
Expand Down Expand Up @@ -329,6 +330,12 @@ The following implementation-specific options are available:\n\
-X thread_inherit_context=[0|1]: enable (1) or disable (0) threads inheriting\n\
context vars by default; enabled by default in the free-threaded\n\
build and disabled otherwise; also PYTHON_THREAD_INHERIT_CONTEXT\n\
-X thread_safe_warnings=[0|1]: if true (1) then the warnings module will\n\
use a context variable to store warnings filtering state, making it\n\
safe to use in multi-threaded programs; if false (0) then the\n\
warnings module will use module globals, which is not thread-safe;\n\
set to true for free-threaded builds and false otherwise; also\n\
PYTHON_THREAD_SAFE_WARNINGS\n\
-X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
-X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\
Expand Down Expand Up @@ -416,8 +423,10 @@ static const char usage_envvars[] =
#ifdef Py_GIL_DISABLED
"PYTHON_TLBC : when set to 0, disables thread-local bytecode (-X tlbc)\n"
#endif
"PYTHON_THREAD_INHERIT_CONTEXT: threads inherit context vars if 1\n"
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
" (-X thread_inherit_context)\n"
"PYTHON_THREAD_SAFE_WARNINGS: if true (1), enable thread-safe warnings module\n"
" behaviour (-X thread_safe_warnings)\n"
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
Expand Down Expand Up @@ -894,6 +903,7 @@ config_check_consistency(const PyConfig *config)
// config->use_frozen_modules is initialized later
// by _PyConfig_InitImportConfig().
assert(config->thread_inherit_context >= 0);
assert(config->thread_safe_warnings >= 0);
#ifdef __APPLE__
assert(config->use_system_logger >= 0);
#endif
Expand Down Expand Up @@ -1001,8 +1011,10 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->cpu_count = -1;
#ifdef Py_GIL_DISABLED
config->thread_inherit_context = 1;
config->thread_safe_warnings = 1;
#else
config->thread_inherit_context = 0;
config->thread_safe_warnings = 0;
#endif
#ifdef __APPLE__
config->use_system_logger = 0;
Expand Down Expand Up @@ -1038,8 +1050,10 @@ config_init_defaults(PyConfig *config)
#endif
#ifdef Py_GIL_DISABLED
config->thread_inherit_context = 1;
config->thread_safe_warnings = 1;
#else
config->thread_inherit_context = 0;
config->thread_safe_warnings = 0;
#endif
#ifdef __APPLE__
config->use_system_logger = 0;
Expand Down Expand Up @@ -1935,6 +1949,32 @@ config_init_thread_inherit_context(PyConfig *config)
return _PyStatus_OK();
}

static PyStatus
config_init_thread_safe_warnings(PyConfig *config)
{
const char *env = config_get_env(config, "PYTHON_THREAD_SAFE_WARNINGS");
if (env) {
int enabled;
if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) {
return _PyStatus_ERR(
"PYTHON_THREAD_SAFE_WARNINGS=N: N is missing or invalid");
}
config->thread_safe_warnings = enabled;
}

const wchar_t *xoption = config_get_xoption(config, L"thread_safe_warnings");
if (xoption) {
int enabled;
const wchar_t *sep = wcschr(xoption, L'=');
if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) {
return _PyStatus_ERR(
"-X thread_safe_warnings=n: n is missing or invalid");
}
config->thread_safe_warnings = enabled;
}
return _PyStatus_OK();
}

static PyStatus
config_init_tlbc(PyConfig *config)
{
Expand Down Expand Up @@ -2219,6 +2259,11 @@ config_read_complex_options(PyConfig *config)
return status;
}

status = config_init_thread_safe_warnings(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = config_init_tlbc(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
2 changes: 2 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3142,6 +3142,7 @@ static PyStructSequence_Field flags_fields[] = {
{"int_max_str_digits", "-X int_max_str_digits"},
{"gil", "-X gil"},
{"thread_inherit_context", "-X thread_inherit_context"},
{"thread_safe_warnings", "-X thread_safe_warnings"},
{0}
};

Expand Down Expand Up @@ -3246,6 +3247,7 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
SetFlagObj(PyLong_FromLong(1));
#endif
SetFlag(config->thread_inherit_context);
SetFlag(config->thread_safe_warnings);
#undef SetFlagObj
#undef SetFlag
return 0;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy