From 11f756f5a9504b003883ae243ec34276222c7c3e Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 16 Sep 2020 22:02:09 -0400 Subject: [PATCH] fix: type bug intruduced in #2492 This now tests the old form too, and fixes the bug introduced. --- include/pybind11/pytypes.h | 5 ++--- tests/test_class.cpp | 4 ++++ tests/test_class.py | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index a2f7cec4..48b52408 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -153,7 +153,7 @@ public: /// Return the object's current reference count int ref_count() const { return static_cast(Py_REFCNT(derived().ptr())); } - PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()") + // TODO PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()") handle get_type() const; private: @@ -1580,8 +1580,7 @@ template str_attr_accessor object_api::doc() const { return attr("__doc__"); } template -PYBIND11_DEPRECATED("Use py::type::of(h) instead of h.get_type()") -handle object_api::get_type() const { return type::handle_of(*this); } +handle object_api::get_type() const { return type::handle_of(derived()); } template bool object_api::rich_compare(object_api const &other, int value) const { diff --git a/tests/test_class.cpp b/tests/test_class.cpp index b0e3d3a4..bb9d3644 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -152,6 +152,10 @@ TEST_SUBMODULE(class_, m) { return py::type::of(ob); }); + m.def("get_type_classic", [](py::handle h) { + return h.get_type(); + }); + m.def("as_type", [](py::object ob) { auto tp = py::type(ob); if (py::isinstance(ob)) diff --git a/tests/test_class.py b/tests/test_class.py index be21f370..64f49419 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -45,6 +45,12 @@ def test_type_of_py(): assert m.get_type_of(int) == type +def test_type_of_classic(): + assert m.get_type_classic(1) == int + assert m.get_type_classic(m.DerivedClass1()) == m.DerivedClass1 + assert m.get_type_classic(int) == type + + def test_type_of_py_nodelete(): # If the above test deleted the class, this will segfault assert m.get_type_of(m.DerivedClass1()) == m.DerivedClass1