diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-05-24 02:22:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-24 02:22:38 -0700 |
commit | 310f414bbd4d6ed1d8813f724c91ce9b4129c0ba (patch) | |
tree | bca2f14b6b451d2300e1a5b5e87785eb83f8cbcc /Lib/test | |
parent | a3488e5902f5c26e5cc289aec2518e7b5058e5d1 (diff) | |
download | cpython-git-310f414bbd4d6ed1d8813f724c91ce9b4129c0ba.tar.gz |
bpo-23395: Fix PyErr_SetInterrupt if the SIGINT signal is ignored or not handled (GH-7778)
``_thread.interrupt_main()`` now avoids setting the Python error status if the ``SIGINT`` signal is ignored or not handled by Python.
(cherry picked from commit 608876b6b1eb59538e6c29671a733033fb8b5be7)
Co-authored-by: Matěj Cepl <mcepl@cepl.eu>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_threading.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 27f328dbe6..aa810bda1c 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -16,6 +16,7 @@ import unittest import weakref import os import subprocess +import signal from test import lock_tests from test import support @@ -1165,6 +1166,7 @@ class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests): class BarrierTests(lock_tests.BarrierTests): barriertype = staticmethod(threading.Barrier) + class MiscTestCase(unittest.TestCase): def test__all__(self): extra = {"ThreadError"} @@ -1172,5 +1174,38 @@ class MiscTestCase(unittest.TestCase): support.check__all__(self, threading, ('threading', '_thread'), extra=extra, blacklist=blacklist) + +class InterruptMainTests(unittest.TestCase): + def test_interrupt_main_subthread(self): + # Calling start_new_thread with a function that executes interrupt_main + # should raise KeyboardInterrupt upon completion. + def call_interrupt(): + _thread.interrupt_main() + t = threading.Thread(target=call_interrupt) + with self.assertRaises(KeyboardInterrupt): + t.start() + t.join() + t.join() + + def test_interrupt_main_mainthread(self): + # Make sure that if interrupt_main is called in main thread that + # KeyboardInterrupt is raised instantly. + with self.assertRaises(KeyboardInterrupt): + _thread.interrupt_main() + + def test_interrupt_main_noerror(self): + handler = signal.getsignal(signal.SIGINT) + try: + # No exception should arise. + signal.signal(signal.SIGINT, signal.SIG_IGN) + _thread.interrupt_main() + + signal.signal(signal.SIGINT, signal.SIG_DFL) + _thread.interrupt_main() + finally: + # Restore original handler + signal.signal(signal.SIGINT, handler) + + if __name__ == "__main__": unittest.main() |