]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41149: Fix a bug in threading that causes fals-y threads callables to fail to...
authorBarneyStratford <barney_stratford@fastmail.fm>
Tue, 2 Feb 2021 20:24:24 +0000 (20:24 +0000)
committerGitHub <noreply@github.com>
Tue, 2 Feb 2021 20:24:24 +0000 (20:24 +0000)
Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS.d/next/Library/2020-06-28-16-13-02.bpo-41149.jiZWtJ.rst [new file with mode: 0644]

index 0a4372ec2df39fe9f737c12d27988a60761c0fa0..a7a716ed59fc4cdc721468f5e070130fb202aca3 100644 (file)
@@ -855,6 +855,26 @@ class ThreadTests(BaseTestCase):
         """)
         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):
 
index 7b3d63dd211ea49e06fed4c79d57533854b4457c..ff2624a3e1e49ebcb355cc2e1b74023924397e08 100644 (file)
@@ -906,7 +906,7 @@ class Thread:
 
         """
         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 (file)
index 0000000..abe0901
--- /dev/null
@@ -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.