Skip to content

Commit 196e71b

Browse files
committed
Apply the same versioning to type_info and module_local
1 parent a5f3cd9 commit 196e71b

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

include/pybind11/cast.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,6 @@
3333

3434
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3535
NAMESPACE_BEGIN(detail)
36-
struct value_and_holder;
37-
38-
/// Additional type information which does not fit into the PyTypeObject
39-
struct type_info {
40-
PyTypeObject *type;
41-
const std::type_info *cpptype;
42-
size_t type_size, holder_size_in_ptrs;
43-
void *(*operator_new)(size_t);
44-
void (*init_instance)(instance *, const void *);
45-
void (*dealloc)(value_and_holder &v_h);
46-
std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
47-
std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
48-
std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
49-
buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
50-
void *get_buffer_data = nullptr;
51-
void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
52-
/* A simple type never occurs as a (direct or indirect) parent
53-
* of a class that makes use of multiple inheritance */
54-
bool simple_type : 1;
55-
/* True if there is no multiple inheritance in this type's inheritance tree */
56-
bool simple_ancestors : 1;
57-
/* for base vs derived holder_type checks */
58-
bool default_holder : 1;
59-
/* true if this is a type registered with py::module_local */
60-
bool module_local : 1;
61-
};
6236

6337
/// A life support system for temporary objects created by `type_caster::load()`.
6438
/// Adding a patient will keep it alive up until the enclosing function returns.
@@ -629,7 +603,7 @@ class type_caster_generic {
629603
/// Try to load with foreign typeinfo, if available. Used when there is no
630604
/// native typeinfo, or when the native one wasn't able to produce a value.
631605
PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) {
632-
constexpr auto *local_key = "_pybind11_module_local_typeinfo";
606+
constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
633607
const auto pytype = src.get_type();
634608
if (!hasattr(pytype, local_key))
635609
return false;

include/pybind11/detail/internals.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,33 @@ struct internals {
8484
#endif
8585
};
8686

87-
/// Tracks the `internals` ABI version independent of the main library version
87+
/// Additional type information which does not fit into the PyTypeObject.
88+
/// Changes to this struct also require bumping `PYBIND11_INTERNALS_VERSION`.
89+
struct type_info {
90+
PyTypeObject *type;
91+
const std::type_info *cpptype;
92+
size_t type_size, holder_size_in_ptrs;
93+
void *(*operator_new)(size_t);
94+
void (*init_instance)(instance *, const void *);
95+
void (*dealloc)(value_and_holder &v_h);
96+
std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
97+
std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
98+
std::vector<bool (*)(PyObject *, void *&)> *direct_conversions;
99+
buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
100+
void *get_buffer_data = nullptr;
101+
void *(*module_local_load)(PyObject *, const type_info *) = nullptr;
102+
/* A simple type never occurs as a (direct or indirect) parent
103+
* of a class that makes use of multiple inheritance */
104+
bool simple_type : 1;
105+
/* True if there is no multiple inheritance in this type's inheritance tree */
106+
bool simple_ancestors : 1;
107+
/* for base vs derived holder_type checks */
108+
bool default_holder : 1;
109+
/* true if this is a type registered with py::module_local */
110+
bool module_local : 1;
111+
};
112+
113+
/// Tracks the `internals` and `type_info` ABI version independent of the main library version
88114
#define PYBIND11_INTERNALS_VERSION 1
89115

90116
#if defined(WITH_THREAD)
@@ -96,6 +122,9 @@ struct internals {
96122
#define PYBIND11_INTERNALS_ID "__pybind11_internals_v" \
97123
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
98124

125+
#define PYBIND11_MODULE_LOCAL_ID "__pybind11_module_local_v" \
126+
PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_INTERNALS_KIND "__"
127+
99128
/// Each module locally stores a pointer to the `internals` data. The data
100129
/// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
101130
inline internals *&get_internals_ptr() {

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ class generic_type : public object {
870870
if (rec.module_local) {
871871
// Stash the local typeinfo and loader so that external modules can access it.
872872
tinfo->module_local_load = &type_caster_generic::local_load;
873-
setattr(m_ptr, "_pybind11_module_local_typeinfo", capsule(tinfo));
873+
setattr(m_ptr, PYBIND11_MODULE_LOCAL_ID, capsule(tinfo));
874874
}
875875
}
876876

0 commit comments

Comments
 (0)