@@ -24,20 +24,6 @@ using pybindit::memory::smart_holder;
24
24
25
25
namespace detail {
26
26
27
- inline std::pair<bool , handle> find_existing_python_instance (void *src_void_ptr,
28
- const detail::type_info *tinfo) {
29
- // Loop copied from type_caster_generic::cast.
30
- // IMPROVABLE: Factor out of type_caster_generic::cast.
31
- auto it_instances = get_internals ().registered_instances .equal_range (src_void_ptr);
32
- for (auto it_i = it_instances.first ; it_i != it_instances.second ; ++it_i) {
33
- for (auto instance_type : detail::all_type_info (Py_TYPE (it_i->second ))) {
34
- if (instance_type && same_type (*instance_type->cpptype , *tinfo->cpptype ))
35
- return std::make_pair (true , handle ((PyObject *) it_i->second ).inc_ref ());
36
- }
37
- }
38
- return std::make_pair (false , handle ());
39
- }
40
-
41
27
// The modified_type_caster_generic_load_impl could replace type_caster_generic::load_impl but not
42
28
// vice versa. The main difference is that the original code only propagates a reference to the
43
29
// held value, while the modified implementation propagates value_and_holder.
@@ -401,9 +387,8 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T> {
401
387
if (src == nullptr )
402
388
return none ().release ();
403
389
404
- auto existing_inst = find_existing_python_instance (src, tinfo);
405
- if (existing_inst.first )
406
- return existing_inst.second ;
390
+ if (handle existing_inst = find_registered_python_instance (src, tinfo))
391
+ return existing_inst;
407
392
408
393
auto inst = reinterpret_steal<object>(make_new_instance (tinfo->type ));
409
394
auto wrapper = reinterpret_cast <instance *>(inst.ptr ());
@@ -494,11 +479,10 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
494
479
495
480
void *src_raw_void_ptr = static_cast <void *>(src_raw_ptr);
496
481
const detail::type_info *tinfo = st.second ;
497
- auto existing_inst = find_existing_python_instance (src_raw_void_ptr, tinfo);
498
- if (existing_inst.first )
482
+ if (handle existing_inst = find_registered_python_instance (src_raw_void_ptr, tinfo))
499
483
// MISSING: Enforcement of consistency with existing smart_holder.
500
484
// MISSING: keep_alive.
501
- return existing_inst. second ;
485
+ return existing_inst;
502
486
503
487
auto inst = reinterpret_steal<object>(make_new_instance (tinfo->type ));
504
488
auto *inst_raw_ptr = reinterpret_cast <instance *>(inst.ptr ());
@@ -557,8 +541,7 @@ struct smart_holder_type_caster<std::unique_ptr<T>> : smart_holder_type_caster_l
557
541
558
542
void *src_raw_void_ptr = static_cast <void *>(src_raw_ptr);
559
543
const detail::type_info *tinfo = st.second ;
560
- auto existing_inst = find_existing_python_instance (src_raw_void_ptr, tinfo);
561
- if (existing_inst.first )
544
+ if (find_registered_python_instance (src_raw_void_ptr, tinfo))
562
545
throw cast_error (" Invalid unique_ptr: another instance owns this pointer already." );
563
546
564
547
auto inst = reinterpret_steal<object>(make_new_instance (tinfo->type ));
0 commit comments