diff --git a/docs/faq.rst b/docs/faq.rst index a7cbbfdf4..4d491fb87 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -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(m, "KeyboardInterrupt"); } Inconsistent detection of Python version in CMake and pybind11