Skip to content

Commit 9cb6f73

Browse files
committed
Merge branch 'thread_inherit_context' into gh-128384-warnings-contextvar
2 parents dba89e0 + 53eb72d commit 9cb6f73

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

Doc/library/decimal.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,13 +1884,20 @@ the current thread.
18841884

18851885
If :func:`setcontext` has not been called before :func:`getcontext`, then
18861886
:func:`getcontext` will automatically create a new context for use in the
1887-
current thread.
1888-
1889-
The new context is copied from a prototype context called *DefaultContext*. To
1890-
control the defaults so that each thread will use the same values throughout the
1891-
application, directly modify the *DefaultContext* object. This should be done
1892-
*before* any threads are started so that there won't be a race condition between
1893-
threads calling :func:`getcontext`. For example::
1887+
current thread. New context objects have default values set from the
1888+
:data:`decimal.DefaultContext` object.
1889+
1890+
The :data:`sys.flags.thread_inherit_context` flag affects the context for
1891+
new threads. If the flag is false, new threads will start with an empty
1892+
context. In this case, :func:`getcontext` will create a new context object
1893+
when called and use the default values from *DefaultContext*. If the flag
1894+
is true, new threads will start with a copy of context from the caller of
1895+
:meth:`Thread.start`.
1896+
1897+
To control the defaults so that each thread will use the same values throughout
1898+
the application, directly modify the *DefaultContext* object. This should be
1899+
done *before* any threads are started so that there won't be a race condition
1900+
between threads calling :func:`getcontext`. For example::
18941901

18951902
# Set applicationwide defaults for all threads about to be launched
18961903
DefaultContext.prec = 12

Lib/test/test_decimal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,9 @@ def test_threading(self):
17261726
self.finish1 = threading.Event()
17271727
self.finish2 = threading.Event()
17281728

1729+
# This test wants to start threads with an empty context, no matter
1730+
# the setting of sys.flags.thread_inherit_context. We pass the
1731+
# 'context' argument explicitly with an empty context instance.
17291732
th1 = threading.Thread(target=thfunc1, args=(self,),
17301733
context=contextvars.Context())
17311734
th2 = threading.Thread(target=thfunc2, args=(self,),

0 commit comments

Comments
 (0)