@@ -257,30 +257,22 @@ is released, so a long-running function won't be interrupted.
257
257
To interrupt from inside your function, you can use the ``PyErr_CheckSignals() ``
258
258
function, that will tell if a signal has been raised on the Python side. This
259
259
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 ):
263
263
264
264
.. code-block :: cpp
265
265
266
- class interruption_error: public std::exception {
267
- public:
268
- const char* what() const noexcept {
269
- return "Interruption signal caught.";
270
- }
271
- };
272
-
273
266
PYBIND11_MODULE(example, m)
274
267
{
275
268
m.def("long running_func", []()
276
269
{
277
270
for (;;) {
278
271
if (PyErr_CheckSignals() != 0)
279
- throw interruption_error ();
272
+ throw py::error_already_set ();
280
273
// Long running iteration
281
274
}
282
275
});
283
- py::register_exception<interruption_error>(m, "KeyboardInterrupt");
284
276
}
285
277
286
278
Inconsistent detection of Python version in CMake and pybind11
0 commit comments