Fix Eigen shape assertion error in dense matrix caster

Missing conformability check was causing Eigen to create a 0x0 matrix
with an error in debug mode and silent corruption in release mode.
This commit is contained in:
Dean Moldovan 2017-05-11 15:38:39 +02:00
parent 94d0a9f7bc
commit 4567f1f82a
2 changed files with 13 additions and 0 deletions

View File

@ -265,6 +265,9 @@ struct type_caster<Type, enable_if_t<is_eigen_dense_plain<Type>::value>> {
return false;
auto fits = props::conformable(buf);
if (!fits)
return false;
// Allocate the new type, then build a numpy reference into it
value = Type(fits.rows, fits.cols);
auto ref = reinterpret_steal<array>(eigen_ref_array<props>(value));

View File

@ -63,6 +63,16 @@ def test_partially_fixed():
np.testing.assert_array_equal(
partial_copy_four_cm_c(ref2[(3, 1, 2), :]), ref2[(3, 1, 2), :])
# TypeError should be raise for a shape mismatch
functions = [partial_copy_four_rm_r, partial_copy_four_rm_c,
partial_copy_four_cm_r, partial_copy_four_cm_c]
matrix_with_wrong_shape = [[1, 2],
[3, 4]]
for f in functions:
with pytest.raises(TypeError) as excinfo:
f(matrix_with_wrong_shape)
assert "incompatible function arguments" in str(excinfo.value)
def test_mutator_descriptors():
from pybind11_tests import fixed_mutator_r, fixed_mutator_c, fixed_mutator_a