Skip to content

Commit 9909a6f

Browse files
Merge branch 'main' into docs-duplicate-words
2 parents 0a0d390 + 10a3d43 commit 9909a6f

File tree

10 files changed

+45
-16
lines changed

10 files changed

+45
-16
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,23 @@ be specified by the build backend, as it will no longer be determined
978978
automatically by the C compiler. For a running interpreter, the setting that
979979
was used at compile time can be found using :func:`sysconfig.get_config_var`.
980980

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+
981998

982999
.. _whatsnew314-pyrepl-highlighting:
9831000

@@ -1028,6 +1045,18 @@ Please report any bugs or major performance regressions that you encounter!
10281045

10291046
.. seealso:: :pep:`744`
10301047

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`.)
10311060

10321061
Other language changes
10331062
======================

Include/cpython/funcobject.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
9797
}
9898
#define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
9999

100-
static inline PyObject* PyFunction_GET_BUILTINS(PyObject *func) {
101-
return _PyFunction_CAST(func)->func_builtins;
102-
}
103-
#define PyFunction_GET_BUILTINS(func) PyFunction_GET_BUILTINS(_PyObject_CAST(func))
104-
105100
static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
106101
return _PyFunction_CAST(func)->func_module;
107102
}

Include/cpython/pystate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
2828
#define PyTrace_OPCODE 7
2929

3030
/* Remote debugger support */
31-
#define MAX_SCRIPT_PATH_SIZE 512
32-
typedef struct _remote_debugger_support {
31+
#define Py_MAX_SCRIPT_PATH_SIZE 512
32+
typedef struct {
3333
int32_t debugger_pending_call;
34-
char debugger_script_path[MAX_SCRIPT_PATH_SIZE];
34+
char debugger_script_path[Py_MAX_SCRIPT_PATH_SIZE];
3535
} _PyRemoteDebuggerSupport;
3636

3737
typedef struct _err_stackitem {

Include/internal/pycore_debug_offsets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ typedef struct _Py_DebugOffsets {
368368
.remote_debugging_enabled = offsetof(PyInterpreterState, config.remote_debug), \
369369
.debugger_pending_call = offsetof(_PyRemoteDebuggerSupport, debugger_pending_call), \
370370
.debugger_script_path = offsetof(_PyRemoteDebuggerSupport, debugger_script_path), \
371-
.debugger_script_path_size = MAX_SCRIPT_PATH_SIZE, \
371+
.debugger_script_path_size = Py_MAX_SCRIPT_PATH_SIZE, \
372372
}, \
373373
}
374374

Include/internal/pycore_function.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ extern PyObject *_Py_set_function_type_params(
4141
PyAPI_FUNC(int)
4242
_PyFunction_VerifyStateless(PyThreadState *, PyObject *);
4343

44+
static inline PyObject* _PyFunction_GET_BUILTINS(PyObject *func) {
45+
return _PyFunction_CAST(func)->func_builtins;
46+
}
47+
#define _PyFunction_GET_BUILTINS(func) _PyFunction_GET_BUILTINS(_PyObject_CAST(func))
48+
4449

4550
#ifdef __cplusplus
4651
}

Lib/_pydatetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ def isoformat(self, sep='T', timespec='auto'):
21642164
By default, the fractional part is omitted if self.microsecond == 0.
21652165
21662166
If self.tzinfo is not None, the UTC offset is also attached, giving
2167-
giving a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM'.
2167+
a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmm+HH:MM'.
21682168
21692169
Optional argument sep specifies the separator between date and
21702170
time, default 'T'.

Lib/pydoc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,6 @@ def writedocs(dir, pkgpath='', done=None):
18121812

18131813

18141814
def _introdoc():
1815-
import textwrap
18161815
ver = '%d.%d' % sys.version_info[:2]
18171816
if os.environ.get('PYTHON_BASIC_REPL'):
18181817
pyrepl_keys = ''
@@ -2170,7 +2169,6 @@ def showtopic(self, topic, more_xrefs=''):
21702169
if more_xrefs:
21712170
xrefs = (xrefs or '') + ' ' + more_xrefs
21722171
if xrefs:
2173-
import textwrap
21742172
text = 'Related help topics: ' + ', '.join(xrefs.split()) + '\n'
21752173
wrapped_text = textwrap.wrap(text, 72)
21762174
doc += '\n%s\n' % '\n'.join(wrapped_text)

Modules/_testinternalcapi.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "pycore_fileutils.h" // _Py_normpath()
2222
#include "pycore_flowgraph.h" // _PyCompile_OptimizeCfg()
2323
#include "pycore_frame.h" // _PyInterpreterFrame
24+
#include "pycore_function.h" // _PyFunction_GET_BUILTINS
2425
#include "pycore_gc.h" // PyGC_Head
2526
#include "pycore_hashtable.h" // _Py_hashtable_new()
2627
#include "pycore_import.h" // _PyImport_ClearExtension()
@@ -1022,7 +1023,7 @@ get_code_var_counts(PyObject *self, PyObject *_args, PyObject *_kwargs)
10221023
globalsns = PyFunction_GET_GLOBALS(codearg);
10231024
}
10241025
if (builtinsns == NULL) {
1025-
builtinsns = PyFunction_GET_BUILTINS(codearg);
1026+
builtinsns = _PyFunction_GET_BUILTINS(codearg);
10261027
}
10271028
codearg = PyFunction_GET_CODE(codearg);
10281029
}
@@ -1190,7 +1191,7 @@ verify_stateless_code(PyObject *self, PyObject *args, PyObject *kwargs)
11901191
globalsns = PyFunction_GET_GLOBALS(codearg);
11911192
}
11921193
if (builtinsns == NULL) {
1193-
builtinsns = PyFunction_GET_BUILTINS(codearg);
1194+
builtinsns = _PyFunction_GET_BUILTINS(codearg);
11941195
}
11951196
codearg = PyFunction_GET_CODE(codearg);
11961197
}

Modules/posixmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ static void
685685
reset_remotedebug_data(PyThreadState *tstate)
686686
{
687687
tstate->remote_debugger_support.debugger_pending_call = 0;
688-
memset(tstate->remote_debugger_support.debugger_script_path, 0, MAX_SCRIPT_PATH_SIZE);
688+
memset(tstate->remote_debugger_support.debugger_script_path, 0,
689+
Py_MAX_SCRIPT_PATH_SIZE);
689690
}
690691

691692

Objects/funcobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ _PyFunction_VerifyStateless(PyThreadState *tstate, PyObject *func)
12561256
return -1;
12571257
}
12581258
// Check the builtins.
1259-
PyObject *builtinsns = PyFunction_GET_BUILTINS(func);
1259+
PyObject *builtinsns = _PyFunction_GET_BUILTINS(func);
12601260
if (builtinsns != NULL && !PyDict_Check(builtinsns)) {
12611261
_PyErr_Format(tstate, PyExc_TypeError,
12621262
"unsupported builtins %R", builtinsns);

0 commit comments

Comments
 (0)