]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106236: Replace `assert` with `raise RuntimeError` in `threading.py` (#106237)
authorNikita Sobolev <mail@sobolevn.me>
Wed, 12 Jul 2023 18:07:59 +0000 (21:07 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Jul 2023 18:07:59 +0000 (11:07 -0700)
Replace `assert` with `raise ` in `threading.py` so that -OO does not alter _DummyThread behavior.

Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS.d/next/Library/2023-06-29-15-10-44.gh-issue-106236.EAIX4l.rst [new file with mode: 0644]

index 9e4972ecb640df8e93504c942c23e04775836a2a..4a91eef31c4b8bf1109b49f514cd829c40180cc5 100644 (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)
index df273870fa42733d8d93c9914cd54607df1944d4..e036cb891e196d8662bdbef82dd9c0f68ceb5a0c 100644 (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
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 (file)
index 0000000..036bdb6
--- /dev/null
@@ -0,0 +1,2 @@
+Replace ``assert`` statements with ``raise RuntimeError`` in
+:mod:`threading`, so that ``_DummyThread`` cannot be joined even with ``-OO``.