Skip to content

Commit 77435f3

Browse files
committed
Making use of the new find_existing_python_instance() function factored out with PR pybind#2822.
1 parent f994556 commit 77435f3

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ using pybindit::memory::smart_holder;
2424

2525
namespace detail {
2626

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-
4127
// The modified_type_caster_generic_load_impl could replace type_caster_generic::load_impl but not
4228
// vice versa. The main difference is that the original code only propagates a reference to the
4329
// 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> {
401387
if (src == nullptr)
402388
return none().release();
403389

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;
407392

408393
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
409394
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
494479

495480
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
496481
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))
499483
// MISSING: Enforcement of consistency with existing smart_holder.
500484
// MISSING: keep_alive.
501-
return existing_inst.second;
485+
return existing_inst;
502486

503487
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
504488
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
557541

558542
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
559543
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))
562545
throw cast_error("Invalid unique_ptr: another instance owns this pointer already.");
563546

564547
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));

0 commit comments

Comments
 (0)