Skip to content

Commit e4b88c1

Browse files
authored
pythongh-106236: Replace assert with raise RuntimeError in threading.py (python#106237)
Replace `assert` with `raise ` in `threading.py` so that -OO does not alter _DummyThread behavior.
1 parent dd1884d commit e4b88c1

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/test/test_threading.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ def f(mutex):
251251
#Issue 29376
252252
self.assertTrue(threading._active[tid].is_alive())
253253
self.assertRegex(repr(threading._active[tid]), '_DummyThread')
254+
255+
# Issue gh-106236:
256+
with self.assertRaises(RuntimeError):
257+
threading._active[tid].join()
258+
threading._active[tid]._started.clear()
259+
with self.assertRaises(RuntimeError):
260+
threading._active[tid].is_alive()
261+
254262
del threading._active[tid]
255263

256264
# PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)

Lib/threading.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,11 +1451,12 @@ def _stop(self):
14511451
pass
14521452

14531453
def is_alive(self):
1454-
assert not self._is_stopped and self._started.is_set()
1455-
return True
1454+
if not self._is_stopped and self._started.is_set():
1455+
return True
1456+
raise RuntimeError("thread is not alive")
14561457

14571458
def join(self, timeout=None):
1458-
assert False, "cannot join a dummy thread"
1459+
raise RuntimeError("cannot join a dummy thread")
14591460

14601461

14611462
# Global API functions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Replace ``assert`` statements with ``raise RuntimeError`` in
2+
:mod:`threading`, so that ``_DummyThread`` cannot be joined even with ``-OO``.

0 commit comments

Comments
 (0)