gh-106236: Replace assert with raise RuntimeError in threading.py (#106237)

Replace `assert` with `raise ` in `threading.py` so that -OO does not alter _DummyThread behavior.
This commit is contained in:
Nikita Sobolev 2023-07-12 21:07:59 +03:00 committed by GitHub
parent dd1884dc5d
commit e4b88c1e4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 3 deletions

View file

@ -251,6 +251,14 @@ class ThreadTests(BaseTestCase):
#Issue 29376
self.assertTrue(threading._active[tid].is_alive())
self.assertRegex(repr(threading._active[tid]), '_DummyThread')
# Issue gh-106236:
with self.assertRaises(RuntimeError):
threading._active[tid].join()
threading._active[tid]._started.clear()
with self.assertRaises(RuntimeError):
threading._active[tid].is_alive()
del threading._active[tid]
# PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)

View file

@ -1451,11 +1451,12 @@ class _DummyThread(Thread):
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 RuntimeError("thread is not alive")
def join(self, timeout=None):
assert False, "cannot join a dummy thread"
raise RuntimeError("cannot join a dummy thread")
# Global API functions

View file

@ -0,0 +1,2 @@
Replace ``assert`` statements with ``raise RuntimeError`` in
:mod:`threading`, so that ``_DummyThread`` cannot be joined even with ``-OO``.