Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bffc94

Browse files
committed
Issue #22018: signal.set_wakeup_fd() now raises an OSError instead of a
ValueError on fstat() failure.
1 parent 38d773b commit 0bffc94

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

Lib/test/test_signal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ class WakeupFDTests(unittest.TestCase):
252252

253253
def test_invalid_fd(self):
254254
fd = support.make_bad_fd()
255-
self.assertRaises(ValueError, signal.set_wakeup_fd, fd)
255+
self.assertRaises(OSError, signal.set_wakeup_fd, fd)
256256

257257
def test_set_wakeup_fd_result(self):
258258
r1, w1 = os.pipe()
259-
os.close(r1)
259+
self.addCleanup(os.close, r1)
260260
self.addCleanup(os.close, w1)
261261
r2, w2 = os.pipe()
262-
os.close(r2)
262+
self.addCleanup(os.close, r2)
263263
self.addCleanup(os.close, w2)
264264

265265
signal.set_wakeup_fd(w1)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ Core and Builtins
108108
Library
109109
-------
110110

111+
- Issue #22018: signal.set_wakeup_fd() now raises an OSError instead of a
112+
ValueError on ``fstat()`` failure.
113+
111114
- Issue #21044: tarfile.open() now handles fileobj with an integer 'name'
112115
attribute. Based on patch by Martin Panter.
113116

Modules/signalmodule.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,20 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
437437
return NULL;
438438
}
439439
#endif
440-
if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
441-
PyErr_SetString(PyExc_ValueError, "invalid fd");
442-
return NULL;
440+
441+
if (fd != -1) {
442+
if (!_PyVerify_fd(fd)) {
443+
PyErr_SetString(PyExc_ValueError, "invalid fd");
444+
return NULL;
445+
}
446+
447+
if (fstat(fd, &buf) != 0)
448+
return PyErr_SetFromErrno(PyExc_OSError);
443449
}
450+
444451
old_fd = wakeup_fd;
445452
wakeup_fd = fd;
453+
446454
return PyLong_FromLong(old_fd);
447455
}
448456

0 commit comments

Comments
 (0)