Skip to content

py::register_exception_translator with const & std::exception_ptr #3095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct internals {
std::unordered_set<std::pair<const PyObject *, const char *>, override_hash> inactive_override_cache;
type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
std::unordered_map<const PyObject *, std::vector<PyObject *>> patients;
std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
std::forward_list<void (*)(const std::exception_ptr &)> registered_exception_translators;
std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
std::vector<PyObject *> loader_patient_stack; // Used by `loader_life_support`
std::forward_list<std::string> static_strings; // Stores the std::strings backing detail::c_str()
Expand Down Expand Up @@ -223,7 +223,7 @@ inline internals **&get_internals_pp() {
return internals_pp;
}

inline void translate_exception(std::exception_ptr p) {
inline void translate_exception(const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (error_already_set &e) { e.restore(); return;
Expand All @@ -243,7 +243,7 @@ inline void translate_exception(std::exception_ptr p) {
}

#if !defined(__GLIBCXX__)
inline void translate_local_exception(std::exception_ptr p) {
inline void translate_local_exception(const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (error_already_set &e) { e.restore(); return;
Expand Down
13 changes: 8 additions & 5 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -2000,10 +2000,13 @@ template <typename InputType, typename OutputType> void implicitly_convertible()
pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
}

template <typename ExceptionTranslator>
void register_exception_translator(ExceptionTranslator&& translator) {
detail::get_internals().registered_exception_translators.push_front(
std::forward<ExceptionTranslator>(translator));
inline void register_exception_translator(void (*translator)(const std::exception_ptr &)) {
detail::get_internals().registered_exception_translators.push_front(translator);
}

inline void register_exception_translator(void (*)(std::exception_ptr)) {
pybind11_fail("Please update your exception translator.");
// or change internals even more
}

/**
Expand Down Expand Up @@ -2054,7 +2057,7 @@ exception<CppException> &register_exception(handle scope,
auto &ex = detail::get_exception_object<CppException>();
if (!ex) ex = exception<CppException>(scope, name, base);

register_exception_translator([](std::exception_ptr p) {
register_exception_translator([](const std::exception_ptr &p) {
if (!p) return;
try {
std::rethrow_exception(p);
Expand Down
2 changes: 1 addition & 1 deletion tests/pybind11_cross_module_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PYBIND11_MODULE(pybind11_cross_module_tests, m) {
m.def("throw_pybind_value_error", []() { throw py::value_error("pybind11 value error"); });
m.def("throw_pybind_type_error", []() { throw py::type_error("pybind11 type error"); });
m.def("throw_stop_iteration", []() { throw py::stop_iteration(); });
py::register_exception_translator([](std::exception_ptr p) {
py::register_exception_translator([](const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (const shared_exception &e) {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST_SUBMODULE(exceptions, m) {

// make a new custom exception and use it as a translation target
static py::exception<MyException> ex(m, "MyException");
py::register_exception_translator([](std::exception_ptr p) {
py::register_exception_translator([](const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (const MyException &e) {
Expand All @@ -113,7 +113,7 @@ TEST_SUBMODULE(exceptions, m) {
// register new translator for MyException2
// no need to store anything here because this type will
// never by visible from Python
py::register_exception_translator([](std::exception_ptr p) {
py::register_exception_translator([](const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (const MyException2 &e) {
Expand All @@ -125,7 +125,7 @@ TEST_SUBMODULE(exceptions, m) {
// register new translator for MyException4
// which will catch it and delegate to the previously registered
// translator for MyException by throwing a new exception
py::register_exception_translator([](std::exception_ptr p) {
py::register_exception_translator([](const std::exception_ptr &p) {
try {
if (p) std::rethrow_exception(p);
} catch (const MyException4 &e) {
Expand Down