Skip to content

Commit 983e7ee

Browse files
committed
Rename flag to 'context_aware_warnings'.
1 parent 15443e8 commit 983e7ee

File tree

11 files changed

+70
-67
lines changed

11 files changed

+70
-67
lines changed

Doc/library/sys.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ always available. Unless explicitly noted otherwise, all variables are read-only
602602
- :option:`-X thread_inherit_context <-X>` and
603603
:envvar:`PYTHON_THREAD_INHERIT_CONTEXT`
604604

605-
* - .. attribute:: flags.thread_safe_warnings
605+
* - .. attribute:: flags.context_aware_warnings
606606
- :option:`-X thread_inherit_context <-X>` and
607-
:envvar:`PYTHON_THREAD_SAFE_WARNINGS`
607+
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS`
608608

609609

610610
.. versionchanged:: 3.2
@@ -640,7 +640,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only
640640
Added the ``thread_inherit_context`` attribute.
641641

642642
.. versionchanged:: 3.14
643-
Added the ``thread_safe_warnings`` attribute.
643+
Added the ``context_aware_warnings`` attribute.
644644

645645

646646
.. data:: float_info

Doc/library/warnings.rst

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ of deprecated code.
328328

329329
.. note::
330330

331-
See :ref:`warning-thread-safe` for details on the thread-safety of the
332-
:class:`catch_warnings` context manager when used in multi-threaded
333-
programs.
331+
See :ref:`warning-concurrent-safe` for details on the
332+
concurrency-safety of the :class:`catch_warnings` context manager when
333+
used in programs using multiple threads or async functions.
334334

335335
.. _warning-testing:
336336

@@ -370,9 +370,9 @@ results.
370370

371371
.. note::
372372

373-
See :ref:`warning-thread-safe` for details on the thread-safety of the
374-
:class:`catch_warnings` context manager when used in multi-threaded
375-
programs.
373+
See :ref:`warning-concurrent-safe` for details on the
374+
concurrency-safety of the :class:`catch_warnings` context manager when
375+
used in programs using multiple threads or async functions.
376376

377377
When testing multiple operations that raise the same kind of warning, it
378378
is important to test them in a manner that confirms each operation is raising
@@ -620,31 +620,35 @@ Available Context Managers
620620

621621
.. note::
622622

623-
See :ref:`warning-thread-safe` for details on the thread-safety of the
624-
:class:`catch_warnings` context manager when used in multi-threaded
625-
programs.
623+
See :ref:`warning-concurrent-safe` for details on the
624+
concurrency-safety of the :class:`catch_warnings` context manager when
625+
used in programs using multiple threads or async functions.
626626

627627

628628
.. versionchanged:: 3.11
629629

630630
Added the *action*, *category*, *lineno*, and *append* parameters.
631631

632632

633-
.. _warning-thread-safe:
633+
.. _warning-concurrent-safe:
634634

635-
Thread-safety of Context Managers
636-
---------------------------------
635+
Concurrent safety of Context Managers
636+
-------------------------------------
637637

638-
The behavior of :class:`catch_warnings` context manager depends on the value
639-
of the :data:`sys.flags.thread_safe_warnings` flag. If the flag is true, the
640-
context manager behaves in a thread-safe fashion and otherwise not. Being
641-
thread-safe means that behavior is predictable in a multi-threaded program.
642-
For free-threaded builds, the flag defaults to true, and false otherwise.
638+
The behavior of :class:`catch_warnings` context manager depends on the
639+
:data:`sys.flags.context_aware_warnings` flag. If the flag is true, the
640+
context manager behaves in a concurrent-safe fashion and otherwise not.
641+
Concurrent-safe means that it is both thread-safe and safe to use within
642+
:ref:`asyncio coroutines <coroutine>` and tasks. Being thread-safe means
643+
that behavior is predictable in a multi-threaded program. The flag defaults
644+
to true for free-threaded builds and false otherwise.
643645

644-
If the :data:`~sys.flags.thread_safe_warnings` flag is false, then
646+
If the :data:`~sys.flags.context_aware_warnings` flag is false, then
645647
:class:`catch_warnings` will modify the global attributes of the
646-
:mod:`warnings` module. This is not thread-safe. If two or more threads use
647-
the context manager at the same time, the behavior is undefined.
648+
:mod:`warnings` module. This is not safe if used within a concurrent program
649+
(using multiple threads or using asyncio coroutines). For example, if two
650+
or more threads use the :class:`catch_warnings` class at the same time, the
651+
behavior is undefined.
648652

649653
If the flag is true, :class:`catch_warnings` will not modify global
650654
attributes and will instead use a :class:`~contextvars.ContextVar` to
@@ -655,16 +659,16 @@ thread-safe.
655659
The *record* parameter of the context handler also behaves differently
656660
depending on the value of the flag. When *record* is true and the flag is
657661
false, the context manager works by replacing and then later restoring the
658-
module's :func:`showwarning` function. This is not thread-safe.
662+
module's :func:`showwarning` function. That is not concurrent-safe.
659663

660664
When *record* is true and the flag is false, the :func:`showwarning` function
661665
is not replaced. The recording status is instead indicated by an internal
662666
property in the context variable. In this case, the :func:`showwarning`
663667
function will not be restored when exiting the context handler.
664668

665-
The :data:`~sys.flags.thread_safe_warnings` flag can be set the :option:`-X
666-
thread_safe_warnings<-X>` command-line option or by the
667-
:envvar:`PYTHON_THREAD_SAFE_WARNINGS` environment variable.
669+
The :data:`~sys.flags.context_aware_warnings` flag can be set the :option:`-X
670+
context_aware_warnings<-X>` command-line option or by the
671+
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS` environment variable.
668672

