Skip to content

Commit b1bd7f2

Browse files
authored
fix: define (non-empty) PYBIND11_EXPORT_EXCEPTION only under macOS. (#4298)
Background: #2999, #4105, #4283, #4284 In a nutshell: * Only macOS actually needs `PYBIND11_EXPORT_EXCEPTION` (#4284). * Evidently (#4283), under macOS `PYBIND11_EXPORT_EXCEPTION` does not run the risk of introducing ODR violations, * but evidently (#4283) under Linux it does, in the presumably rare/unusual situation that `RTLD_GLOBAL` is used. * Windows does no have the equivalent of `RTLD_GLOBAL`, therefore `PYBIND11_EXPORT_EXCEPTION` has no practical benefit, on the contrary, noisy warning suppression pragmas are needed, therefore it is best left empty.
1 parent 3a2c96b commit b1bd7f2

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

docs/advanced/exceptions.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ section.
177177
may be explicitly (re-)thrown to delegate it to the other,
178178
previously-declared existing exception translators.
179179

180-
Note that ``libc++`` and ``libstdc++`` `behave differently <https://stackoverflow.com/questions/19496643/using-clang-fvisibility-hidden-and-typeinfo-and-type-erasure/28827430>`_
181-
with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI boundaries need to be explicitly exported, as exercised in ``tests/test_exceptions.h``.
182-
See also: "Problems with C++ exceptions" under `GCC Wiki <https://gcc.gnu.org/wiki/Visibility>`_.
180+
Note that ``libc++`` and ``libstdc++`` `behave differently under macOS
181+
<https://stackoverflow.com/questions/19496643/using-clang-fvisibility-hidden-and-typeinfo-and-type-erasure/28827430>`_
182+
with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI
183+
boundaries need to be explicitly exported, as exercised in
184+
``tests/test_exceptions.h``. See also:
185+
"Problems with C++ exceptions" under `GCC Wiki <https://gcc.gnu.org/wiki/Visibility>`_.
183186

184187

185188
Local vs Global Exception Translators

include/pybind11/detail/common.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,10 @@
9696
#endif
9797

9898
#if !defined(PYBIND11_EXPORT_EXCEPTION)
99-
# ifdef __MINGW32__
100-
// workaround for:
101-
// error: 'dllexport' implies default visibility, but xxx has already been declared with a
102-
// different visibility
103-
# define PYBIND11_EXPORT_EXCEPTION
104-
# else
99+
# if defined(__apple_build_version__)
105100
# define PYBIND11_EXPORT_EXCEPTION PYBIND11_EXPORT
101+
# else
102+
# define PYBIND11_EXPORT_EXCEPTION
106103
# endif
107104
#endif
108105

@@ -904,22 +901,13 @@ using expand_side_effects = bool[];
904901

905902
PYBIND11_NAMESPACE_END(detail)
906903

907-
#if defined(_MSC_VER)
908-
# pragma warning(push)
909-
# pragma warning(disable : 4275)
910-
// warning C4275: An exported class was derived from a class that wasn't exported.
911-
// Can be ignored when derived from a STL class.
912-
#endif
913904
/// C++ bindings of builtin Python exceptions
914905
class PYBIND11_EXPORT_EXCEPTION builtin_exception : public std::runtime_error {
915906
public:
916907
using std::runtime_error::runtime_error;
917908
/// Set the error using the Python C API
918909
virtual void set_error() const = 0;
919910
};
920-
#if defined(_MSC_VER)
921-
# pragma warning(pop)
922-
#endif
923911

924912
#define PYBIND11_RUNTIME_EXCEPTION(name, type) \
925913
class PYBIND11_EXPORT_EXCEPTION name : public builtin_exception { \

include/pybind11/pytypes.h

-9
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,6 @@ inline std::string error_string() {
623623

624624
PYBIND11_NAMESPACE_END(detail)
625625

626-
#if defined(_MSC_VER)
627-
# pragma warning(push)
628-
# pragma warning(disable : 4275 4251)
629-
// warning C4275: An exported class was derived from a class that wasn't exported.
630-
// Can be ignored when derived from a STL class.
631-
#endif
632626
/// Fetch and hold an error which was already set in Python. An instance of this is typically
633627
/// thrown to propagate python-side errors back through C++ which can either be caught manually or
634628
/// else falls back to the function dispatcher (which then raises the captured error back to
@@ -688,9 +682,6 @@ class PYBIND11_EXPORT_EXCEPTION error_already_set : public std::exception {
688682
/// crashes (undefined behavior) if the Python interpreter is finalizing.
689683
static void m_fetched_error_deleter(detail::error_fetch_and_normalize *raw_ptr);
690684
};
691-
#if defined(_MSC_VER)
692-
# pragma warning(pop)
693-
#endif
694685

695686
/// Replaces the current Python error indicator with the chosen error, performing a
696687
/// 'raise from' to indicate that the chosen error was caused by the original error.

0 commit comments

Comments
 (0)