@@ -207,6 +207,19 @@ PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool t
207
207
return handle (type_info ? ((PyObject *) type_info->type ) : nullptr );
208
208
}
209
209
210
+ // Searches the inheritance graph for a registered Python instance, using all_type_info().
211
+ PYBIND11_NOINLINE inline handle find_registered_python_instance (void *src,
212
+ const detail::type_info *tinfo) {
213
+ auto it_instances = get_internals ().registered_instances .equal_range (src);
214
+ for (auto it_i = it_instances.first ; it_i != it_instances.second ; ++it_i) {
215
+ for (auto instance_type : detail::all_type_info (Py_TYPE (it_i->second ))) {
216
+ if (instance_type && same_type (*instance_type->cpptype , *tinfo->cpptype ))
217
+ return handle ((PyObject *) it_i->second ).inc_ref ();
218
+ }
219
+ }
220
+ return handle ();
221
+ }
222
+
210
223
struct value_and_holder {
211
224
instance *inst = nullptr ;
212
225
size_t index = 0u ;
@@ -508,13 +521,8 @@ class type_caster_generic {
508
521
if (src == nullptr )
509
522
return none ().release ();
510
523
511
- auto it_instances = get_internals ().registered_instances .equal_range (src);
512
- for (auto it_i = it_instances.first ; it_i != it_instances.second ; ++it_i) {
513
- for (auto instance_type : detail::all_type_info (Py_TYPE (it_i->second ))) {
514
- if (instance_type && same_type (*instance_type->cpptype , *tinfo->cpptype ))
515
- return handle ((PyObject *) it_i->second ).inc_ref ();
516
- }
517
- }
524
+ if (handle registered_inst = find_registered_python_instance (src, tinfo))
525
+ return registered_inst;
518
526
519
527
auto inst = reinterpret_steal<object>(make_new_instance (tinfo->type ));
520
528
auto wrapper = reinterpret_cast <instance *>(inst.ptr ());
0 commit comments