Skip to content

Commit 57714d0

Browse files
committed
feat: Init _thread.interrupt_main
1 parent 4c49a55 commit 57714d0

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

vm/src/stdlib/signal.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,20 @@ 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+
run_signal(signum);
294+
Ok(())
295+
}
296+
}
297+
}
282298
}

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)