X Tutup
Skip to content

Commit 3e01e22

Browse files
committed
set_wakeup_fd
1 parent a2bbc14 commit 3e01e22

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Lib/test/test_regrtest.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,6 @@ def test_fromfile(self):
820820
output = self.run_tests('--fromfile', filename)
821821
self.check_executed_tests(output, tests)
822822

823-
# TODO: RUSTPYTHON
824-
@unittest.expectedFailure
825823
def test_interrupted(self):
826824
code = TEST_INTERRUPTED
827825
test = self.create_test('sigint', code=code)
@@ -839,8 +837,6 @@ def test_slowest(self):
839837
% (self.TESTNAME_REGEX, len(tests)))
840838
self.check_line(output, regex)
841839

842-
# TODO: RUSTPYTHON
843-
@unittest.expectedFailure
844840
def test_slowest_interrupted(self):
845841
# Issue #25373: test --slowest with an interrupted test
846842
code = TEST_INTERRUPTED

Lib/test/test_signal.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ def trivial_signal_handler(self, *args):
8282
def create_handler_with_partial(self, argument):
8383
return functools.partial(self.trivial_signal_handler, argument)
8484

85-
# TODO: RUSTPYTHON
86-
@unittest.expectedFailure
8785
def test_out_of_range_signal_number_raises_error(self):
8886
self.assertRaises(ValueError, signal.getsignal, 4242)
8987

@@ -126,8 +124,6 @@ def __repr__(self):
126124
self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
127125
self.assertEqual(0, argument.repr_count)
128126

129-
# TODO: RUSTPYTHON
130-
@unittest.expectedFailure
131127
def test_strsignal(self):
132128
self.assertIn("Interrupt", signal.strsignal(signal.SIGINT))
133129
self.assertIn("Terminated", signal.strsignal(signal.SIGTERM))
@@ -141,8 +137,6 @@ def test_interprocess_signal(self):
141137
script = os.path.join(dirname, 'signalinterproctester.py')
142138
assert_python_ok(script)
143139

144-
# TODO: RUSTPYTHON
145-
@unittest.expectedFailure
146140
@unittest.skipUnless(
147141
hasattr(signal, "valid_signals"),
148142
"requires signal.valid_signals"
@@ -249,13 +243,11 @@ def test_invalid_call(self):
249243
with self.assertRaises(TypeError):
250244
signal.set_wakeup_fd(signal.SIGINT, False)
251245

252-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
253246
def test_invalid_fd(self):
254247
fd = os_helper.make_bad_fd()
255248
self.assertRaises((ValueError, OSError),
256249
signal.set_wakeup_fd, fd)
257250

258-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
259251
@unittest.skipUnless(support.has_socket_support, "needs working sockets.")
260252
def test_invalid_socket(self):
261253
sock = socket.socket()
@@ -264,7 +256,6 @@ def test_invalid_socket(self):
264256
self.assertRaises((ValueError, OSError),
265257
signal.set_wakeup_fd, fd)
266258

267-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
268259
# Emscripten does not support fstat on pipes yet.
269260
# https://github.com/emscripten-core/emscripten/issues/16414
270261
@unittest.skipIf(support.is_emscripten, "Emscripten cannot fstat pipes.")
@@ -286,7 +277,6 @@ def test_set_wakeup_fd_result(self):
286277
self.assertEqual(signal.set_wakeup_fd(-1), w2)
287278
self.assertEqual(signal.set_wakeup_fd(-1), -1)
288279

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

crates/vm/src/stdlib/signal.rs

Lines changed: 23 additions & 4 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")))]
@@ -306,13 +326,12 @@ pub(crate) mod _signal {
306326
#[cfg(any(unix, windows))]
307327
#[pyfunction]
308328
fn raise_signal(signalnum: i32, vm: &VirtualMachine) -> PyResult<()> {
309-
use crate::convert::IntoPyException;
310-
311329
signal::assert_in_range(signalnum, vm)?;
312330

313331
// On Windows, only certain signals are supported
314332
#[cfg(windows)]
315333
{
334+
use crate::convert::IntoPyException;
316335
// Windows supports: SIGINT(2), SIGILL(4), SIGFPE(8), SIGSEGV(11), SIGTERM(15), SIGABRT(22)
317336
const VALID_SIGNALS: &[i32] = &[
318337
libc::SIGINT,

0 commit comments

Comments
 (0)
X Tutup