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

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()`` 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, 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 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 been received, you must either explicitly interrupt execution by throwing
that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions` ``py::error_already_set`` (which will propagate the existing
section): ``KeyboardInterrupt``), or clear the error (which you usually will not want):
.. code-block:: cpp .. code-block:: cpp
class interruption_error: public std::exception {
public:
const char* what() const noexcept {
return "Interruption signal caught.";
}
};
PYBIND11_MODULE(example, m) PYBIND11_MODULE(example, m)
{ {
m.def("long running_func", []() m.def("long running_func", []()
{ {
for (;;) { for (;;) {
if (PyErr_CheckSignals() != 0) if (PyErr_CheckSignals() != 0)
throw interruption_error(); throw py::error_already_set();
// Long running iteration // Long running iteration
} }
}); });
py::register_exception<interruption_error>(m, "KeyboardInterrupt");
} }
Inconsistent detection of Python version in CMake and pybind11 Inconsistent detection of Python version in CMake and pybind11