Skip to content

Commit 16bedbe

Browse files
committed
Simplify getObjectForType()
Pybind11 2.6 introduces `pybind11::type::of<T>()` and `pybind11::type`, see pybind/pybind11#2364. We can use neither: `of<T>()` throws and `type` cannot represent `None`.
1 parent da6634c commit 16bedbe

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

runtime/genpybind/runtime.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88

99
namespace genpybind {
1010

11+
/// A non-throwing variant of `pybind11::type::of<T>()`.
12+
///
13+
/// Emits a warning and returns `None` if `T` has not previously
14+
/// been registered.
1115
template <typename T> auto getObjectForType() -> ::pybind11::object {
1216
const ::std::type_info &type_info = typeid(T);
1317
bool throw_if_missing = false;
14-
auto *pybind_info =
15-
::pybind11::detail::get_type_info(type_info, throw_if_missing);
16-
if (pybind_info == nullptr) {
18+
auto handle =
19+
::pybind11::detail::get_type_handle(type_info, throw_if_missing);
20+
if (!handle) {
1721
std::string name = type_info.name();
1822
::pybind11::detail::clean_type_id(name);
1923
PyErr_WarnFormat(PyExc_Warning, /*stack_level=*/7,
2024
"Reference to unknown type '%s'", name.c_str());
21-
return pybind11::none();
25+
return ::pybind11::none();
2226
}
23-
return ::pybind11::reinterpret_borrow<::pybind11::object>(
24-
(PyObject *)pybind_info->type);
27+
return ::pybind11::reinterpret_borrow<::pybind11::object>(handle);
2528
}
2629

2730
template <typename T> std::string string_from_lshift(const T &obj) {

0 commit comments

Comments
 (0)