Skip to content

Commit cef04e2

Browse files
committed
Use context=None to mean inherit.
This matches what asyncio-task does.
1 parent c6c454d commit cef04e2

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

Doc/library/threading.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ since it is impossible to detect the termination of alien threads.
334334

335335

336336
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
337-
daemon=None, context="inherit")
337+
daemon=None, context=None)
338338

339339
This constructor should always be called with keyword arguments. Arguments
340340
are:
@@ -361,7 +361,8 @@ since it is impossible to detect the termination of alien threads.
361361

362362
*context* is the `contextvars.Context` value to use while running the thread.
363363
The default is to inherit the context of the caller of :meth:`~Thread.start`.
364-
If set to ``None``, the context will be empty.
364+
To start with an empty context, pass a new instance of
365+
:class:`contextvars.Context()`
365366

366367
If the subclass overrides the constructor, it must make sure to invoke the
367368
base class constructor (``Thread.__init__()``) before doing anything else to

Lib/test/test_decimal.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import random
4545
import inspect
4646
import threading
47+
import contextvars
4748

4849

4950
if sys.platform == 'darwin':
@@ -1725,8 +1726,10 @@ def test_threading(self):
17251726
self.finish1 = threading.Event()
17261727
self.finish2 = threading.Event()
17271728

1728-
th1 = threading.Thread(target=thfunc1, args=(self,), context=None)
1729-
th2 = threading.Thread(target=thfunc2, args=(self,), context=None)
1729+
th1 = threading.Thread(target=thfunc1, args=(self,),
1730+
context=contextvars.Context())
1731+
th2 = threading.Thread(target=thfunc2, args=(self,),
1732+
context=contextvars.Context())
17301733

17311734
th1.start()
17321735
th2.start()

Lib/threading.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ class Thread:
873873
_initialized = False
874874

875875
def __init__(self, group=None, target=None, name=None,
876-
args=(), kwargs=None, *, daemon=None, context='inherit'):
876+
args=(), kwargs=None, *, daemon=None, context=None):
877877
"""This constructor should always be called with keyword arguments. Arguments are:
878878
879879
*group* should be None; reserved for future extension when a ThreadGroup
@@ -891,8 +891,8 @@ class is implemented.
891891
invocation. Defaults to {}.
892892
893893
*context* is the contextvars.Context value to use for the thread. The default
894-
is to inherit the context of the caller. Set to None to start with an empty
895-
context.
894+
is to inherit the context of the caller. To start with an empty context,
895+
pass a new instance of contextvars.Context().
896896
897897
If a subclass overrides the constructor, it must make sure to invoke
898898
the base class constructor (Thread.__init__()) before doing anything
@@ -980,7 +980,7 @@ def start(self):
980980
with _active_limbo_lock:
981981
_limbo[self] = self
982982

983-
if self._context == 'inherit':
983+
if self._context is None:
984984
# No context provided, inherit the context of the caller.
985985
self._context = _contextvars.copy_context()
986986

0 commit comments

Comments
 (0)