669673
.. note::
670674

@@ -681,6 +685,6 @@ thread_safe_warnings<-X>` command-line option or by the
681685

682686
.. versionchanged:: 3.14
683687

684-
Added the :data:`sys.flags.thread_safe_warnings` flag and the use of a
688+
Added the :data:`sys.flags.context_aware_warnings` flag and the use of a
685689
context variable for :class:`catch_warnings` if the flag is true. Previous
686690
versions of Python acted as if the flag was always set to false.

Doc/using/cmdline.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,11 @@ Miscellaneous options
637637

638638
.. versionadded:: 3.14
639639

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

646646
.. versionadded:: 3.14
647647

@@ -1248,13 +1248,13 @@ conflict.
12481248

12491249
.. versionadded:: 3.14
12501250

1251-
.. envvar:: PYTHON_THREAD_SAFE_WARNINGS
1251+
.. envvar:: PYTHON_CONTEXT_AWARE_WARNINGS
12521252

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

12591259
.. versionadded:: 3.14
12601260

Include/cpython/initconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ typedef struct PyConfig {
180180
int safe_path;
181181
int int_max_str_digits;
182182
int thread_inherit_context;
183-
int thread_safe_warnings;
183+
int context_aware_warnings;
184184
#ifdef __APPLE__
185185
int use_system_logger;
186186
#endif

Lib/_py_warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _set_module(module):
4646
# If true, catch_warnings() will use a context var to hold the modified
4747
# filters list. Otherwise, catch_warnings() will operate on the 'filters'
4848
# global of the warnings module.
49-
_use_context = sys.flags.thread_safe_warnings
49+
_use_context = sys.flags.context_aware_warnings
5050

5151

5252
class _Context:

Lib/test/test_capi/test_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_config_get(self):
5656
("hash_seed", int, None),
5757
("home", str | None, None),
5858
("thread_inherit_context", int, None),
59-
("thread_safe_warnings", int, None),
59+
("context_aware_warnings", int, None),
6060
("import_time", bool, None),
6161
("inspect", bool, None),
6262
("install_signal_handlers", bool, None),
@@ -172,7 +172,7 @@ def test_config_get_sys_flags(self):
172172
("warn_default_encoding", "warn_default_encoding", False),
173173
("safe_path", "safe_path", False),
174174
("int_max_str_digits", "int_max_str_digits", False),
175-
# "gil", "thread_inherit_context" and "thread_safe_warnings" are tested below
175+
# "gil", "thread_inherit_context" and "context_aware_warnings" are tested below
176176
):
177177
with self.subTest(flag=flag, name=name, negate=negate):
178178
value = config_get(name)
@@ -193,7 +193,7 @@ def test_config_get_sys_flags(self):
193193
self.assertEqual(sys.flags.thread_inherit_context, expected_inherit_context)
194194

195195
expected_safe_warnings = 1 if support.Py_GIL_DISABLED else 0
196-
self.assertEqual(sys.flags.thread_safe_warnings, expected_safe_warnings)
196+
self.assertEqual(sys.flags.context_aware_warnings, expected_safe_warnings)
197197

198198
def test_config_get_non_existent(self):
199199
# Test PyConfig_Get() on non-existent option name

Lib/test/test_embed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
f'{VERSION_MINOR}{ABI_THREAD}/lib-dynload')
6262

6363
DEFAULT_THREAD_INHERIT_CONTEXT = 1 if support.Py_GIL_DISABLED else 0
64-
DEFAULT_THREAD_SAFE_WARNINGS = 1 if support.Py_GIL_DISABLED else 0
64+
DEFAULT_CONTEXT_AWARE_WARNINGS = 1 if support.Py_GIL_DISABLED else 0
6565

6666
# If we are running from a build dir, but the stdlib has been installed,
6767
# some tests need to expect different results.
@@ -589,7 +589,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
589589
'perf_profiling': 0,
590590
'import_time': False,
591591
'thread_inherit_context': DEFAULT_THREAD_INHERIT_CONTEXT,
592-
'thread_safe_warnings': DEFAULT_THREAD_SAFE_WARNINGS,
592+
'context_aware_warnings': DEFAULT_CONTEXT_AWARE_WARNINGS,
593593
'code_debug_ranges': True,
594594
'show_ref_count': False,
595595
'dump_refs': False,

Lib/test/test_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ def test_pythontypes(self):
18461846
# XXX
18471847
# sys.flags
18481848
# FIXME: The +3 is for the 'gil', 'thread_inherit_context' and
1849-
# 'thread_safe_warnings' flags and will not be necessary once
1849+
# 'context_aware_warnings' flags and will not be necessary once
18501850
# gh-122575 is fixed
18511851
check(sys.flags, vsize('') + self.P * (3 + len(sys.flags)))
18521852

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Make :class:`warnings.catch_warnings` use a context variable for holding
2-
the warning filtering state if the :data:`sys.flags.thread_safe_warnings`
2+
the warning filtering state if the :data:`sys.flags.context_aware_warnings`
33
flag is set to true. This makes using the context manager thread-safe in
44
multi-threaded programs. The flag is true by default in free-threaded builds
55
and is otherwise false. The value of the flag can be overridden by the
6-
the :option:`-X thread_safe_warnings <-X>` command-line option or by the
7-
:envvar:`PYTHON_THREAD_SAFE_WARNINGS` environment variable.
6+
the :option:`-X context_aware_warnings <-X>` command-line option or by the
7+
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS` environment variable.

