Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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``.