Skip to content

Add support for nested C++11 exceptions #3608

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

Merged
merged 21 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
# define PYBIND11_CPP14
# if __cplusplus >= 201703L
# define PYBIND11_CPP17
# if __cplusplus >= 202002L
# define PYBIND11_CPP20
# endif
# endif
# endif
#elif defined(_MSC_VER) && __cplusplus == 199711L
Expand All @@ -45,6 +48,9 @@
# define PYBIND11_CPP14
# if _MSVC_LANG > 201402L && _MSC_VER >= 1910
# define PYBIND11_CPP17
# if _MSVC_LANG > 202002L
# define PYBIND11_CPP20
# endif
# endif
# endif
#endif
Expand Down Expand Up @@ -612,6 +618,18 @@ template <typename T> using remove_cv_t = typename std::remove_cv<T>::type;
template <typename T> using remove_reference_t = typename std::remove_reference<T>::type;
#endif

#if defined(PYBIND11_CPP20)
using std::remove_cvref;
using std::remove_cvref_t;
#else
template <class T>
struct remove_cvref {
using type = remove_cv_t<remove_reference_t<T>>;
};
template <class T>
using remove_cvref_t = typename remove_cvref<T>::type;
#endif

/// Index sequences
#if defined(PYBIND11_CPP14)
using std::index_sequence;
Expand Down
22 changes: 9 additions & 13 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,8 @@ inline bool raise_err(PyObject *exc_type, const char *msg) {
// forward decl
inline void translate_exception(std::exception_ptr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forward declarations should never have inline:

https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions

(I find this highly counter-intuitive myself.)

Copy link
Collaborator

@henryiii henryiii Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C99 requires the inline on the definition; are you sure C++ cares? Looking at the spec, it seems clearly allowed unless it's nested inside another function https://en.cppreference.com/w/cpp/language/inline & https://en.cppreference.com/w/cpp/language/declarations#Specifiers seem to indicate it's allowed, and I didn't have any problems on any complier I quickly checked on godbolt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C99 requires the inline on the definition;

Ouch.

My previous comment was based on what I remembered from working on
#3179.

From memory, in a Google-internal chat I was advised to not use inline in forward declarations.

Am I sure? No. I was asking for advice and then going with that. The work on PR #3179 left me believing that even compilers are unusually confused about what is correct.

To be pragmatic, we can reduce it to: do we want to be consistent within pybind11? PR #3179 removed some inline based on the advice I got, and to be consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwgk Other forward declare (line 40) still use inline.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally kept #3179 limited to functions involving PYBIND11_NOINLINE. I didn't look around too much, as I very often do, to not let a good project die the death of a thousand cuts.

For this PR, idk. If we're inconsistent already and the advice I got in Aug last year isn't universally accepted (as I was thinking until now) ... shrug :-)


template <
class T,
enable_if_t<std::is_same<std::nested_exception, remove_cv_t<remove_reference_t<T>>>::value,
int> = 0>
template <class T,
enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
std::exception_ptr nested = nullptr;
nested = exc.nested_ptr();
Expand All @@ -309,29 +307,27 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
return false;
}

template <
class T,
enable_if_t<!std::is_same<std::nested_exception, remove_cv_t<remove_reference_t<T>>>::value,
int> = 0>
template <class T,
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
return handle_nested_exception(*nep, p);
}
return false;
}

#else
template <class T>
bool handle_nested_exception(T, std::exception_ptr) {
inline bool raise_err(PyObject *exc_type, const char *msg) {
PyErr_SetString(exc_type, msg);
return false;
}

inline bool raise_err(PyObject *exc_type, const char *msg) {
PyErr_SetString(exc_type, msg);
template <class T>
bool handle_nested_exception(const T&, std::exception_ptr) {
return false;
}
#endif


inline void translate_exception(std::exception_ptr p) {
if (!p) {
return;
Expand Down