Skip to content

Commit bdbb491

Browse files
committed
Use thread inheritable contextvars.
Change `warnings` and `decimal` to use thread inheritable context vars. This means new threads will start with bindings for these variables from the starter thread, rather than having empty bindings (no matter the value of the thread_inherit_context flag).
1 parent 1f146e6 commit bdbb491

5 files changed

Lines changed: 9 additions & 8 deletions

File tree

Lib/_py_warnings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _filters(self):
8383
_global_context = _GlobalContext()
8484

8585

86-
_warnings_context = _contextvars.ContextVar('warnings_context')
86+
_warnings_context = _contextvars.ContextVar.thread_inheritable(
87+
'warnings_context')
8788

8889

8990
def _get_context():

Lib/_pydecimal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class FloatOperation(DecimalException, TypeError):
350350
import contextvars
351351
from _contextvars import _current_context_depth
352352

353-
_current_context_var = contextvars.ContextVar('decimal_context')
353+
_current_context_var = contextvars.thread_inheritable('decimal_context')
354354

355355
_context_attributes = frozenset(
356356
['prec', 'Emin', 'Emax', 'capitals', 'clamp', 'rounding', 'flags', 'traps']

Lib/test/test_warnings/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,12 +1807,11 @@ class PyAsyncTests(AsyncTests, unittest.TestCase):
18071807

18081808
class ThreadTests(BaseTest):
18091809
"""Verifies that the catch_warnings() context manager behaves as
1810-
expected when used within threads. This requires that both the
1811-
context_aware_warnings flag and thread_inherit_context flags are enabled.
1810+
expected when used within threads. This requires the
1811+
context_aware_warnings flag to be enabled.
18121812
"""
18131813

1814-
ENABLE_THREAD_TESTS = (sys.flags.context_aware_warnings and
1815-
sys.flags.thread_inherit_context)
1814+
ENABLE_THREAD_TESTS = sys.flags.context_aware_warnings
18161815

18171816
def setUp(self):
18181817
super().setUp()

Modules/_decimal/_decimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7931,7 +7931,7 @@ _decimal_exec(PyObject *m)
79317931
PyUnicode_FromString("___DECIMAL_CTX__"));
79327932
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_False));
79337933
#else
7934-
ASSIGN_PTR(state->current_context_var, PyContextVar_New("decimal_context", NULL));
7934+
ASSIGN_PTR(state->current_context_var, PyContextVar_NewThreadInheritable("decimal_context", NULL));
79357935
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_CONTEXTVAR", Py_True));
79367936
#endif
79377937
CHECK_INT(PyModule_AddObjectRef(m, "HAVE_THREADS", Py_True));

Python/_warnings.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ _PyWarnings_InitState(PyInterpreterState *interp)
158158
}
159159

160160
if (st->context == NULL) {
161-
st->context = PyContextVar_New("_warnings_context", NULL);
161+
st->context = PyContextVar_NewThreadInheritable(
162+
"_warnings_context", NULL);
162163
if (st->context == NULL) {
163164
return -1;
164165
}

0 commit comments

Comments
 (0)