File tree Expand file tree Collapse file tree 3 files changed +18
-15
lines changed Expand file tree Collapse file tree 3 files changed +18
-15
lines changed Original file line number Diff line number Diff line change @@ -2204,25 +2204,16 @@ object object_api<Derived>::call(Args &&...args) const {
2204
2204
PYBIND11_NAMESPACE_END (detail)
2205
2205
2206
2206
2207
- /* * \ingroup python_builtins
2208
- \rst
2209
- Return the registered type object for a C++ class, given as a template parameter.
2210
- py::type<T>() returns the Python type object previously registered for T.
2211
- \endrst */
2212
2207
template<typename T>
2213
- handle type() {
2214
- static_assert (
2208
+ type type::of () {
2209
+ static_assert (
2215
2210
std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
2216
2211
" This currently only works for registered C++ types. The type here is most likely type converted (using type_caster)."
2217
2212
);
2218
2213
2219
- return detail::get_type_handle (typeid (T), true );
2214
+ return type ((PyTypeObject*) detail::get_type_handle (typeid (T), true ). ptr () );
2220
2215
}
2221
2216
2222
- inline handle type (handle h) {
2223
- PyObject* obj = (PyObject *) Py_TYPE (h.ptr ());
2224
- return handle (obj);
2225
- }
2226
2217
2227
2218
#define PYBIND11_MAKE_OPAQUE (...) \
2228
2219
namespace pybind11 { namespace detail { \
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
19
19
/* A few forward declarations */
20
20
class handle; class object ;
21
21
class str ; class iterator ;
22
+ class type ;
22
23
struct arg ; struct arg_v ;
23
24
24
25
PYBIND11_NAMESPACE_BEGIN (detail)
@@ -890,6 +891,17 @@ class iterator : public object {
890
891
object value = {};
891
892
};
892
893
894
+ class type : public handle {
895
+ public:
896
+ type (PyTypeObject* type_ptr) : handle((PyObject*) type_ptr) {}
897
+ type (const handle& h) : handle((PyObject *) Py_TYPE(h.ptr())) {}
898
+
899
+ template <typename T>
900
+ static type of ();
901
+
902
+ bool check () const { return ptr () != nullptr && PyType_Check (ptr ());}
903
+ };
904
+
893
905
class iterable : public object {
894
906
public:
895
907
PYBIND11_OBJECT_DEFAULT (iterable, object, detail::PyIterable_Check)
Original file line number Diff line number Diff line change @@ -140,11 +140,11 @@ TEST_SUBMODULE(class_, m) {
140
140
m.def (" check_type" , [](int category) {
141
141
// Currently not supported (via a fail at compile time)
142
142
// if (category == 2)
143
- // return py::type<int>();
143
+ // return py::type::of <int>();
144
144
if (category == 1 )
145
- return py::type<DerivedClass1>();
145
+ return py::type::of <DerivedClass1>();
146
146
else
147
- return py::type<Invalid>();
147
+ return py::type::of <Invalid>();
148
148
});
149
149
150
150
m.def (" compute_type" , [](py::handle h) {
You can’t perform that action at this time.
0 commit comments