Skip to content

Commit c64efa0

Browse files
committed
Implement compatbility layer for old pybind version using the existing pattern.
1 parent e75c1d3 commit c64efa0

4 files changed

Lines changed: 21 additions & 30 deletions

File tree

Plugin/src/SofaPython3/config.h.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,18 @@
5555
#else
5656
#define SOFAPYTHON3_BIND_ATTRIBUTE_ERROR() namespace pybind11 { PYBIND11_RUNTIME_EXCEPTION(attribute_error, PyExc_AttributeError) }
5757
#endif // PYBIND11_SOFA_VERSION >= 20801
58+
59+
#if PYBIND11_SOFA_VERSION >= 20801
60+
#define SOFAPYTHON3_ADD_PYBIND_TYPE_FOR_OLD_VERSION()
61+
#else
62+
#define SOFAPYTHON3_ADD_PYBIND_TYPE_FOR_OLD_VERSION() namespace pybind11 { \
63+
class type : public pybind11::object { \
64+
public: \
65+
PYBIND11_OBJECT(type, pybind11::object, PyType_Check) \
66+
static pybind11::handle handle_of(pybind11::handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); } \
67+
static type of(pybind11::handle h) { return type(type::handle_of(h), borrowed_t{}); } \
68+
template<typename T> static handle handle_of(); \
69+
template<typename T> static type of() {return type(type::handle_of<T>(), borrowed_t{}); } \
70+
}; \
71+
}
72+
#endif // PYBIND11_SOFA_VERSION >= 20801

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ using sofa::simulation::Node;
4545

4646
#include <SofaPython3/DataHelper.h>
4747

48+
// These two lines are there to handle deprecated version of pybind.
4849
SOFAPYTHON3_BIND_ATTRIBUTE_ERROR()
50+
SOFAPYTHON3_ADD_PYBIND_TYPE_FOR_OLD_VERSION()
4951

5052
/// Makes an alias for the pybind11 namespace to increase readability.
5153
namespace py { using namespace pybind11; }
@@ -361,7 +363,7 @@ py::object BindingBase::__getattr__(py::object self, const std::string& attribut
361363
emitSpellingMessage(tmp, " - The link named ", selfbase->getLinks(), attributeName, 2, 0.6);
362364

363365
// Also provide spelling hints on python functions.
364-
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(pybind11_compat::type::of(self).attr("__dict__")), attributeName, 5, 0.8,
366+
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(py::type::of(self).attr("__dict__")), attributeName, 5, 0.8,
365367
[](const std::pair<py::handle, py::handle>& kv) { return py::cast<std::string>(std::get<0>(kv)); });
366368

367369
std::stringstream message;

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,6 @@ namespace sofapython3 {
3030
template <typename T>
3131
using py_shared_ptr = sofa::core::sptr<T>;
3232

33-
namespace pybind11_compat
34-
{
35-
using namespace pybind11;
36-
class type : public pybind11::object {
37-
public:
38-
PYBIND11_OBJECT(type, pybind11::object, PyType_Check)
39-
40-
/// Return a type handle from a handle or an object
41-
static pybind11::handle handle_of(pybind11::handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); }
42-
43-
/// Return a type object from a handle or an object
44-
static type of(pybind11::handle h) { return type(type::handle_of(h), borrowed_t{}); }
45-
46-
// Defined in pybind11/cast.h
47-
/// Convert C++ type to handle if previously registered. Does not convert
48-
/// standard types, like int, float. etc. yet.
49-
/// See https://github.com/pybind/pybind11/issues/2486
50-
template<typename T>
51-
static handle handle_of();
52-
53-
/// Convert C++ type to type if previously registered. Does not convert
54-
/// standard types, like int, float. etc. yet.
55-
/// See https://github.com/pybind/pybind11/issues/2486
56-
template<typename T>
57-
static type of() {return type(type::handle_of<T>(), borrowed_t{}); }
58-
};
59-
}
60-
6133
} // namespace sofapython3
6234

6335
PYBIND11_DECLARE_HOLDER_TYPE(T, sofapython3::py_shared_ptr<T>, true)

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ using sofa::core::objectmodel::BaseObjectDescription;
6767
#include <queue>
6868
#include <sofa/core/objectmodel/Link.h>
6969

70+
// These two lines are there to handle deprecated version of pybind.
7071
SOFAPYTHON3_BIND_ATTRIBUTE_ERROR()
72+
SOFAPYTHON3_ADD_PYBIND_TYPE_FOR_OLD_VERSION()
7173

7274
/// Makes an alias for the pybind11 namespace to increase readability.
7375
namespace py { using namespace pybind11; }
@@ -484,7 +486,7 @@ py::object __getattr__(py::object pyself, const std::string& name)
484486
emitSpellingMessage(tmp, " - The child node named ", selfnode->getChildren(), name, 2, 0.8);
485487

486488
// Also provide spelling hints on python functions.
487-
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(pybind11_compat::type::of(pyself).attr("__dict__")), name, 5, 0.8,
489+
emitSpellingMessage(tmp, " - The python attribute named ", py::cast<py::dict>(py::type::of(pyself).attr("__dict__")), name, 5, 0.8,
488490
[](const std::pair<py::handle, py::handle>& kv) { return py::cast<std::string>(std::get<0>(kv)); });
489491

490492
std::stringstream message;

0 commit comments

Comments
 (0)