From 8c100e262fad4c397229fad1751448c424a5a1e9 Mon Sep 17 00:00:00 2001
From: Pim Schellart
Date: Tue, 18 Jul 2017 15:57:55 -0400
Subject: [PATCH] Fix regression in Eigen converter
---
include/ndarray/converter/eigen.h | 6 +++---
tests/pybind11_test.py | 7 +++++++
tests/pybind11_test_mod.cc | 5 +++++
3 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/include/ndarray/converter/eigen.h b/include/ndarray/converter/eigen.h
index 30895fc6..9f56fb5e 100644
--- a/include/ndarray/converter/eigen.h
+++ b/include/ndarray/converter/eigen.h
@@ -30,7 +30,6 @@ template
struct PyConverter< EigenView > {
static bool fromPythonStage1(PyPtr & p) {
- auto array = reinterpret_cast(p.get());
// add or remove dimensions with size one so we have the right number of dimensions
if (PyArray_Check(p.get())) {
if ((Rows_ == 1 || Cols_ == 1) && N == 2) {
@@ -41,17 +40,18 @@ struct PyConverter< EigenView > {
shape[1] = 1;
}
PyArray_Dims dims = { shape, 2 };
- PyPtr r(PyArray_Newshape(array, &dims, NPY_ANYORDER));
+ PyPtr r(PyArray_Newshape(reinterpret_cast(p.get()), &dims, NPY_ANYORDER));
if (!r) return false;
p.swap(r);
} else if (N == 1) {
- PyPtr r(PyArray_Squeeze(array));
+ PyPtr r(PyArray_Squeeze(reinterpret_cast(p.get())));
if (!r) return false;
p.swap(r);
}
} // else let the Array converter raise the exception
if (!PyConverter< Array >::fromPythonStage1(p)) return false;
// check whether the size is correct if it's static
+ auto array = reinterpret_cast(p.get());
if (N == 2) {
if (Rows_ != Eigen::Dynamic && PyArray_DIM(array, 0) != Rows_) {
PyErr_SetString(PyExc_ValueError, "incorrect number of rows for matrix");
diff --git a/tests/pybind11_test.py b/tests/pybind11_test.py
index 82e65b5f..911f19e1 100755
--- a/tests/pybind11_test.py
+++ b/tests/pybind11_test.py
@@ -88,5 +88,12 @@ def testNone(self):
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(m2), 4)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(None), 5)
self.assertEqual(pybind11_test_mod.acceptNoneMatrix2d(), 5)
+
+ def testFullySpecifiedMatrix(self):
+ # Failure on this specific case was not caught by other unit tests
+ a = numpy.array([[ 1., 0.], [ 0., 1.]])
+ b = numpy.array([ 0., 0.])
+ self.assert_(pybind11_test_mod.acceptFullySpecifiedMatrix(a, b))
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/pybind11_test_mod.cc b/tests/pybind11_test_mod.cc
index 5e75def6..1e008ed4 100644
--- a/tests/pybind11_test_mod.cc
+++ b/tests/pybind11_test_mod.cc
@@ -127,6 +127,10 @@ struct MatrixOwner {
explicit MatrixOwner() : member(MemberMatrix::Zero()) {}
};
+bool acceptFullySpecifiedMatrix(Eigen::Matrix const & a, Eigen::Matrix const & b) {
+ return true;
+};
+
PYBIND11_PLUGIN(pybind11_test_mod) {
pybind11::module mod("pybind11_test_mod", "Tests for the ndarray library");
@@ -159,6 +163,7 @@ PYBIND11_PLUGIN(pybind11_test_mod) {
mod.def("acceptNoneArray", acceptNoneArray, "array"_a = nullptr);
mod.def("acceptNoneMatrixXd", acceptNoneMatrixXd, "matrix"_a = nullptr);
mod.def("acceptNoneMatrix2d", acceptNoneMatrix2d, "matrix"_a = nullptr);
+ mod.def("acceptFullySpecifiedMatrix", acceptFullySpecifiedMatrix);
return mod.ptr();
}
\ No newline at end of file