Skip to content

Commit cba7020

Browse files
committed
Fix for pybind11>=2.12
1 parent 41aab59 commit cba7020

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

wrapper/cccorelib/src/cccorelib.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,38 @@ class NumpyCloud : public CCCoreLib::GenericIndexedCloud
143143
public:
144144
explicit NumpyCloud(const py::array &array)
145145
{
146-
py::detail::PyArrayDescr_Proxy *descr =
147-
py::detail::array_descriptor_proxy(py::detail::array_proxy(array.ptr())->descr);
148146

149-
py::list names = py::cast<py::list>(descr->names);
147+
py::list names;
148+
#if ((PYBIND11_VERSION_MAJOR > 2) || (PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR > 11))
149+
// The "name" field is no longer available in Descr_Proxy struct,
150+
// but it is available on both Descr1_Proxy and Descr2_Proxy structs
151+
152+
// Get the currrent numpy version
153+
// Maybe it would be worth having a dedicated function in the long run.
154+
py::module_ numpy = py::module_::import("numpy");
155+
py::str version_string = numpy.attr("__version__");
156+
157+
py::module_ numpy_lib = py::module_::import("numpy.lib");
158+
py::object numpy_version = numpy_lib.attr("NumpyVersion")(version_string);
159+
int major_version = numpy_version.attr("major").cast<int>();
160+
161+
if (major_version < 2)
162+
{
163+
const py::detail::PyArrayDescr1_Proxy *descr =
164+
py::detail::array_descriptor1_proxy(py::detail::array_proxy(array.ptr())->descr);
165+
names = py::cast<py::list>(descr->names);
166+
}
167+
else
168+
{
169+
const py::detail::PyArrayDescr2_Proxy *descr =
170+
py::detail::array_descriptor2_proxy(py::detail::array_proxy(array.ptr())->descr);
171+
names = py::cast<py::list>(descr->names);
172+
}
173+
#else
174+
const py::detail::PyArrayDescr_Proxy *descr =
175+
py::detail::array_descriptor_proxy(py::detail::array_proxy(array.ptr())->descr);
176+
names = py::cast<py::list>(descr->names);
177+
#endif
150178
if (names.size() >= 3)
151179
{
152180
m_xs = array[names[0]].cast<py::array_t<PointCoordinateType>>();

0 commit comments

Comments
 (0)