Skip to content

Commit

Permalink
pythongh-106236: Replace assert with raise RuntimeError in `threa…
Browse files Browse the repository at this point in the history
…ding.py` (python#106237)

Replace `assert` with `raise ` in `threading.py` so that -OO does not alter _DummyThread behavior.
  • Loading branch information
sobolevn committed Jul 12, 2023
1 parent dd1884d commit e4b88c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(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)
Expand Down
7 changes: 4 additions & 3 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Replace ``assert`` statements with ``raise RuntimeError`` in
:mod:`threading`, so that ``_DummyThread`` cannot be joined even with ``-OO``.

0 comments on commit e4b88c1

Please sign in to comment.