Skip to content

Commit 227f4df

Browse files
Properly set LibsemigroupsErrors
1 parent 682fb61 commit 227f4df

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/errors.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff 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

tests/test_words.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,5 @@ def test_human_readable_letter():
348348

349349
def 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

0 commit comments

Comments
 (0)