Skip to content

Commit a859dd6

Browse files
committed
Force hidden visibility on pybind code
This adds a PYBIND11_NAMESPACE macro that expands to the `pybind11` namespace with hidden visibility under gcc-type compilers, and otherwise to the plain `pybind11`. This then forces hidden visibility on everything in pybind, solving the visibility issues discussed at end end of #949.
1 parent 8d3cedb commit a859dd6

File tree

20 files changed

+51
-40
lines changed

20 files changed

+51
-40
lines changed

include/pybind11/attr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "cast.h"
1414

15-
NAMESPACE_BEGIN(pybind11)
15+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1616

1717
/// \addtogroup annotations
1818
/// @{
@@ -478,4 +478,4 @@ constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
478478
}
479479

480480
NAMESPACE_END(detail)
481-
NAMESPACE_END(pybind11)
481+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/buffer_info.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "common.h"
1313

14-
NAMESPACE_BEGIN(pybind11)
14+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1515

1616
/// Information record describing a Python buffer object
1717
struct buffer_info {
@@ -105,4 +105,4 @@ template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_
105105
};
106106

107107
NAMESPACE_END(detail)
108-
NAMESPACE_END(pybind11)
108+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <string_view>
3131
#endif
3232

33-
NAMESPACE_BEGIN(pybind11)
33+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3434
NAMESPACE_BEGIN(detail)
3535
// Forward declarations:
3636
inline PyTypeObject *make_static_property_type();
@@ -2084,4 +2084,4 @@ NAMESPACE_END(detail)
20842084
template<> class type_caster<Type> : public type_caster_base<Type> { }; \
20852085
}}
20862086

2087-
NAMESPACE_END(pybind11)
2087+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/chrono.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds)
2828
#endif
2929

30-
NAMESPACE_BEGIN(pybind11)
30+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3131
NAMESPACE_BEGIN(detail)
3232

3333
template <typename type> class duration_caster {
@@ -159,4 +159,4 @@ template <typename Rep, typename Period> class type_caster<std::chrono::duration
159159
};
160160

161161
NAMESPACE_END(detail)
162-
NAMESPACE_END(pybind11)
162+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/class_support.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "attr.h"
1313

14-
NAMESPACE_BEGIN(pybind11)
14+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1515
NAMESPACE_BEGIN(detail)
1616

1717
inline PyTypeObject *type_incref(PyTypeObject *type) {
@@ -599,4 +599,4 @@ inline PyObject* make_new_python_type(const type_record &rec) {
599599
}
600600

601601
NAMESPACE_END(detail)
602-
NAMESPACE_END(pybind11)
602+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/common.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
# define NAMESPACE_END(name) }
1717
#endif
1818

19+
// Robust support for some features and loading modules compiled against different pybind versions
20+
// requires forcing hidden visibility on pybind code, so we enforce this by setting the attribute on
21+
// the main `pybind11` namespace.
22+
#if !defined(PYBIND11_NAMESPACE)
23+
# ifdef __GNUG__
24+
# define PYBIND11_NAMESPACE pybind11 __attribute__((visibility("hidden")))
25+
# else
26+
# define PYBIND11_NAMESPACE pybind11
27+
# endif
28+
#endif
29+
1930
#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
2031
# if __cplusplus >= 201402L
2132
# define PYBIND11_CPP14
@@ -297,7 +308,7 @@ extern "C" {
297308
void pybind11_init_##name(pybind11::module &variable)
298309

299310

300-
NAMESPACE_BEGIN(pybind11)
311+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
301312

302313
using ssize_t = Py_ssize_t;
303314
using size_t = std::size_t;
@@ -877,4 +888,4 @@ NAMESPACE_END(detail)
877888

878889

879890

880-
NAMESPACE_END(pybind11)
891+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/complex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# undef I
1818
#endif
1919

20-
NAMESPACE_BEGIN(pybind11)
20+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
2121

2222
template <typename T> struct format_descriptor<std::complex<T>, detail::enable_if_t<std::is_floating_point<T>::value>> {
2323
static constexpr const char c = format_descriptor<T>::c;
@@ -58,4 +58,4 @@ template <typename T> class type_caster<std::complex<T>> {
5858
PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
5959
};
6060
NAMESPACE_END(detail)
61-
NAMESPACE_END(pybind11)
61+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/descr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "common.h"
1414

15-
NAMESPACE_BEGIN(pybind11)
15+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
1616
NAMESPACE_BEGIN(detail)
1717

1818
/* Concatenate type signatures at compile time using C++14 */
@@ -182,4 +182,4 @@ PYBIND11_NOINLINE inline descr type_descr(descr&& d) { return _("{") + std::move
182182
#endif
183183

184184
NAMESPACE_END(detail)
185-
NAMESPACE_END(pybind11)
185+
NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/eigen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// of matrices seems highly undesirable.
3636
static_assert(EIGEN_VERSION_AT_LEAST(3,2,7), "Eigen support in pybind11 requires Eigen >= 3.2.7");
3737

38-
NAMESPACE_BEGIN(pybind11)
38+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3939

4040
// Provide a convenience alias for easier pass-by-ref usage with fully dynamic strides:
4141
using EigenDStride = Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>;
@@ -601,7 +601,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
601601
};
602602

603603
NAMESPACE_END(detail)
604-
NAMESPACE_END(pybind11)
604+
NAMESPACE_END(PYBIND11_NAMESPACE)
605605

606606
#if defined(__GNUG__) || defined(__clang__)
607607
# pragma GCC diagnostic pop

include/pybind11/embed.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
void pybind11_init_##name(pybind11::module &variable)
6464

6565

66-
NAMESPACE_BEGIN(pybind11)
66+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
6767
NAMESPACE_BEGIN(detail)
6868

6969
/// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
@@ -190,4 +190,4 @@ class scoped_interpreter {
190190
bool is_valid = true;
191191
};
192192

193-
NAMESPACE_END(pybind11)
193+
NAMESPACE_END(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)