Python/initconfig.c

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
142142
SPEC(hash_seed, ULONG, READ_ONLY, NO_SYS),
143143
SPEC(home, WSTR_OPT, READ_ONLY, NO_SYS),
144144
SPEC(thread_inherit_context, INT, READ_ONLY, NO_SYS),
145-
SPEC(thread_safe_warnings, INT, READ_ONLY, NO_SYS),
145+
SPEC(context_aware_warnings, INT, READ_ONLY, NO_SYS),
146146
SPEC(import_time, BOOL, READ_ONLY, NO_SYS),
147147
SPEC(install_signal_handlers, BOOL, READ_ONLY, NO_SYS),
148148
SPEC(isolated, BOOL, READ_ONLY, NO_SYS), // sys.flags.isolated
@@ -330,12 +330,11 @@ The following implementation-specific options are available:\n\
330330
-X thread_inherit_context=[0|1]: enable (1) or disable (0) threads inheriting\n\
331331
context vars by default; enabled by default in the free-threaded\n\
332332
build and disabled otherwise; also PYTHON_THREAD_INHERIT_CONTEXT\n\
333-
-X thread_safe_warnings=[0|1]: if true (1) then the warnings module will\n\
334-
use a context variable to store warnings filtering state, making it\n\
335-
safe to use in multi-threaded programs; if false (0) then the\n\
336-
warnings module will use module globals, which is not thread-safe;\n\
337-
set to true for free-threaded builds and false otherwise; also\n\
338-
PYTHON_THREAD_SAFE_WARNINGS\n\
333+
-X context_aware_warnings=[0|1]: if true (1) then the warnings module will\n\
334+
use a context variables; if false (0) then the warnings module will\n\
335+
use module globals, which is not concurrent-safe; set to true for\n\
336+
free-threaded builds and false otherwise; also\n\
337+
PYTHON_CONTEXT_AWARE_WARNINGS\n\
339338
-X tracemalloc[=N]: trace Python memory allocations; N sets a traceback limit\n \
340339
of N frames (default: 1); also PYTHONTRACEMALLOC=N\n\
341340
-X utf8[=0|1]: enable (1) or disable (0) UTF-8 mode; also PYTHONUTF8\n\
@@ -425,8 +424,8 @@ static const char usage_envvars[] =
425424
#endif
426425
"PYTHON_THREAD_INHERIT_CONTEXT: if true (1), threads inherit context vars\n"
427426
" (-X thread_inherit_context)\n"
428-
"PYTHON_THREAD_SAFE_WARNINGS: if true (1), enable thread-safe warnings module\n"
429-
" behaviour (-X thread_safe_warnings)\n"
427+
"PYTHON_CONTEXT_AWARE_WARNINGS: if true (1), enable thread-safe warnings module\n"
428+
" behaviour (-X context_aware_warnings)\n"
430429
"PYTHONTRACEMALLOC: trace Python memory allocations (-X tracemalloc)\n"
431430
"PYTHONUNBUFFERED: disable stdout/stderr buffering (-u)\n"
432431
"PYTHONUTF8 : control the UTF-8 mode (-X utf8)\n"
@@ -903,7 +902,7 @@ config_check_consistency(const PyConfig *config)
903902
// config->use_frozen_modules is initialized later
904903
// by _PyConfig_InitImportConfig().
905904
assert(config->thread_inherit_context >= 0);
906-
assert(config->thread_safe_warnings >= 0);
905+
assert(config->context_aware_warnings >= 0);
907906
#ifdef __APPLE__
908907
assert(config->use_system_logger >= 0);
909908
#endif
@@ -1011,10 +1010,10 @@ _PyConfig_InitCompatConfig(PyConfig *config)
10111010
config->cpu_count = -1;
10121011
#ifdef Py_GIL_DISABLED
10131012
config->thread_inherit_context = 1;
1014-
config->thread_safe_warnings = 1;
1013+
config->context_aware_warnings = 1;
10151014
#else
10161015
config->thread_inherit_context = 0;
1017-
config->thread_safe_warnings = 0;
1016+
config->context_aware_warnings = 0;
10181017
#endif
10191018
#ifdef __APPLE__
10201019
config->use_system_logger = 0;
@@ -1050,10 +1049,10 @@ config_init_defaults(PyConfig *config)
10501049
#endif
10511050
#ifdef Py_GIL_DISABLED
10521051
config->thread_inherit_context = 1;
1053-
config->thread_safe_warnings = 1;
1052+
config->context_aware_warnings = 1;
10541053
#else
10551054
config->thread_inherit_context = 0;
1056-
config->thread_safe_warnings = 0;
1055+
config->context_aware_warnings = 0;
10571056
#endif
10581057
#ifdef __APPLE__
10591058
config->use_system_logger = 0;
@@ -1950,27 +1949,27 @@ config_init_thread_inherit_context(PyConfig *config)
19501949
}
19511950

