Skip to content

Commit 6778d2f

Browse files
committed
Use get_type_info(•, throw_if_missing = true) to throw on unknown type
1 parent 27ea0cf commit 6778d2f

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,8 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
623623
static constexpr auto name = _<std::shared_ptr<T>>();
624624

625625
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
626-
auto src_raw_ptr = src.get();
627-
auto st = type_caster_base<T>::src_and_type(src_raw_ptr);
628-
if (st.first == nullptr)
629-
throw error_already_set();
630-
626+
auto src_raw_ptr = src.get();
627+
auto st = type_caster_base<T>::src_and_type(src_raw_ptr);
631628
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
632629
const detail::type_info *tinfo = st.second;
633630
if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo))
@@ -688,11 +685,8 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
688685
throw cast_error("Invalid return_value_policy for unique_ptr.");
689686
}
690687

691-
auto src_raw_ptr = src.get();
692-
auto st = type_caster_base<T>::src_and_type(src_raw_ptr);
693-
if (st.first == nullptr)
694-
return none().release(); // PyErr was set already.
695-
688+
auto src_raw_ptr = src.get();
689+
auto st = type_caster_base<T>::src_and_type(src_raw_ptr);
696690
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
697691
const detail::type_info *tinfo = st.second;
698692
if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) {

include/pybind11/detail/type_caster_base.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,9 @@ class type_caster_generic {
739739
// isn't needed or can't be used. If the type is unknown, sets the error and returns a pair
740740
// with .second = nullptr. (p.first = nullptr is not an error: it becomes None).
741741
PYBIND11_NOINLINE static std::pair<const void *, const type_info *> src_and_type(
742-
const void *src, const std::type_info &cast_type, const std::type_info *rtti_type = nullptr) {
743-
if (auto *tpi = get_type_info(cast_type))
744-
return {src, const_cast<const type_info *>(tpi)};
745-
746-
// Not found, set error:
747-
std::string tname = rtti_type ? rtti_type->name() : cast_type.name();
748-
detail::clean_type_id(tname);
749-
std::string msg = "Unregistered type : " + tname;
750-
PyErr_SetString(PyExc_TypeError, msg.c_str());
751-
return {nullptr, nullptr};
742+
const void *src, const std::type_info &cast_type) {
743+
auto *tpi = get_type_info(cast_type, true);
744+
return {src, const_cast<const type_info *>(tpi)};
752745
}
753746

754747
const type_info *typeinfo = nullptr;
@@ -895,7 +888,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
895888
}
896889
// Otherwise we have either a nullptr, an `itype` pointer, or an unknown derived pointer, so
897890
// don't do a cast
898-
return type_caster_generic::src_and_type(src, cast_type, instance_type);
891+
return type_caster_generic::src_and_type(src, cast_type);
899892
}
900893

901894
static handle cast(const itype *src, return_value_policy policy, handle parent) {

0 commit comments

Comments
 (0)