From fa1bfb2ec7654b7ea6e8419967174e3b6c35698c Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Thu, 22 Oct 2015 16:03:44 +0200 Subject: [PATCH] do a fallback search over types to handle incompatible std::type_info* across module boundaries (fixes issue #4) --- include/pybind11/cast.h | 15 +++++++++++++-- include/pybind11/common.h | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index aa3001b06..5fe93426e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -101,10 +101,21 @@ public: class type_caster_custom { public: PYBIND11_NOINLINE type_caster_custom(const std::type_info *type_info) { - auto const& registered_types = get_internals().registered_types; + auto & registered_types = get_internals().registered_types; auto it = registered_types.find(type_info); - if (it != registered_types.end()) + if (it != registered_types.end()) { typeinfo = &it->second; + } else { + /* Unknown type?! Since std::type_info* often varies across + module boundaries, the following does an explicit check */ + for (auto const &type : registered_types) { + if (strcmp(type.first->name(), type_info->name()) == 0) { + registered_types[type_info] = type.second; + typeinfo = &type.second; + break; + } + } + } } PYBIND11_NOINLINE bool load(PyObject *src, bool convert) { diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 0bbf5ee9c..d26c84f81 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -135,7 +135,7 @@ template > struct in holder_type holder; }; -/// Additional type information which does not fit into the PyTypeObjet +/// Additional type information which does not fit into the PyTypeObject struct type_info { PyTypeObject *type; size_t type_size;