Skip to content

Commit

Permalink
Add wrapper for PyErr_CheckSignals() to Python.
Browse files Browse the repository at this point in the history
This is a useful API in long-running Rust code, which lets users
cancel evaluation by pressing Ctrl-C, and run any other signal
handlers that have been set using the signal module.
  • Loading branch information
birkenfeld committed Oct 7, 2020
1 parent 37ce406 commit 214dd69
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,21 @@ impl<'p> Python<'p> {
pub fn xdecref<T: IntoPyPointer>(self, ptr: T) {
unsafe { ffi::Py_XDECREF(ptr.into_ptr()) };
}

/// Lets the Python interpreter check for pending signals and invoke the
/// corresponding signal handlers. This can run arbitrary Python code.
///
/// If an exception is raised by the signal handler, or the default signal
/// handler raises an exception (such as `KeyboardInterrupt` for `SIGINT`),
/// an `Err` is returned.
pub fn check_signals(self) -> PyResult<()> {
let v = unsafe { ffi::PyErr_CheckSignals() };
if v == -1 {
Err(PyErr::fetch(self))
} else {
Ok(())
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 214dd69

Please sign in to comment.