Skip to content

Commit b4560fe

Browse files
committed
refactor: replace .get_type with type::handle_of
1 parent dabbbf3 commit b4560fe

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

docs/changelog.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ See :ref:`upgrade-guide-2.6` for help upgrading to the new version.
1717
This reduces binary size quite substantially (~25%).
1818
`#2463 <https://github.com/pybind/pybind11/pull/2463>`_
1919

20-
* Keyword-only argument supported in Python 2 or 3 with ``py::kw_only()``.
20+
* Keyword-only arguments supported in Python 2 or 3 with ``py::kw_only()``.
2121
`#2100 <https://github.com/pybind/pybind11/pull/2100>`_
2222

23-
* Positional-only argument supported in Python 2 or 3 with ``py::pos_only()``.
23+
* Positional-only arguments supported in Python 2 or 3 with ``py::pos_only()``.
24+
`#2459 <https://github.com/pybind/pybind11/pull/2459>`_
25+
26+
* Access to the type object now provided with ``py::type::of<T>()`` and
27+
``py::type::of(h)``.
28+
`#2364 <https://github.com/pybind/pybind11/pull/2364>`_
29+
2430

2531
* Perfect forwarding support for methods.
2632
`#2048 <https://github.com/pybind/pybind11/pull/2048>`_

docs/upgrade.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ An error is now thrown when ``__init__`` is forgotten on subclasses. This was
1717
incorrect before, but was not checked. Add a call to ``__init__`` if it is
1818
missing.
1919

20+
The undocumented ``h.get_type()`` method has been deprecated and replaced by
21+
``py::type::of(h)``.
22+
2023
If ``__eq__`` defined but not ``__hash__``, ``__hash__`` is now set to
2124
``None``, as in normal CPython. You should add ``__hash__`` if you intended the
2225
class to be hashable, possibly using the new ``py::hash`` shortcut.

