cpython: d984dfe8c34e (original) (raw)
Mercurial > cpython
changeset 92251:d984dfe8c34e
Issue #22042: signal.set_wakeup_fd(fd) now raises an exception if the file descriptor is in blocking mode. [#22042]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Wed, 27 Aug 2014 12:59:44 +0200 |
parents | 6d8e01f2169c |
children | f5f5553f219e |
files | Lib/test/test_signal.py Misc/NEWS Modules/signalmodule.c |
diffstat | 3 files changed, 47 insertions(+), 1 deletions(-)[+] [-] Lib/test/test_signal.py 25 Misc/NEWS 3 Modules/signalmodule.c 20 |
line wrap: on
line diff
--- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -271,6 +271,9 @@ class WakeupFDTests(unittest.TestCase): self.addCleanup(os.close, r2) self.addCleanup(os.close, w2)
os.set_blocking(w1, False)[](#l1.7)
os.set_blocking(w2, False)[](#l1.8)
+ signal.set_wakeup_fd(w1) self.assertEqual(signal.set_wakeup_fd(w2), w1) self.assertEqual(signal.set_wakeup_fd(-1), w2) @@ -279,10 +282,12 @@ class WakeupFDTests(unittest.TestCase): def test_set_wakeup_fd_socket_result(self): sock1 = socket.socket() self.addCleanup(sock1.close)
sock1.setblocking(False)[](#l1.17) fd1 = sock1.fileno()[](#l1.18)
sock2 = socket.socket() self.addCleanup(sock2.close)
sock2.setblocking(False)[](#l1.22) fd2 = sock2.fileno()[](#l1.23)
signal.set_wakeup_fd(fd1) @@ -290,6 +295,26 @@ class WakeupFDTests(unittest.TestCase): self.assertEqual(signal.set_wakeup_fd(-1), fd2) self.assertEqual(signal.set_wakeup_fd(-1), -1)
On Windows, files are always blocking and Windows does not provide a
function to test if a socket is in non-blocking mode.
- @unittest.skipIf(sys.platform == "win32", "tests specific to POSIX")
- def test_set_wakeup_fd_blocking(self):
rfd, wfd = os.pipe()[](#l1.34)
self.addCleanup(os.close, rfd)[](#l1.35)
self.addCleanup(os.close, wfd)[](#l1.36)
# fd must be non-blocking[](#l1.38)
os.set_blocking(wfd, True)[](#l1.39)
with self.assertRaises(ValueError) as cm:[](#l1.40)
signal.set_wakeup_fd(wfd)[](#l1.41)
self.assertEqual(str(cm.exception),[](#l1.42)
"the fd %s must be in non-blocking mode" % wfd)[](#l1.43)
# non-blocking is ok[](#l1.45)
os.set_blocking(wfd, False)[](#l1.46)
signal.set_wakeup_fd(wfd)[](#l1.47)
signal.set_wakeup_fd(-1)[](#l1.48)
+ @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class WakeupSignalTests(unittest.TestCase):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -124,6 +124,9 @@ Core and Builtins Library ------- +- Issue #22042: signal.set_wakeup_fd(fd) now raises an exception if the file
--- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -561,9 +561,15 @@ signal_set_wakeup_fd(PyObject *self, PyO PyErr_SetFromErrno(PyExc_OSError); return NULL; } +
/* on Windows, a file cannot be set to non-blocking mode */[](#l3.8) }[](#l3.9)
else[](#l3.10)
else {[](#l3.11) is_socket = 1;[](#l3.12)
/* Windows does not provide a function to test if a socket[](#l3.14)
is in non-blocking mode */[](#l3.15)
} old_fd = wakeup.fd; @@ -576,6 +582,8 @@ signal_set_wakeup_fd(PyObject *self, PyO return PyLong_FromLong(-1);}[](#l3.16)
int blocking;[](#l3.24)
+ if (!_PyVerify_fd(fd)) { PyErr_SetString(PyExc_ValueError, "invalid fd"); return NULL; @@ -585,6 +593,16 @@ signal_set_wakeup_fd(PyObject *self, PyO PyErr_SetFromErrno(PyExc_OSError); return NULL; } +