cpython: 6b536f0516ea (original) (raw)
Mercurial > cpython
changeset 91742:6b536f0516ea
Issue #22018: Add _testcapi.raise_signal() - Use _testcapi.raise_signal() in test_signal - close also os.pipe() file descriptors in some test_signal tests where they were not closed properly - Remove faulthandler._sigill() and faulthandler._sigbus(): reuse _testcapi.raise_signal() in test_faulthandler [#22018]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Mon, 21 Jul 2014 12:30:22 +0200 |
parents | 6fcdeea47cea |
children | 057a8bd043ea |
files | Lib/test/test_faulthandler.py Lib/test/test_signal.py Modules/_testcapimodule.c Modules/faulthandler.c |
diffstat | 4 files changed, 85 insertions(+), 53 deletions(-)[+] [-] Lib/test/test_faulthandler.py 26 Lib/test/test_signal.py 65 Modules/_testcapimodule.c 21 Modules/faulthandler.c 26 |
line wrap: on
line diff
--- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -16,6 +16,10 @@ try: HAVE_THREADS = True except ImportError: HAVE_THREADS = False +try:
TIMEOUT = 0.5 @@ -133,26 +137,32 @@ faulthandler._sigfpe() 3, 'Floating point exception')
- @unittest.skipIf(_testcapi is None, 'need _testcapi')
- @unittest.skipUnless(hasattr(signal, 'SIGBUS'), 'need signal.SIGBUS') def test_sigbus(self): self.check_fatal_error("""
+import _testcapi import faulthandler +import signal + faulthandler.enable() -faulthandler._sigbus() +_testcapi.raise_signal(signal.SIGBUS) """.strip(),
3,[](#l1.32)
6,[](#l1.33) 'Bus error')[](#l1.34)
- @unittest.skipIf(_testcapi is None, 'need _testcapi')
- @unittest.skipUnless(hasattr(signal, 'SIGILL'), 'need signal.SIGILL') def test_sigill(self): self.check_fatal_error("""
+import _testcapi import faulthandler +import signal + faulthandler.enable() -faulthandler._sigill() +_testcapi.raise_signal(signal.SIGILL) """.strip(),
3,[](#l1.50)
6,[](#l1.51) 'Illegal instruction')[](#l1.52)
--- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -15,6 +15,10 @@ try: import threading except ImportError: threading = None +try:
class HandlerBCalled(Exception): @@ -250,12 +254,27 @@ class WakeupFDTests(unittest.TestCase): fd = support.make_bad_fd() self.assertRaises(ValueError, signal.set_wakeup_fd, fd)
- def test_set_wakeup_fd_result(self):
r1, w1 = os.pipe()[](#l2.19)
os.close(r1)[](#l2.20)
self.addCleanup(os.close, w1)[](#l2.21)
r2, w2 = os.pipe()[](#l2.22)
os.close(r2)[](#l2.23)
self.addCleanup(os.close, w2)[](#l2.24)
signal.set_wakeup_fd(w1)[](#l2.26)
self.assertIs(signal.set_wakeup_fd(w2), w1)[](#l2.27)
self.assertIs(signal.set_wakeup_fd(-1), w2)[](#l2.28)
self.assertIs(signal.set_wakeup_fd(-1), -1)[](#l2.29)
+ @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class WakeupSignalTests(unittest.TestCase):
- @unittest.skipIf(_testcapi is None, 'need _testcapi') def check_wakeup(self, test_body, *signals, ordered=True): # use a subprocess to have only one thread code = """if 1:
import _testcapi[](#l2.38) import fcntl[](#l2.39) import os[](#l2.40) import signal[](#l2.41)
@@ -294,17 +313,18 @@ class WakeupSignalTests(unittest.TestCas assert_python_ok('-c', code)
- @unittest.skipIf(_testcapi is None, 'need _testcapi') def test_wakeup_write_error(self): # Issue #16105: write() errors in the C signal handler should not # pass silently. # Use a subprocess to have only one thread. code = """if 1:
import _testcapi[](#l2.52) import errno[](#l2.53) import fcntl[](#l2.54) import os[](#l2.55) import signal[](#l2.56) import sys[](#l2.57)
import time[](#l2.58) from test.support import captured_stderr[](#l2.59)
def handler(signum, frame): @@ -319,8 +339,7 @@ class WakeupSignalTests(unittest.TestCas signal.set_wakeup_fd(r) try: with captured_stderr() as err:
signal.alarm(1)[](#l2.66)
time.sleep(5.0)[](#l2.67)
_testcapi.raise_signal(signal.SIGALRM)[](#l2.68) except ZeroDivisionError:[](#l2.69) # An ignored exception should have been printed out on stderr[](#l2.70) err = err.getvalue()[](#l2.71)
@@ -331,6 +350,9 @@ class WakeupSignalTests(unittest.TestCas raise AssertionError(err) else: raise AssertionError("ZeroDivisionError not raised") +
os.close(r)[](#l2.77)
os.close(w)[](#l2.78) """[](#l2.79) r, w = os.pipe()[](#l2.80) try:[](#l2.81)
@@ -394,9 +416,10 @@ class WakeupSignalTests(unittest.TestCas def test_signum(self): self.check_wakeup("""def test():
import _testcapi[](#l2.86) signal.signal(signal.SIGUSR1, handler)[](#l2.87)
os.kill(os.getpid(), signal.SIGUSR1)[](#l2.88)
os.kill(os.getpid(), signal.SIGALRM)[](#l2.89)
_testcapi.raise_signal(signal.SIGUSR1)[](#l2.90)
_testcapi.raise_signal(signal.SIGALRM)[](#l2.91) """, signal.SIGUSR1, signal.SIGALRM)[](#l2.92)
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), @@ -410,8 +433,8 @@ class WakeupSignalTests(unittest.TestCas signal.signal(signum2, handler) signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2))
os.kill(os.getpid(), signum1)[](#l2.99)
os.kill(os.getpid(), signum2)[](#l2.100)
_testcapi.raise_signal(signum1)[](#l2.101)
_testcapi.raise_signal(signum2)[](#l2.102) # Unblocking the 2 signals calls the C signal handler twice[](#l2.103) signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2))[](#l2.104) """, signal.SIGUSR1, signal.SIGUSR2, ordered=False)[](#l2.105)
@@ -447,18 +470,22 @@ class SiginterruptTest(unittest.TestCase sys.stdout.flush() # run the test twice
for loop in range(2):[](#l2.110)
# send a SIGALRM in a second (during the read)[](#l2.111)
signal.alarm(1)[](#l2.112)
try:[](#l2.113)
# blocking call: read from a pipe without data[](#l2.114)
os.read(r, 1)[](#l2.115)
except OSError as err:[](#l2.116)
if err.errno != errno.EINTR:[](#l2.117)
raise[](#l2.118)
else:[](#l2.119)
sys.exit(2)[](#l2.120)
sys.exit(3)[](#l2.121)
try:[](#l2.122)
for loop in range(2):[](#l2.123)
# send a SIGALRM in a second (during the read)[](#l2.124)
signal.alarm(1)[](#l2.125)
try:[](#l2.126)
# blocking call: read from a pipe without data[](#l2.127)
os.read(r, 1)[](#l2.128)
except OSError as err:[](#l2.129)
if err.errno != errno.EINTR:[](#l2.130)
raise[](#l2.131)
else:[](#l2.132)
sys.exit(2)[](#l2.133)
sys.exit(3)[](#l2.134)
finally:[](#l2.135)
os.close(r)[](#l2.136)
os.close(w)[](#l2.137) """ % (interrupt,)[](#l2.138) with spawn_python('-c', code) as process:[](#l2.139) try:[](#l2.140)
--- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -11,6 +11,7 @@ #include <float.h> #include "structmember.h" #include "datetime.h" +#include <signal.h> #ifdef WITH_THREAD #include "pythread.h" @@ -3063,6 +3064,24 @@ exit: } #endif /* WITH_THREAD / +static PyObject +test_raise_signal(PyObject* self, PyObject *args) +{
+} + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, @@ -3198,6 +3217,8 @@ static PyMethodDef TestMethods[] = { {"docstring_with_signature_with_defaults", (PyCFunction)test_with_docstring, METH_NOARGS, docstring_with_signature_with_defaults},
#ifdef WITH_THREAD {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, PyDoc_STR("set_error_class(error_class) -> None")},
--- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -874,24 +874,6 @@ faulthandler_sigabrt(PyObject *self, PyO Py_RETURN_NONE; } -#ifdef SIGBUS -static PyObject * -faulthandler_sigbus(PyObject *self, PyObject *args) -{
-} -#endif - -#ifdef SIGILL -static PyObject * -faulthandler_sigill(PyObject *self, PyObject *args) -{
-} -#endif - static PyObject * faulthandler_fatal_error_py(PyObject *self, PyObject *args) { @@ -1012,14 +994,6 @@ static PyMethodDef module_methods[] = { PyDoc_STR("_sigabrt(): raise a SIGABRT signal")}, {"_sigfpe", (PyCFunction)faulthandler_sigfpe, METH_NOARGS, PyDoc_STR("_sigfpe(): raise a SIGFPE signal")}, -#ifdef SIGBUS
- {"_sigbus", (PyCFunction)faulthandler_sigbus, METH_NOARGS,
PyDoc_STR("_sigbus(): raise a SIGBUS signal")},[](#l4.34)
- {"_sigill", (PyCFunction)faulthandler_sigill, METH_NOARGS,
PyDoc_STR("_sigill(): raise a SIGILL signal")},[](#l4.38)
-#endif {"_fatal_error", faulthandler_fatal_error_py, METH_VARARGS, PyDoc_STR("_fatal_error(message): call Py_FatalError(message)")}, #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION)