From 5c4cb8320d2a25a39bd50d72b56dc5385bbd9c59 Mon Sep 17 00:00:00 2001 From: "ndbecker2@gmail.com" Date: Wed, 12 Apr 2017 09:51:57 -0400 Subject: [PATCH 1/4] add support for noconvert --- include/xtensor-python/pyarray.hpp | 10 ++++++++- include/xtensor-python/pytensor.hpp | 12 +++++++++-- include/xtensor-python/pytraits.hpp | 33 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 include/xtensor-python/pytraits.hpp diff --git a/include/xtensor-python/pyarray.hpp b/include/xtensor-python/pyarray.hpp index 648c80d..2980977 100644 --- a/include/xtensor-python/pyarray.hpp +++ b/include/xtensor-python/pyarray.hpp @@ -17,6 +17,7 @@ #include "pycontainer.hpp" #include "pybuffer_adaptor.hpp" +#include "pytraits.hpp" namespace xt { @@ -42,8 +43,15 @@ namespace pybind11 { using type = xt::pyarray; - bool load(handle src, bool) + bool load(handle src, bool convert) { + if (!convert) { + if (!PyArray_Check(src.ptr())) + return false; + int type_num = xt::detail::numpy_traits::type_num; + if (PyArray_TYPE(src.ptr()) != type_num) + return false; + } value = type::ensure(src); return static_cast(value); } diff --git a/include/xtensor-python/pytensor.hpp b/include/xtensor-python/pytensor.hpp index 69f2916..c078579 100644 --- a/include/xtensor-python/pytensor.hpp +++ b/include/xtensor-python/pytensor.hpp @@ -18,6 +18,7 @@ #include "pycontainer.hpp" #include "pybuffer_adaptor.hpp" +#include "pytraits.hpp" namespace xt { @@ -43,9 +44,16 @@ namespace pybind11 { using type = xt::pytensor; - bool load(handle src, bool) + bool load(handle src, bool convert) { - value = type::ensure(src); + if (!convert) { + if (!PyArray_Check(src.ptr())) + return false; + int type_num = xt::detail::numpy_traits::type_num; + if (PyArray_TYPE(src.ptr()) != type_num) + return false; + } + value = type::ensure(src); return static_cast(value); } diff --git a/include/xtensor-python/pytraits.hpp b/include/xtensor-python/pytraits.hpp new file mode 100644 index 0000000..e7b83db --- /dev/null +++ b/include/xtensor-python/pytraits.hpp @@ -0,0 +1,33 @@ +#ifndef PY_TRAITS_HPP +#define PY_TRAITS_HPP + +#include + +namespace pybind11 +{ + namespace detail + { + template + struct numpy_traits + { + private: + + 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_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE, + NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE + }; + + public: + + using value_type = std::remove_const_t; + + static constexpr int type_num = value_list[pybind11::detail::is_fmt_numeric::index]; + }; + + } +} + +#endif From f317231b601fce0ed80e5381aaadf9560d3ca160 Mon Sep 17 00:00:00 2001 From: "ndbecker2@gmail.com" Date: Wed, 12 Apr 2017 10:23:53 -0400 Subject: [PATCH 2/4] need cast --- include/xtensor-python/pyarray.hpp | 2 +- include/xtensor-python/pytensor.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/xtensor-python/pyarray.hpp b/include/xtensor-python/pyarray.hpp index 2980977..6f172a0 100644 --- a/include/xtensor-python/pyarray.hpp +++ b/include/xtensor-python/pyarray.hpp @@ -49,7 +49,7 @@ namespace pybind11 if (!PyArray_Check(src.ptr())) return false; int type_num = xt::detail::numpy_traits::type_num; - if (PyArray_TYPE(src.ptr()) != type_num) + if (PyArray_TYPE(reinterpret_cast(src.ptr())) != type_num) return false; } value = type::ensure(src); diff --git a/include/xtensor-python/pytensor.hpp b/include/xtensor-python/pytensor.hpp index c078579..e404e03 100644 --- a/include/xtensor-python/pytensor.hpp +++ b/include/xtensor-python/pytensor.hpp @@ -50,7 +50,7 @@ namespace pybind11 if (!PyArray_Check(src.ptr())) return false; int type_num = xt::detail::numpy_traits::type_num; - if (PyArray_TYPE(src.ptr()) != type_num) + if (PyArray_TYPE(reinterpret_cast(src.ptr())) != type_num) return false; } value = type::ensure(src); From d6b2fb41246140e667fb756952cfb4a87631fda1 Mon Sep 17 00:00:00 2001 From: "ndbecker2@gmail.com" Date: Wed, 12 Apr 2017 10:47:57 -0400 Subject: [PATCH 3/4] CMakeLists.txt: add pytraits.hpp --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e97aa4d..2830246 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(XTENSOR_PYTHON_HEADERS ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytensor.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyvectorize.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp + ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytraits.hpp ) OPTION(BUILD_TESTS "xtensor test suite" OFF) From 64a404a76695275134b121de682b8658c68565c7 Mon Sep 17 00:00:00 2001 From: "ndbecker2@gmail.com" Date: Wed, 12 Apr 2017 11:04:04 -0400 Subject: [PATCH 4/4] Don't need header pytraits.hpp: same code is in pycontainer.hpp --- CMakeLists.txt | 1 - include/xtensor-python/pyarray.hpp | 1 - include/xtensor-python/pytensor.hpp | 1 - include/xtensor-python/pytraits.hpp | 33 ----------------------------- 4 files changed, 36 deletions(-) delete mode 100644 include/xtensor-python/pytraits.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2830246..e97aa4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,6 @@ set(XTENSOR_PYTHON_HEADERS ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytensor.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pyvectorize.hpp ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/xtensor_python_config.hpp - ${XTENSOR_PYTHON_INCLUDE_DIR}/xtensor-python/pytraits.hpp ) OPTION(BUILD_TESTS "xtensor test suite" OFF) diff --git a/include/xtensor-python/pyarray.hpp b/include/xtensor-python/pyarray.hpp index 6f172a0..b7fc162 100644 --- a/include/xtensor-python/pyarray.hpp +++ b/include/xtensor-python/pyarray.hpp @@ -17,7 +17,6 @@ #include "pycontainer.hpp" #include "pybuffer_adaptor.hpp" -#include "pytraits.hpp" namespace xt { diff --git a/include/xtensor-python/pytensor.hpp b/include/xtensor-python/pytensor.hpp index e404e03..97b265c 100644 --- a/include/xtensor-python/pytensor.hpp +++ b/include/xtensor-python/pytensor.hpp @@ -18,7 +18,6 @@ #include "pycontainer.hpp" #include "pybuffer_adaptor.hpp" -#include "pytraits.hpp" namespace xt { diff --git a/include/xtensor-python/pytraits.hpp b/include/xtensor-python/pytraits.hpp deleted file mode 100644 index e7b83db..0000000 --- a/include/xtensor-python/pytraits.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef PY_TRAITS_HPP -#define PY_TRAITS_HPP - -#include - -namespace pybind11 -{ - namespace detail - { - template - struct numpy_traits - { - private: - - 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_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE, - NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE - }; - - public: - - using value_type = std::remove_const_t; - - static constexpr int type_num = value_list[pybind11::detail::is_fmt_numeric::index]; - }; - - } -} - -#endif