mirror of
https://github.com/python/cpython
synced 2024-10-14 08:51:20 +00:00
bpo-41149: Fix a bug in threading that causes fals-y threads callables to fail to start. (GH-21201)
This commit is contained in:
parent
58fb156edd
commit
01c4fddc4b
|
@ -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):
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
Loading…
Reference in a new issue