Skip to content

Commit a4d0d95

Browse files
committed
Make static internals ptr pybind version specific
Under gcc, the `static internals *internals_ptr` is shared across .so's, which breaks for obvious reasons. This commit fixes it by moving the static pointer declaration into a pybind-version-templated function.
1 parent 731a9f6 commit a4d0d95

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

include/pybind11/cast.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ struct type_info {
4343
bool default_holder : 1;
4444
};
4545

46+
// Store the static internals pointer in a version-specific function so that we're guaranteed it
47+
// will be distinct for modules compiled for different pybind11 versions. Without this, some
48+
// compilers (i.e. gcc) can use the same static pointer storage location across different .so's,
49+
// even though the `get_internals()` function itself is local to each shared object.
50+
template <int = PYBIND11_VERSION_MAJOR, int = PYBIND11_VERSION_MINOR>
51+
internals *&get_internals_ptr() { static internals *internals_ptr = nullptr; return internals_ptr; }
52+
4653
PYBIND11_NOINLINE inline internals &get_internals() {
47-
static internals *internals_ptr = nullptr;
54+
internals *&internals_ptr = get_internals_ptr();
4855
if (internals_ptr)
4956
return *internals_ptr;
5057
handle builtins(PyEval_GetBuiltins());

0 commit comments

Comments
 (0)