include/pybind11/cast.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ class type_caster_generic {
636636
/// native typeinfo, or when the native one wasn't able to produce a value.
637637
PYBIND11_NOINLINE bool try_load_foreign_module_local(handle src) {
638638
constexpr auto *local_key = PYBIND11_MODULE_LOCAL_ID;
639-
const auto pytype = src.get_type();
639+
const auto pytype = type::handle_of(src);
640640
if (!hasattr(pytype, local_key))
641641
return false;
642642

@@ -1130,7 +1130,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
11301130
}
11311131

11321132
/* Check if this is a C++ type */
1133-
auto &bases = all_type_info((PyTypeObject *) h.get_type().ptr());
1133+
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
11341134
if (bases.size() == 1) { // Only allowing loading from a single-value type
11351135
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
11361136
return true;
@@ -1708,7 +1708,7 @@ template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_ca
17081708
throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)");
17091709
#else
17101710
throw cast_error("Unable to cast Python instance of type " +
1711-
(std::string) str(handle.get_type()) + " to C++ type '" + type_id<T>() + "'");
1711+
(std::string) str(type::handle_of(handle)) + " to C++ type '" + type_id<T>() + "'");
17121712
#endif
17131713
}
17141714
return conv;
@@ -1759,7 +1759,7 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
17591759
throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references"
17601760
" (compile in debug mode for details)");
17611761
#else
1762-
throw cast_error("Unable to move from Python " + (std::string) str(obj.get_type()) +
1762+
throw cast_error("Unable to move from Python " + (std::string) str(type::handle_of(obj)) +
17631763
" instance to C++ " + type_id<T>() + " instance: instance has multiple references");
17641764
#endif
17651765

@@ -2206,13 +2206,13 @@ PYBIND11_NAMESPACE_END(detail)
22062206

22072207

22082208
template<typename T>
2209-
type type::of() {
2209+
handle type::handle_of() {
22102210
static_assert(
22112211
std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
22122212
"py::type::of<T> only supports the case where T is a registered C++ types."
22132213
);
22142214

2215-
return type((PyObject*) detail::get_type_handle(typeid(T), true).ptr(), borrowed_t());
2215+
return detail::get_type_handle(typeid(T), true);
22162216
}
22172217

22182218

include/pybind11/eigen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
553553
object matrix_type = sparse_module.attr(
554554
rowMajor ? "csr_matrix" : "csc_matrix");
555555

556-
if (!obj.get_type().is(matrix_type)) {
556+
if (!type::handle_of(obj).is(matrix_type)) {
557557
try {
558558
obj = matrix_type(obj);
559559
} catch (const error_already_set &) {

include/pybind11/pybind11.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ struct enum_base {
14891489

14901490
m_base.attr("__repr__") = cpp_function(
14911491
[](handle arg) -> str {
1492-
handle type = arg.get_type();
1492+
handle type = type::handle_of(arg);
14931493
object type_name = type.attr("__name__");
14941494
dict entries = type.attr("__entries");
14951495
for (const auto &kv : entries) {
@@ -1503,7 +1503,7 @@ struct enum_base {
15031503

15041504
m_base.attr("name") = property(cpp_function(
15051505
[](handle arg) -> str {
1506-
dict entries = arg.get_type().attr("__entries");
1506+
dict entries = type::handle_of(arg).attr("__entries");
15071507
for (const auto &kv : entries) {
15081508
if (handle(kv.second[int_(0)]).equal(arg))
15091509
return pybind11::str(kv.first);
@@ -1542,7 +1542,7 @@ struct enum_base {
15421542
#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior) \
15431543
m_base.attr(op) = cpp_function( \
15441544
[](object a, object b) { \
1545-
if (!a.get_type().is(b.get_type())) \
1545+
if (!type::handle_of(a).is(type::handle_of(b))) \
15461546
strict_behavior; \
15471547
return expr; \
15481548
}, \
@@ -2115,7 +2115,7 @@ inline function get_type_override(const void *this_ptr, const type_info *this_ty
21152115
handle self = get_object_handle(this_ptr, this_type);
21162116
if (!self)
21172117
return function();
2118-
handle type = self.get_type();
2118+
handle type = type::handle_of(self);
21192119
auto key = std::make_pair(type.ptr(), name);
21202120

21212121
/* Cache functions that aren't overridden in Python to avoid

include/pybind11/pytypes.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class object_api : public pyobject_tag {
152152

153153
/// Return the object's current reference count
154154
int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
155-
/// Return a handle to the Python type object underlying the instance
155+
156+
PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
156157
handle get_type() const;
157158

158159
private:
@@ -897,13 +898,24 @@ class type : public object {
897898
public:
898899
PYBIND11_OBJECT(type, object, PyType_Check)
899900

900-
static type of(handle h) { return type((PyObject*) Py_TYPE(h.ptr()), borrowed_t{}); }
901+
/// Return a type handle from a handle or an object
902+
static handle handle_of(handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); }
903+
904+
/// Return a type object from a handle or an object
905+
static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); }
901906

902-
/// Convert C++ type to py::type if previously registered. Does not convert
903-
// standard types, like int, float. etc. yet.
904-
// See https://github.com/pybind/pybind11/issues/2486
907+
// Defined in pybind11/cast.h
908+
/// Convert C++ type to handle if previously registered. Does not convert
909+
/// standard types, like int, float. etc. yet.
910+
/// See https://github.com/pybind/pybind11/issues/2486
905911
template<typename T>
906-
static type of();
912+
static handle handle_of();
913+
914+
/// Convert C++ type to type if previously registered. Does not convert
915+
/// standard types, like int, float. etc. yet.
916+
/// See https://github.com/pybind/pybind11/issues/2486
917+
template<typename T>
918+
static type of() {return type(type::handle_of<T>(), borrowed_t{}); }
907919
};
908920

909921
class iterable : public object {
@@ -1568,7 +1580,9 @@ template <typename D>
15681580
str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); }
15691581

15701582
template <typename D>
1571-
handle object_api<D>::get_type() const { return (PyObject *) Py_TYPE(derived().ptr()); }
1583+
handle object_api<D>::get_type() const {
1584+
return (PyObject *) Py_TYPE(derived().ptr());
1585+
}
15721586

15731587
template <typename D>
15741588
bool object_api<D>::rich_compare(object_api const &other, int value) const {

0 commit comments

Comments
 (0)