Skip to content

Commit 3cfab44

Browse files
authored
gh-132578: Rename the threading.Thread._handle field (#132696)
Commit `0e9c364f` introduced the `_handle` field on instances of `threading.Thread`. Unfortunately it's fairly common for subclasses of `threading.Thread` to define a `_handle()` method, which is shadowed by the new field.
1 parent 2b47f46 commit 3cfab44

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Lib/threading.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ class is implemented.
935935
self._ident = None
936936
if _HAVE_THREAD_NATIVE_ID:
937937
self._native_id = None
938-
self._handle = _ThreadHandle()
938+
self._os_thread_handle = _ThreadHandle()
939939
self._started = Event()
940940
self._initialized = True
941941
# Copy of sys.stderr used by self._invoke_excepthook()
@@ -950,7 +950,7 @@ def _after_fork(self, new_ident=None):
950950
if new_ident is not None:
951951
# This thread is alive.
952952
self._ident = new_ident
953-
assert self._handle.ident == new_ident
953+
assert self._os_thread_handle.ident == new_ident
954954
else:
955955
# Otherwise, the thread is dead, Jim. _PyThread_AfterFork()
956956
# already marked our handle done.
@@ -961,7 +961,7 @@ def __repr__(self):
961961
status = "initial"
962962
if self._started.is_set():
963963
status = "started"
964-
if self._handle.is_done():
964+
if self._os_thread_handle.is_done():
965965
status = "stopped"
966966
if self._daemonic:
967967
status += " daemon"
@@ -999,7 +999,7 @@ def start(self):
999999

10001000
try:
10011001
# Start joinable thread
1002-
_start_joinable_thread(self._bootstrap, handle=self._handle,
1002+
_start_joinable_thread(self._bootstrap, handle=self._os_thread_handle,
10031003
daemon=self.daemon)
10041004
except Exception:
10051005
with _active_limbo_lock:
@@ -1127,7 +1127,7 @@ def join(self, timeout=None):
11271127
if timeout is not None:
11281128
timeout = max(timeout, 0)
11291129

1130-
self._handle.join(timeout)
1130+
self._os_thread_handle.join(timeout)
11311131

11321132
@property
11331133
def name(self):
@@ -1180,7 +1180,7 @@ def is_alive(self):
11801180
11811181
"""
11821182
assert self._initialized, "Thread.__init__() not called"
1183-
return self._started.is_set() and not self._handle.is_done()
1183+
return self._started.is_set() and not self._os_thread_handle.is_done()
11841184

11851185
@property
11861186
def daemon(self):
@@ -1391,7 +1391,7 @@ def __init__(self):
13911391
Thread.__init__(self, name="MainThread", daemon=False)
13921392
self._started.set()
13931393
self._ident = _get_main_thread_ident()
1394-
self._handle = _make_thread_handle(self._ident)
1394+
self._os_thread_handle = _make_thread_handle(self._ident)
13951395
if _HAVE_THREAD_NATIVE_ID:
13961396
self._set_native_id()
13971397
with _active_limbo_lock:
@@ -1439,15 +1439,15 @@ def __init__(self):
14391439
daemon=_daemon_threads_allowed())
14401440
self._started.set()
14411441
self._set_ident()
1442-
self._handle = _make_thread_handle(self._ident)
1442+
self._os_thread_handle = _make_thread_handle(self._ident)
14431443
if _HAVE_THREAD_NATIVE_ID:
14441444
self._set_native_id()
14451445
with _active_limbo_lock:
14461446
_active[self._ident] = self
14471447
_DeleteDummyThreadOnDel(self)
14481448

14491449
def is_alive(self):
1450-
if not self._handle.is_done() and self._started.is_set():
1450+
if not self._os_thread_handle.is_done() and self._started.is_set():
14511451
return True
14521452
raise RuntimeError("thread is not alive")
14531453

@@ -1561,7 +1561,7 @@ def _shutdown():
15611561
# dubious, but some code does it. We can't wait for it to be marked as done
15621562
# normally - that won't happen until the interpreter is nearly dead. So
15631563
# mark it done here.
1564-
if _main_thread._handle.is_done() and _is_main_interpreter():
1564+
if _main_thread._os_thread_handle.is_done() and _is_main_interpreter():
15651565
# _shutdown() was already called
15661566
return
15671567

@@ -1574,7 +1574,7 @@ def _shutdown():
15741574
atexit_call()
15751575

15761576
if _is_main_interpreter():
1577-
_main_thread._handle._set_done()
1577+
_main_thread._os_thread_handle._set_done()
15781578

15791579
# Wait for all non-daemon threads to exit.
15801580
_thread_shutdown()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Rename the ``threading.Thread._handle`` field to avoid shadowing methods
2+
defined on subclasses of ``threading.Thread``.

0 commit comments

Comments
 (0)