From da23183f02d0372af3293cf6ad2d8295ffd6bff8 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Thu, 26 Oct 2017 16:04:53 +0200 Subject: [PATCH] fix casting for int64/uint64 and long long types --- README.md | 2 ++ docs/source/numpy_capi.rst | 2 +- include/xtensor-python/pyarray.hpp | 19 ++++++++++++++++++- include/xtensor-python/pycontainer.hpp | 2 +- .../xtensor_type_caster_base.hpp | 6 +++--- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 998bd9d..d15de97 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Both containers enable the numpy-style APIs of xtensor (see [the numpy to xtenso #include // Standard library import for std::accumulate #include "pybind11/pybind11.h" // Pybind11 import to define Python bindings #include "xtensor/xmath.hpp" // xtensor import for the C++ universal functions +#define FORCE_IMPORT_ARRAY #include "xtensor-python/pyarray.hpp" // Numpy bindings double sum_of_sines(xt::pyarray& m) @@ -55,6 +56,7 @@ double sum_of_sines(xt::pyarray& m) PYBIND11_PLUGIN(xtensor_python_test) { + xt::import_numpy(); pybind11::module m("xtensor_python_test", "Test module for xtensor python bindings"); m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values"); diff --git a/docs/source/numpy_capi.rst b/docs/source/numpy_capi.rst index a88007a..2a7c458 100644 --- a/docs/source/numpy_capi.rst +++ b/docs/source/numpy_capi.rst @@ -25,7 +25,7 @@ Thus the basic skeleton of the module looks like: #include "pybind11/pybind11.h" #define FORCE_IMPORT_ARRAY - #include "xgtensor-python/pyarray.hpp" + #include "xtensor-python/pyarray.hpp" PYBIND11_PLUGIN(plugin_name) { diff --git a/include/xtensor-python/pyarray.hpp b/include/xtensor-python/pyarray.hpp index 1e33dfe..ac6ce7e 100644 --- a/include/xtensor-python/pyarray.hpp +++ b/include/xtensor-python/pyarray.hpp @@ -40,11 +40,27 @@ namespace pybind11 } }; + template struct pyobject_caster> { using type = xt::pyarray; + // we need to do special checks for unsigned long long and long long + inline int get_py_type_num(handle src) + { + int type_num = PyArray_TYPE(reinterpret_cast(src.ptr())); + if (type_num == NPY_LONGLONG) + { + return xt::detail::numpy_traits::type_num; + } + if (type_num == NPY_ULONGLONG) + { + return xt::detail::numpy_traits::type_num; + } + return type_num; + } + bool load(handle src, bool convert) { if (!convert) @@ -54,7 +70,8 @@ namespace pybind11 return false; } int type_num = xt::detail::numpy_traits::type_num; - if (PyArray_TYPE(reinterpret_cast(src.ptr())) != type_num) + int py_type_num = get_py_type_num(src); + if (py_type_num != type_num) { return false; } diff --git a/include/xtensor-python/pycontainer.hpp b/include/xtensor-python/pycontainer.hpp index 6c5d1f8..b38eafa 100644 --- a/include/xtensor-python/pycontainer.hpp +++ b/include/xtensor-python/pycontainer.hpp @@ -125,7 +125,7 @@ namespace xt constexpr static const int value_list[15] = { NPY_BOOL, NPY_BYTE, NPY_UBYTE, NPY_SHORT, NPY_USHORT, - NPY_INT, NPY_UINT, NPY_LONGLONG, NPY_ULONGLONG, + NPY_INT, NPY_UINT, NPY_INT64, NPY_UINT64, NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE, NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE}; diff --git a/include/xtensor-python/xtensor_type_caster_base.hpp b/include/xtensor-python/xtensor_type_caster_base.hpp index 294bce3..41e43ef 100644 --- a/include/xtensor-python/xtensor_type_caster_base.hpp +++ b/include/xtensor-python/xtensor_type_caster_base.hpp @@ -1,7 +1,7 @@ -/* +/* xtensor-python/xtensor_type_caster.hpp: Transparent conversion for xtensor and xarray - This code is based on the following code written by Wenzei Jakob + This code is based on the following code written by Wenzel Jakob pybind11/eigen.h: Transparent conversion for dense and sparse Eigen matrices @@ -78,7 +78,7 @@ namespace pybind11 } private: - + // Cast implementation template static handle cast_impl(CType* src, return_value_policy policy, handle parent)