@@ -978,6 +978,23 @@ be specified by the build backend, as it will no longer be determined
978
978
automatically by the C compiler. For a running interpreter, the setting that
979
979
was used at compile time can be found using :func: `sysconfig.get_config_var `.
980
980
981
+ A new flag has been added, :data: `~sys.flags.context_aware_warnings `. This
982
+ flag defaults to true for the free-threaded build and false for the GIL-enabled
983
+ build. If the flag is true then the :class: `warnings.catch_warnings ` context
984
+ manager uses a context variable for warning filters. This makes the context
985
+ manager behave predicably when used with multiple threads or asynchronous
986
+ tasks.
987
+
988
+ A new flag has been added, :data: `~sys.flags.thread_inherit_context `. This flag
989
+ defaults to true for the free-threaded build and false for the GIL-enabled
990
+ build. If the flag is true then threads created with :class: `threading.Thread `
991
+ start with a copy of the :class: `~contextvars.Context() ` of the caller of
992
+ :meth: `~threading.Thread.start `. Most significantly, this makes the warning
993
+ filtering context established by :class: `~warnings.catch_warnings ` be
994
+ "inherited" by threads (or asyncio tasks) started within that context. It also
995
+ affects other modules that use context variables, such as the :mod: `decimal `
996
+ context manager.
997
+
981
998
982
999
.. _whatsnew314-pyrepl-highlighting :
983
1000
@@ -1028,6 +1045,18 @@ Please report any bugs or major performance regressions that you encounter!
1028
1045
1029
1046
.. seealso :: :pep:`744`
1030
1047
1048
+ Concurrent safe warnings control
1049
+ --------------------------------
1050
+
1051
+ The :class: `warnings.catch_warnings ` context manager will now optionally
1052
+ use a context variable for warning filters. This is enabled by setting
1053
+ the :data: `~sys.flags.context_aware_warnings ` flag, either with the ``-X ``
1054
+ command-line option or an environment variable. This gives predicable
1055
+ warnings control when using :class: `~warnings.catch_warnings ` combined with
1056
+ multiple threads or asynchronous tasks. The flag defaults to true for the
1057
+ free-threaded build and false for the GIL-enabled build.
1058
+
1059
+ (Contributed by Neil Schemenauer and Kumar Aditya in :gh: `130010 `.)
1031
1060
1032
1061
Other language changes
1033
1062
======================
0 commit comments