Skip to content

Commit 01c4fdd

Browse files
bpo-41149: Fix a bug in threading that causes fals-y threads callables to fail to start. (GH-21201)
1 parent 58fb156 commit 01c4fdd

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_threading.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,26 @@ def __del__(self):
855855
""")
856856
self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'")
857857

858+
def test_boolean_target(self):
859+
# bpo-41149: A thread that had a boolean value of False would not
860+
# run, regardless of whether it was callable. The correct behaviour
861+
# is for a thread to do nothing if its target is None, and to call
862+
# the target otherwise.
863+
class BooleanTarget(object):
864+
def __init__(self):
865+
self.ran = False
866+
def __bool__(self):
867+
return False
868+
def __call__(self):
869+
self.ran = True
870+
871+
target = BooleanTarget()
872+
thread = threading.Thread(target=target)
873+
thread.start()
874+
thread.join()
875+
self.assertTrue(target.ran)
876+
877+
858878

859879
class ThreadJoinOnShutdown(BaseTestCase):
860880

Lib/threading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ def run(self):
906906
907907
"""
908908
try:
909-
if self._target:
909+
if self._target is not None:
910910
self._target(*self._args, **self._kwargs)
911911
finally:
912912
# Avoid a refcycle if the thread is running a function with
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow executing callables that have a boolean value of ``False`` when passed to :class:`Threading.thread` as the target. Patch contributed by Barney Stratford.

0 commit comments

Comments
 (0)