Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Both containers enable the numpy-style APIs of xtensor (see [the numpy to xtenso
#include <numeric> // 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<double>& m)
Expand All @@ -55,6 +56,7 @@ double sum_of_sines(xt::pyarray<double>& 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");
Expand Down
2 changes: 1 addition & 1 deletion docs/source/numpy_capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 18 additions & 1 deletion include/xtensor-python/pyarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,27 @@ namespace pybind11
}
};


template <typename T>
struct pyobject_caster<xt::pyarray<T>>
{
using type = xt::pyarray<T>;

// 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<PyArrayObject*>(src.ptr()));
if (type_num == NPY_LONGLONG)
{
return xt::detail::numpy_traits<long long>::type_num;
}
if (type_num == NPY_ULONGLONG)
{
return xt::detail::numpy_traits<unsigned long long>::type_num;
}
return type_num;
}

bool load(handle src, bool convert)
{
if (!convert)
Expand All @@ -54,7 +70,8 @@ namespace pybind11
return false;
}
int type_num = xt::detail::numpy_traits<T>::type_num;
if (PyArray_TYPE(reinterpret_cast<PyArrayObject*>(src.ptr())) != type_num)
int py_type_num = get_py_type_num(src);
if (py_type_num != type_num)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion include/xtensor-python/pycontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions include/xtensor-python/xtensor_type_caster_base.hpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -78,7 +78,7 @@ namespace pybind11
}

private:

// Cast implementation
template <typename CType>
static handle cast_impl(CType* src, return_value_policy policy, handle parent)
Expand Down