[Python-checkins] bpo-41149: Fix a bug in threading that causes fals-y threads callables to fail to start. (GH-21201)

pablogsal webhook-mailer at python.org
Tue Feb 2 15:24:32 EST 2021


https://github.com/python/cpython/commit/01c4fddc4b2ac707f226e0bd92679588d2252cc4
commit: 01c4fddc4b2ac707f226e0bd92679588d2252cc4
branch: master
author: BarneyStratford <barney_stratford at fastmail.fm>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-02-02T20:24:24Z
summary:

bpo-41149: Fix a bug in threading that causes fals-y threads callables to fail to start. (GH-21201)

files:
A Misc/NEWS.d/next/Library/2020-06-28-16-13-02.bpo-41149.jiZWtJ.rst
M Lib/test/test_threading.py
M Lib/threading.py

diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 0a4372ec2df39..a7a716ed59fc4 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -855,6 +855,26 @@ def __del__(self):
         """)
         self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'")
 
+    def test_boolean_target(self):
+        # bpo-41149: A thread that had a boolean value of False would not
+        # run, regardless of whether it was callable. The correct behaviour
+        # is for a thread to do nothing if its target is None, and to call
+        # the target otherwise.
+        class BooleanTarget(object):
+            def __init__(self):
+                self.ran = False
+            def __bool__(self):
+                return False
+            def __call__(self):
+                self.ran = True
+
+        target = BooleanTarget()
+        thread = threading.Thread(target=target)
+        thread.start()
+        thread.join()
+        self.assertTrue(target.ran)
+
+
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
diff --git a/Lib/threading.py b/Lib/threading.py
index 7b3d63dd211ea..ff2624a3e1e49 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -906,7 +906,7 @@ def run(self):
 
         """
         try:
-            if self._target:
+            if self._target is not None:
                 self._target(*self._args, **self._kwargs)
         finally:
             # Avoid a refcycle if the thread is running a function with
diff --git a/Misc/NEWS.d/next/Library/2020-06-28-16-13-02.bpo-41149.jiZWtJ.rst b/Misc/NEWS.d/next/Library/2020-06-28-16-13-02.bpo-41149.jiZWtJ.rst
new file mode 100644
index 0000000000000..abe09016a6525
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-28-16-13-02.bpo-41149.jiZWtJ.rst
@@ -0,0 +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.



More information about the Python-checkins mailing list