Skip to content

Commit 2196696

Browse files
committed
Use std::type_info::name() for type lookups outside stdlibc++
Using `std::type_info::operator==` fails under libc++ because the .so is loaded with RTLD_LOCAL. libc++ considers types under such .sos distinct, and so comparing typeid() values directly isn't going to work. This adds a custom hasher and equality class for the type lookup maps when not under stdlibc++, and adds a `detail::same_type` function to perform the equality test. It also converts a few pointer arguments to const lvalue references, particularly since doing the pointer comparison wasn't technically valid to being with (though in practice, appeared to work everywhere). This fixes #912.
1 parent 2d6116b commit 2196696

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

include/pybind11/attr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,17 @@ struct type_record {
243243
/// Is the default (unique_ptr) holder type used?
244244
bool default_holder : 1;
245245

246-
PYBIND11_NOINLINE void add_base(const std::type_info *base, void *(*caster)(void *)) {
247-
auto base_info = detail::get_type_info(*base, false);
246+
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
247+
auto base_info = detail::get_type_info(base, false);
248248
if (!base_info) {
249-
std::string tname(base->name());
249+
std::string tname(base.name());
250250
detail::clean_type_id(tname);
251251
pybind11_fail("generic_type: type \"" + std::string(name) +
252252
"\" referenced unknown base type \"" + tname + "\"");
253253
}
254254

255255
if (default_holder != base_info->default_holder) {
256-
std::string tname(base->name());
256+
std::string tname(base.name());
257257
detail::clean_type_id(tname);
258258
pybind11_fail("generic_type: type \"" + std::string(name) + "\" " +
259259
(default_holder ? "does not have" : "has") +
@@ -384,7 +384,7 @@ struct process_attribute<T, enable_if_t<is_pyobject<T>::value>> : process_attrib
384384
/// Process a parent class attribute (deprecated, does not support multiple inheritance)
385385
template <typename T>
386386
struct process_attribute<base<T>> : process_attribute_default<base<T>> {
387-
static void init(const base<T> &, type_record *r) { r->add_base(&typeid(T), nullptr); }
387+
static void init(const base<T> &, type_record *r) { r->add_base(typeid(T), nullptr); }
388388
};
389389

390390
/// Process a multiple inheritance attribute

include/pybind11/cast.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <array>
1717
#include <limits>
1818
#include <tuple>
19-
#include <cstring>
2019

2120
#if defined(PYBIND11_CPP17)
2221
# if defined(__has_include)
@@ -659,14 +658,14 @@ class type_caster_generic {
659658
// isn't needed or can't be used. If the type is unknown, sets the error and returns a pair
660659
// with .second = nullptr. (p.first = nullptr is not an error: it becomes None).
661660
PYBIND11_NOINLINE static std::pair<const void *, const type_info *> src_and_type(
662-
const void *src, const std::type_info *cast_type, const std::type_info *rtti_type = nullptr) {
661+
const void *src, const std::type_info &cast_type, const std::type_info *rtti_type = nullptr) {
663662
auto &internals = get_internals();
664-
auto it = internals.registered_types_cpp.find(std::type_index(*cast_type));
663+
auto it = internals.registered_types_cpp.find(std::type_index(cast_type));
665664
if (it != internals.registered_types_cpp.end())
666665
return {src, (const type_info *) it->second};
667666

668667
// Not found, set error:
669-
std::string tname = (rtti_type ? rtti_type : cast_type)->name();
668+
std::string tname = rtti_type ? rtti_type->name() : cast_type.name();
670669
detail::clean_type_id(tname);
671670
std::string msg = "Unregistered type : " + tname;
672671
PyErr_SetString(PyExc_TypeError, msg.c_str());
@@ -743,11 +742,11 @@ template <typename type> class type_caster_base : public type_caster_generic {
743742
static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
744743
const void *vsrc = src;
745744
auto &internals = get_internals();
746-
auto cast_type = &typeid(itype);
745+
auto &cast_type = typeid(itype);
747746
const std::type_info *instance_type = nullptr;
748747
if (vsrc) {
749748
instance_type = &typeid(*src);
750-
if (instance_type != cast_type) {
749+
if (!same_type(cast_type, *instance_type)) {
751750
// This is a base pointer to a derived type; if it is a pybind11-registered type, we
752751
// can get the correct derived pointer (which may be != base pointer) by a
753752
// dynamic_cast to most derived type:
@@ -764,7 +763,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
764763
// Non-polymorphic type, so no dynamic casting; just call the generic version directly
765764
template <typename T = itype, enable_if_t<!std::is_polymorphic<T>::value, int> = 0>
766765
static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
767-
return type_caster_generic::src_and_type(src, &typeid(itype));
766+
return type_caster_generic::src_and_type(src, typeid(itype));
768767
}
769768

770769
static handle cast(const itype *src, return_value_policy policy, handle parent) {
@@ -1739,7 +1738,7 @@ constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
17391738

17401739
NAMESPACE_BEGIN(detail)
17411740

1742-
// forward declaration
1741+
// forward declaration (definition in attr.h)
17431742
struct function_record;
17441743

17451744
/// Internal data associated with a single function call

include/pybind11/common.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#endif
125125

126126
#include <cstddef>
127+
#include <cstring>
127128
#include <forward_list>
128129
#include <vector>
129130
#include <string>
@@ -426,13 +427,48 @@ struct overload_hash {
426427
}
427428
};
428429

430+
// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly
431+
// other stls, this means `typeid(A)` from one module won't equal `typeid(A)` from another module
432+
// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under
433+
// stdlibc++, this doesn't happen: equality and the type_index hash are based on the type name,
434+
// which works. If not under a known-good stl, provide our own name-based hasher and equality
435+
// functions that use the type name.
436+
#if defined(__GLIBCXX__)
437+
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
438+
using type_hash = std::hash<std::type_index>;
439+
using type_equal_to = std::equal_to<std::type_index>;
440+
#else
441+
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
442+
return lhs.name() == rhs.name() ||
443+
std::strcmp(lhs.name(), rhs.name()) == 0;
444+
}
445+
struct type_hash {
446+
size_t operator()(const std::type_index &t) const {
447+
size_t hash = 5381;
448+
const char *ptr = t.name();
449+
while (auto c = static_cast<unsigned char>(*ptr++))
450+
hash = (hash * 33) ^ c;
451+
return hash;
452+
}
453+
};
454+
struct type_equal_to {
455+
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
456+
return lhs.name() == rhs.name() ||
457+
std::strcmp(lhs.name(), rhs.name()) == 0;
458+
}
459+
};
460+
#endif
461+
462+
template <typename value_type>
463+
using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
464+
429465
/// Internal data structure used to track registered instances and types
430466
struct internals {
431-
std::unordered_map<std::type_index, void*> registered_types_cpp; // std::type_index -> type_info
467+
type_map<void *> registered_types_cpp; // std::type_index -> type_info
432468
std::unordered_map<PyTypeObject *, std::vector<type_info *>> registered_types_py; // PyTypeObject* -> base type_info(s)
433469
std::unordered_multimap<const void *, instance*> registered_instances; // void * -> instance*
434470
std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
435-
std::unordered_map<std::type_index, std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
471+
type_map<std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
436472
std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
437473
std::unordered_map<std::string, void *> shared_data; // Custom data to be shared across extensions
438474
PyTypeObject *static_property_type;

include/pybind11/functional.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct type_caster<std::function<Return(Args...)>> {
4646
auto c = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(cfunc.ptr()));
4747
auto rec = (function_record *) c;
4848

49-
if (rec && rec->is_stateless && rec->data[1] == &typeid(function_type)) {
49+
if (rec && rec->is_stateless &&
50+
same_type(typeid(function_type), *reinterpret_cast<const std::type_info *>(rec->data[1]))) {
5051
struct capture { function_type f; };
5152
value = ((capture *) &rec->data)->f;
5253
return true;

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ class class_ : public detail::generic_type {
968968

969969
template <typename Base, detail::enable_if_t<is_base<Base>::value, int> = 0>
970970
static void add_base(detail::type_record &rec) {
971-
rec.add_base(&typeid(Base), [](void *src) -> void * {
971+
rec.add_base(typeid(Base), [](void *src) -> void * {
972972
return static_cast<Base *>(reinterpret_cast<type *>(src));
973973
});
974974
}

0 commit comments

Comments
 (0)