Skip to content

Commit

Permalink
Factoring out find_registered_python_instance() from type_caster_gene…
Browse files Browse the repository at this point in the history
…ric::cast. (#2822)

Factoring out find_registered_python_instance() from type_caster_generic::cast.
  • Loading branch information
Ralf W. Grosse-Kunstleve authored Jan 27, 2021
1 parent 87954e7 commit 9b7bfef
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool t
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
}

// Searches the inheritance graph for a registered Python instance, using all_type_info().
PYBIND11_NOINLINE inline handle find_registered_python_instance(void *src,
const detail::type_info *tinfo) {
auto it_instances = get_internals().registered_instances.equal_range(src);
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
return handle((PyObject *) it_i->second).inc_ref();
}
}
return handle();
}

struct value_and_holder {
instance *inst = nullptr;
size_t index = 0u;
Expand Down Expand Up @@ -508,13 +521,8 @@ class type_caster_generic {
if (src == nullptr)
return none().release();

auto it_instances = get_internals().registered_instances.equal_range(src);
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
return handle((PyObject *) it_i->second).inc_ref();
}
}
if (handle registered_inst = find_registered_python_instance(src, tinfo))
return registered_inst;

auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
auto wrapper = reinterpret_cast<instance *>(inst.ptr());
Expand Down

0 comments on commit 9b7bfef

Please sign in to comment.