diff -r 0ad7eb75152a Lib/test/test_signal.py --- a/Lib/test/test_signal.py Sun Jul 27 15:51:36 2014 +0200 +++ b/Lib/test/test_signal.py Sun Jul 27 16:08:38 2014 +0200 @@ -262,12 +262,31 @@ class WakeupFDTests(unittest.TestCase): r2, w2 = os.pipe() self.addCleanup(os.close, r2) self.addCleanup(os.close, w2) + os.set_blocking(w1, False) + os.set_blocking(w2, False) signal.set_wakeup_fd(w1) self.assertEqual(signal.set_wakeup_fd(w2), w1) self.assertEqual(signal.set_wakeup_fd(-1), w2) self.assertEqual(signal.set_wakeup_fd(-1), -1) + def test_set_wakeup_fd_blocking(self): + rfd, wfd = os.pipe() + self.addCleanup(os.close, rfd) + self.addCleanup(os.close, wfd) + + # fd must be non-blocking + os.set_blocking(wfd, True) + with self.assertRaises(ValueError) as cm: + signal.set_wakeup_fd(wfd) + self.assertEqual(str(cm.exception), + "the fd must be in non-blocking mode") + + # non-blocking is ok + os.set_blocking(wfd, False) + signal.set_wakeup_fd(wfd) + signal.set_wakeup_fd(-1) + @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class WakeupSignalTests(unittest.TestCase): @@ -328,7 +347,7 @@ class WakeupSignalTests(unittest.TestCas signal.signal(signal.SIGALRM, handler) r, w = os.pipe() - os.set_blocking(w, False) + os.set_blocking(r, False) # Set wakeup_fd a read-only file descriptor to trigger the error signal.set_wakeup_fd(r) diff -r 0ad7eb75152a Modules/signalmodule.c --- a/Modules/signalmodule.c Sun Jul 27 15:51:36 2014 +0200 +++ b/Modules/signalmodule.c Sun Jul 27 16:08:38 2014 +0200 @@ -439,6 +439,8 @@ signal_set_wakeup_fd(PyObject *self, PyO #endif if (fd != -1) { + int blocking; + if (!_PyVerify_fd(fd)) { PyErr_SetString(PyExc_ValueError, "invalid fd"); return NULL; @@ -446,6 +448,17 @@ signal_set_wakeup_fd(PyObject *self, PyO if (fstat(fd, &buf) != 0) return PyErr_SetFromErrno(PyExc_OSError); + +#ifndef MS_WINDOWS + blocking = _Py_get_blocking(fd); + if (blocking < 0) + return NULL; +#endif + if (blocking) { + PyErr_SetString(PyExc_ValueError, + "the fd must be in non-blocking mode"); + return NULL; + } } old_fd = wakeup_fd;