File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -57,19 +57,23 @@ namespace libsemigroups {
5757 void init_error (py::module & m) {
5858 // TODO this doesn't seem to properly catch all LibsemigroupsExceptions,
5959 // particularly on macOS. This may have been resolved in pybind11 2.12.0
60- static py::exception<LibsemigroupsException> exc (
61- m, " LibsemigroupsError" , PyExc_RuntimeError);
60+
61+ // Using the GIL safe call below rather than simply having a static
62+ // py::exception is recommended in the pybind11 doc.
63+ PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object>
64+ exc_storage;
65+ exc_storage.call_once_and_store_result ([&]() {
66+ return py::exception<LibsemigroupsException>(
67+ m, " LibsemigroupsError" , PyExc_RuntimeError);
68+ });
6269 py::register_exception_translator ([](std::exception_ptr p) {
6370 try {
6471 if (p) {
6572 std::rethrow_exception (p);
6673 }
6774 } catch (LibsemigroupsException const & e) {
68- exc (formatted_error_message (e).c_str ());
69- } catch (py::stop_iteration const & e) {
70- throw e;
71- } catch (std::runtime_error const & e) {
72- exc (formatted_error_message (e).c_str ());
75+ py::set_error (exc_storage.get_stored (),
76+ formatted_error_message (e).c_str ());
7377 }
7478 });
7579 // TODO: Doc
Original file line number Diff line number Diff line change @@ -348,5 +348,5 @@ def test_human_readable_letter():
348348
349349def test_human_readable_index ():
350350 assert human_readable_index ("\377 " ) == 255
351- with pytest .raises (LibsemigroupsError ):
351+ with pytest .raises (ValueError ):
352352 human_readable_index ("Ā" ) # this is character 256
You can’t perform that action at this time.
0 commit comments