Skip to content

Commit 75b6e87

Browse files
committed
feat: Initial interrupt_main
1 parent 2b2ed5a commit 75b6e87

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

vm/src/stdlib/signal.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,21 @@ pub(crate) mod _signal {
279279
Err(vm.new_value_error("signal number out of range".to_owned()))
280280
}
281281
}
282+
283+
/// Similar to `PyErr_SetInterruptEx` in CPython
284+
///
285+
/// Missing signal handler for the given signal number is silently ignored.
286+
pub fn set_interrupt_ex(signum: i32, vm: &VirtualMachine) -> PyResult<()> {
287+
assert_in_range(signum, vm)?;
288+
289+
match signum as usize {
290+
SIG_DFL | SIG_IGN => Ok(()),
291+
_ => {
292+
// interrupt the main thread with given signal number
293+
//? Should follow with https://github.com/python/cpython/blob/b2afe482f21b826d53886a69ea2c99d0d940c59a/Modules/signalmodule.c#L301-L381
294+
//? or another way we can do in Rust?
295+
Ok(())
296+
}
297+
}
298+
}
282299
}

vm/src/stdlib/thread.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ pub(crate) mod _thread {
304304
vm.state.thread_count.fetch_sub(1);
305305
}
306306

307+
#[pyfunction]
308+
fn interrupt_main(signum: OptionalArg<i32>, vm: &VirtualMachine) -> PyResult<()> {
309+
crate::stdlib::signal::_signal::set_interrupt_ex(signum.unwrap_or(libc::SIGINT), vm)
310+
}
311+
307312
#[pyfunction]
308313
fn exit(vm: &VirtualMachine) -> PyResult {
309314
Err(vm.new_exception_empty(vm.ctx.exceptions.system_exit.to_owned()))

0 commit comments

Comments
 (0)