Skip to content

Commit 8cab405

Browse files
committed
more signal
1 parent a2bbc14 commit 8cab405

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

Lib/test/test_signal.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,11 @@ def test_invalid_call(self):
249249
with self.assertRaises(TypeError):
250250
signal.set_wakeup_fd(signal.SIGINT, False)
251251

252-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
253252
def test_invalid_fd(self):
254253
fd = os_helper.make_bad_fd()
255254
self.assertRaises((ValueError, OSError),
256255
signal.set_wakeup_fd, fd)
257256

258-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
259257
@unittest.skipUnless(support.has_socket_support, "needs working sockets.")
260258
def test_invalid_socket(self):
261259
sock = socket.socket()
@@ -264,7 +262,6 @@ def test_invalid_socket(self):
264262
self.assertRaises((ValueError, OSError),
265263
signal.set_wakeup_fd, fd)
266264

267-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
268265
# Emscripten does not support fstat on pipes yet.
269266
# https://github.com/emscripten-core/emscripten/issues/16414
270267
@unittest.skipIf(support.is_emscripten, "Emscripten cannot fstat pipes.")
@@ -286,7 +283,6 @@ def test_set_wakeup_fd_result(self):
286283
self.assertEqual(signal.set_wakeup_fd(-1), w2)
287284
self.assertEqual(signal.set_wakeup_fd(-1), -1)
288285

289-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
290286
@unittest.skipIf(support.is_emscripten, "Emscripten cannot fstat pipes.")
291287
@unittest.skipUnless(support.has_socket_support, "needs working sockets.")
292288
def test_set_wakeup_fd_socket_result(self):

crates/vm/src/stdlib/signal.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub(crate) mod _signal {
228228
}
229229

230230
#[pyfunction]
231-
fn set_wakeup_fd(args: SetWakeupFdArgs, vm: &VirtualMachine) -> PyResult<WakeupFdRaw> {
231+
fn set_wakeup_fd(args: SetWakeupFdArgs, vm: &VirtualMachine) -> PyResult<i64> {
232232
// TODO: implement warn_on_full_buffer
233233
let _ = args.warn_on_full_buffer;
234234
#[cfg(windows)]
@@ -264,6 +264,15 @@ pub(crate) mod _signal {
264264
if err.raw_os_error() != Some(WinSock::WSAENOTSOCK) {
265265
return Err(err.into_pyexception(vm));
266266
}
267+
// Validate that fd is a valid file descriptor using fstat
268+
// First check if SOCKET can be safely cast to i32 (file descriptor)
269+
let fd_i32 =
270+
i32::try_from(fd).map_err(|_| vm.new_value_error("invalid fd".to_owned()))?;
271+
// Verify the fd is valid by trying to fstat it
272+
let borrowed_fd =
273+
unsafe { crate::common::crt_fd::Borrowed::try_borrow_raw(fd_i32) }
274+
.map_err(|e| e.into_pyexception(vm))?;
275+
crate::common::fileutils::fstat(borrowed_fd).map_err(|e| e.into_pyexception(vm))?;
267276
}
268277
is_socket
269278
} else {
@@ -287,7 +296,18 @@ pub(crate) mod _signal {
287296
#[cfg(windows)]
288297
WAKEUP_IS_SOCKET.store(is_socket, Ordering::Relaxed);
289298

290-
Ok(old_fd)
299+
#[cfg(windows)]
300+
{
301+
if old_fd == INVALID_WAKEUP {
302+
Ok(-1)
303+
} else {
304+
Ok(old_fd as i64)
305+
}
306+
}
307+
#[cfg(not(windows))]
308+
{
309+
Ok(old_fd as i64)
310+
}
291311
}
292312

293313
#[cfg(all(unix, not(target_os = "redox")))]

0 commit comments

Comments
 (0)