diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 9e4972ecb640df..37c24dd5de8037 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -251,6 +251,14 @@ def f(mutex): #Issue 29376 self.assertTrue(threading._active[tid].is_alive()) self.assertRegex(repr(threading._active[tid]), '_DummyThread') + + # Issue gh-106236: + with self.assertRaises(AssertionError): + threading._active[tid].join() + threading._active[tid]._started.clear() + with self.assertRaises(AssertionError): + threading._active[tid].is_alive() + del threading._active[tid] # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) diff --git a/Lib/threading.py b/Lib/threading.py index df273870fa4273..4565fe8d0a0eec 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1451,11 +1451,12 @@ def _stop(self): pass def is_alive(self): - assert not self._is_stopped and self._started.is_set() - return True + if not self._is_stopped and self._started.is_set(): + return True + raise AssertionError("thread is not alive") def join(self, timeout=None): - assert False, "cannot join a dummy thread" + raise AssertionError("cannot join a dummy thread") # Global API functions diff --git a/Misc/NEWS.d/next/Library/2023-06-29-15-10-44.gh-issue-106236.EAIX4l.rst b/Misc/NEWS.d/next/Library/2023-06-29-15-10-44.gh-issue-106236.EAIX4l.rst new file mode 100644 index 00000000000000..3c50df6e7ed680 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-29-15-10-44.gh-issue-106236.EAIX4l.rst @@ -0,0 +1,3 @@ +Replace ``assert`` statements with ``raise AssertionError`` in +:mod:`threading`, so the behaviour of ``_DummyThread`` with and without +``-OO`` is consistent.