Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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
48 changes: 44 additions & 4 deletions 48 include/ndarray/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ template <typename T, int N, int C>
class type_caster< ndarray::Array<T,N,C> > {
public:
bool load(handle src, bool) {
_none = src.is_none();
if (_none) return true;
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< ndarray::Array<T,N,C> >::fromPythonStage1(_src)) {
PyErr_Clear();
Expand All @@ -80,12 +82,20 @@ class type_caster< ndarray::Array<T,N,C> > {
protected:
ndarray::PyPtr _src;
ndarray::Array<T,N,C> _value;
bool _none = false;
public:
static PYBIND11_DESCR name() { return type_descr(_<ndarray::Array<T,N,C>>()); }
static handle cast(const ndarray::Array<T,N,C> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator ndarray::Array<T,N,C> * () { set_value(); return &_value; }
operator ndarray::Array<T,N,C> * () {
if (_none) {
return nullptr;
} else {
set_value();
return &_value;
}
}
operator ndarray::Array<T,N,C> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
Expand All @@ -96,6 +106,8 @@ template <typename T, int N, int C, typename Kind, int Rows, int Cols>
class type_caster< ndarray::EigenView<T,N,C,Kind,Rows,Cols> > {
public:
bool load(handle src, bool) {
_none = src.is_none();
if (_none) return true;
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< ndarray::EigenView<T,N,C,Kind,Rows,Cols> >::fromPythonStage1(_src)) {
PyErr_Clear();
Expand All @@ -117,12 +129,20 @@ class type_caster< ndarray::EigenView<T,N,C,Kind,Rows,Cols> > {
protected:
ndarray::PyPtr _src;
ndarray::EigenView<T,N,C,Kind,Rows,Cols> _value;
bool _none = false;
public:
static PYBIND11_DESCR name() { return type_descr(_<ndarray::EigenView<T,N,C,Kind,Rows,Cols>>()); }
static handle cast(const ndarray::EigenView<T,N,C,Kind,Rows,Cols> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator ndarray::EigenView<T,N,C,Kind,Rows,Cols> * () { set_value(); return &_value; }
operator ndarray::EigenView<T,N,C,Kind,Rows,Cols> * () {
if (_none) {
return nullptr;
} else {
set_value();
return &_value;
}
}
operator ndarray::EigenView<T,N,C,Kind,Rows,Cols> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
Expand All @@ -133,6 +153,8 @@ template<typename T, int R, int C, int O, int MR, int MC>
class type_caster< Eigen::Array<T,R,C,O,MR,MC> > {
public:
bool load(handle src, bool) {
_none = src.is_none();
if (_none) return true;
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< Eigen::Array<T,R,C,O,MR,MC> >::fromPythonStage1(_src)) {
PyErr_Clear();
Expand All @@ -154,12 +176,20 @@ class type_caster< Eigen::Array<T,R,C,O,MR,MC> > {
protected:
ndarray::PyPtr _src;
Eigen::Array<T,R,C,O,MR,MC> _value;
bool _none = false;
public:
static PYBIND11_DESCR name() { return type_descr(_<Eigen::Array<T,R,C,O,MR,MC>>()); }
static handle cast(const Eigen::Array<T,R,C,O,MR,MC> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator Eigen::Array<T,R,C,O,MR,MC> * () { set_value(); return &_value; }
operator Eigen::Array<T,R,C,O,MR,MC> * () {
if (_none) {
return nullptr;
} else {
set_value();
return &_value;
}
}
operator Eigen::Array<T,R,C,O,MR,MC> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
Expand All @@ -170,6 +200,8 @@ template<typename T, int R, int C, int O, int MR, int MC>
class type_caster< Eigen::Matrix<T,R,C,O,MR,MC> > {
public:
bool load(handle src, bool) {
_none = src.is_none();
if (_none) return true;
_src.reset(src.ptr(), true); // keep alive for stage 2
if (!ndarray::PyConverter< Eigen::Matrix<T,R,C,O,MR,MC> >::fromPythonStage1(_src)) {
PyErr_Clear();
Expand All @@ -191,12 +223,20 @@ class type_caster< Eigen::Matrix<T,R,C,O,MR,MC> > {
protected:
ndarray::PyPtr _src;
Eigen::Matrix<T,R,C,O,MR,MC> _value;
bool _none = false;
public:
static PYBIND11_DESCR name() { return type_descr(_<Eigen::Matrix<T,R,C,O,MR,MC>>()); }
static handle cast(const Eigen::Matrix<T,R,C,O,MR,MC> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator Eigen::Matrix<T,R,C,O,MR,MC> * () { set_value(); return &_value; }
operator Eigen::Matrix<T,R,C,O,MR,MC> * () {
if (_none) {
return nullptr;
} else {
set_value();
return &_value;
}
}
operator Eigen::Matrix<T,R,C,O,MR,MC> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};
Expand Down
14 changes: 14 additions & 0 deletions 14 tests/pybind11_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ def testStrideHandling(self):
table = numpy.zeros(3, dtype=dtype)
self.assertRaises(TypeError, pybind11_test_mod.acceptArray10, table['f1'])

def testNone(self):
array = numpy.zeros(10, dtype=float)
self.assertEqual(pybind11_test_mod.acceptNoneArray(array), 0)
self.assertEqual(pybind11_test_mod.acceptNoneArray(None), 1)
self.assertEqual(pybind11_test_mod.acceptNoneArray(), 1)

m1 = pybind11_test_mod.returnMatrixXd()
self.assertEqual(pybind11_test_mod.acceptNoneMatrixXd(m1), 2)
self.assertEqual(pybind11_test_mod.acceptNoneMatrixXd(None), 3)
self.assertEqual(pybind11_test_mod.acceptNoneMatrixXd(), 3)

m2 = pybind11_test_mod.returnMatrix2d()
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(m2), 4)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(None), 5)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(), 5)
if __name__ == "__main__":
unittest.main()
28 changes: 28 additions & 0 deletions 28 tests/pybind11_test_mod.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ndarray/converter.h"

namespace py = pybind11;
using namespace py::literals;

Eigen::MatrixXd returnMatrixXd() {
Eigen::MatrixXd r(5, 3);
Expand Down Expand Up @@ -94,6 +95,30 @@ int acceptOverload(Eigen::Matrix2d const & m) {
return 2;
}

int acceptNoneArray(ndarray::Array<double, 1, 1> const * array = nullptr) {
if (array) {
return 0;
} else {
return 1;
}
}

int acceptNoneMatrixXd(Eigen::MatrixXd const * matrix = nullptr) {
if (matrix) {
return 2;
} else {
return 3;
}
}

int acceptNoneMatrix2d(Eigen::Matrix2d const * matrix = nullptr) {
if (matrix) {
return 4;
} else {
return 5;
}
}

struct MatrixOwner {
typedef Eigen::Matrix<double,2,2,Eigen::DontAlign> MemberMatrix;
MemberMatrix member;
Expand Down Expand Up @@ -130,6 +155,9 @@ PYBIND11_PLUGIN(pybind11_test_mod) {
mod.def("acceptOverload", (int (*)(int)) acceptOverload);
mod.def("acceptOverload", (int (*)(Eigen::Matrix2d const &)) acceptOverload);
mod.def("acceptOverload", (int (*)(Eigen::Matrix3d const &)) acceptOverload);
mod.def("acceptNoneArray", acceptNoneArray, "array"_a = nullptr);
mod.def("acceptNoneMatrixXd", acceptNoneMatrixXd, "matrix"_a = nullptr);
mod.def("acceptNoneMatrix2d", acceptNoneMatrix2d, "matrix"_a = nullptr);

return mod.ptr();
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.