19521951
static PyStatus
1953-
config_init_thread_safe_warnings(PyConfig *config)
1952+
config_init_context_aware_warnings(PyConfig *config)
19541953
{
1955-
const char *env = config_get_env(config, "PYTHON_THREAD_SAFE_WARNINGS");
1954+
const char *env = config_get_env(config, "PYTHON_CONTEXT_AWARE_WARNINGS");
19561955
if (env) {
19571956
int enabled;
19581957
if (_Py_str_to_int(env, &enabled) < 0 || (enabled < 0) || (enabled > 1)) {
19591958
return _PyStatus_ERR(
1960-
"PYTHON_THREAD_SAFE_WARNINGS=N: N is missing or invalid");
1959+
"PYTHON_CONTEXT_AWARE_WARNINGS=N: N is missing or invalid");
19611960
}
1962-
config->thread_safe_warnings = enabled;
1961+
config->context_aware_warnings = enabled;
19631962
}
19641963

1965-
const wchar_t *xoption = config_get_xoption(config, L"thread_safe_warnings");
1964+
const wchar_t *xoption = config_get_xoption(config, L"context_aware_warnings");
19661965
if (xoption) {
19671966
int enabled;
19681967
const wchar_t *sep = wcschr(xoption, L'=');
19691968
if (!sep || (config_wstr_to_int(sep + 1, &enabled) < 0) || (enabled < 0) || (enabled > 1)) {
19701969
return _PyStatus_ERR(
1971-
"-X thread_safe_warnings=n: n is missing or invalid");
1970+
"-X context_aware_warnings=n: n is missing or invalid");
19721971
}
1973-
config->thread_safe_warnings = enabled;
1972+
config->context_aware_warnings = enabled;
19741973
}
19751974
return _PyStatus_OK();
19761975
}
@@ -2259,7 +2258,7 @@ config_read_complex_options(PyConfig *config)
22592258
return status;
22602259
}
22612260

2262-
status = config_init_thread_safe_warnings(config);
2261+
status = config_init_context_aware_warnings(config);
22632262
if (_PyStatus_EXCEPTION(status)) {
22642263
return status;
22652264
}

0 commit comments

Comments
 (0)