From 08c26967fdab39038af91b52427b7978f019a50e Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Thu, 18 Apr 2019 20:15:29 -0700 Subject: [PATCH 1/3] Mark exceptions with export to ensure throw/catch works between different client libraries --- include/pybind11/detail/common.h | 4 ++-- include/pybind11/pytypes.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 5ff74856b1..33e765feec 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -654,7 +654,7 @@ using expand_side_effects = bool[]; NAMESPACE_END(detail) /// C++ bindings of builtin Python exceptions -class builtin_exception : public std::runtime_error { +class PYBIND11_EXPORT builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API @@ -662,7 +662,7 @@ class builtin_exception : public std::runtime_error { }; #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ - class name : public builtin_exception { public: \ + class PYBIND11_EXPORT name : public builtin_exception { public: \ using builtin_exception::builtin_exception; \ name() : name("") { } \ void set_error() const override { PyErr_SetString(type, what()); } \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 3329fda2d4..458a2f6fae 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -319,7 +319,7 @@ NAMESPACE_END(detail) /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). -class error_already_set : public std::runtime_error { +class PYBIND11_EXPORT error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current /// Python error indicator will be cleared. From abca20bd839d613a77f8f89873c5ae7683a78a3f Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Apr 2019 11:14:32 -0700 Subject: [PATCH 2/3] Export py::object and all base types --- include/pybind11/pytypes.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 458a2f6fae..ea5274a9cf 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -43,7 +43,7 @@ using list_accessor = accessor; using tuple_accessor = accessor; /// Tag and check to identify a class which implements the Python object API -class pyobject_tag { }; +class PYBIND11_EXPORT pyobject_tag { }; template using is_pyobject = std::is_base_of>; /** \rst @@ -51,7 +51,7 @@ template using is_pyobject = std::is_base_of -class object_api : public pyobject_tag { +class PYBIND11_EXPORT object_api : public pyobject_tag { const Derived &derived() const { return static_cast(*this); } public: @@ -171,7 +171,7 @@ NAMESPACE_END(detail) The `object` class inherits from `handle` and adds automatic reference counting features. \endrst */ -class handle : public detail::object_api { +class PYBIND11_EXPORT handle : public detail::object_api { public: /// The default constructor creates a handle with a ``nullptr``-valued pointer handle() = default; @@ -227,7 +227,7 @@ class handle : public detail::object_api { scope and is destructed. When using `object` instances consistently, it is much easier to get reference counting right at the first attempt. \endrst */ -class object : public handle { +class PYBIND11_EXPORT object : public handle { public: object() = default; PYBIND11_DEPRECATED("Use reinterpret_borrow() or reinterpret_steal()") From d3512971b75d370eedcf74b58332f457517b98d0 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Fri, 19 Apr 2019 11:19:58 -0700 Subject: [PATCH 3/3] Disable warning C4275 in MSVC --- include/pybind11/detail/common.h | 8 ++++++++ include/pybind11/pytypes.h | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 33e765feec..17d22be867 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -654,12 +654,20 @@ using expand_side_effects = bool[]; NAMESPACE_END(detail) /// C++ bindings of builtin Python exceptions +// +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported +#endif class PYBIND11_EXPORT builtin_exception : public std::runtime_error { public: using std::runtime_error::runtime_error; /// Set the error using the Python C API virtual void set_error() const = 0; }; +#if defined(_MSC_VER) +# pragma warning(pop) +#endif #define PYBIND11_RUNTIME_EXCEPTION(name, type) \ class PYBIND11_EXPORT name : public builtin_exception { public: \ diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index ea5274a9cf..4815defc30 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -319,6 +319,10 @@ NAMESPACE_END(detail) /// thrown to propagate python-side errors back through C++ which can either be caught manually or /// else falls back to the function dispatcher (which then raises the captured error back to /// python). +#if defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable: 4275) // warning C4275: An exported class was derived from a class that wasn't exported +#endif class PYBIND11_EXPORT error_already_set : public std::runtime_error { public: /// Constructs a new exception from the current Python error indicator, if any. The current @@ -349,7 +353,9 @@ class PYBIND11_EXPORT error_already_set : public std::runtime_error { private: object type, value, trace; }; - +#if defined(_MSC_VER) +# pragma warning(pop) +#endif /** \defgroup python_builtins _ Unless stated otherwise, the following C++ functions behave the same as their Python counterparts.