Minor modifications to interrupt handling FAQ (#2007)

This commit is contained in:
Eric Cousineau 2019-11-25 09:14:06 -05:00 committed by Wenzel Jakob
parent 0f1d3bfee2
commit baf69345f6
1 changed files with 4 additions and 12 deletions

View File

@ -257,30 +257,22 @@ is released, so a long-running function won't be interrupted.
To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
function, that will tell if a signal has been raised on the Python side. This
function merely checks a flag, so its impact is negligible. When a signal has
been received, you can explicitely interrupt execution by throwing an exception
that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions`
section):
been received, you must either explicitly interrupt execution by throwing
``py::error_already_set`` (which will propagate the existing
``KeyboardInterrupt``), or clear the error (which you usually will not want):
.. code-block:: cpp
class interruption_error: public std::exception {
public:
const char* what() const noexcept {
return "Interruption signal caught.";
}
};
PYBIND11_MODULE(example, m)
{
m.def("long running_func", []()
{
for (;;) {
if (PyErr_CheckSignals() != 0)
throw interruption_error();
throw py::error_already_set();
// Long running iteration
}
});
py::register_exception<interruption_error>(m, "KeyboardInterrupt");
}
Inconsistent detection of Python version in CMake and pybind11