Skip to content

Commit baf6934

Browse files
eacousineauwjakob
authored andcommitted
Minor modifications to interrupt handling FAQ (pybind#2007)
1 parent 0f1d3bf commit baf6934

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

docs/faq.rst

+4-12
Original file line numberDiff line numberDiff line change
@@ -257,30 +257,22 @@ is released, so a long-running function won't be interrupted.
257257
To interrupt from inside your function, you can use the ``PyErr_CheckSignals()``
258258
function, that will tell if a signal has been raised on the Python side. This
259259
function merely checks a flag, so its impact is negligible. When a signal has
260-
been received, you can explicitely interrupt execution by throwing an exception
261-
that gets translated to KeyboardInterrupt (see :doc:`advanced/exceptions`
262-
section):
260+
been received, you must either explicitly interrupt execution by throwing
261+
``py::error_already_set`` (which will propagate the existing
262+
``KeyboardInterrupt``), or clear the error (which you usually will not want):
263263

264264
.. code-block:: cpp
265265
266-
class interruption_error: public std::exception {
267-
public:
268-
const char* what() const noexcept {
269-
return "Interruption signal caught.";
270-
}
271-
};
272-
273266
PYBIND11_MODULE(example, m)
274267
{
275268
m.def("long running_func", []()
276269
{
277270
for (;;) {
278271
if (PyErr_CheckSignals() != 0)
279-
throw interruption_error();
272+
throw py::error_already_set();
280273
// Long running iteration
281274
}
282275
});
283-
py::register_exception<interruption_error>(m, "KeyboardInterrupt");
284276
}
285277
286278
Inconsistent detection of Python version in CMake and pybind11

0 commit comments

Comments
 (0)