Fix SEGV to create empty shaped numpy array (#1371)

Fix a segfault when creating a 0-dimension, c-strides array.
This commit is contained in:
Naotoshi Seo 2018-05-06 13:59:25 +00:00 committed by Wenzel Jakob
parent abf916d845
commit a50125ff85
3 changed files with 10 additions and 2 deletions

View File

@ -764,6 +764,7 @@ protected:
static std::vector<ssize_t> c_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) {
auto ndim = shape.size();
std::vector<ssize_t> strides(ndim, itemsize);
if (ndim > 0)
for (size_t i = ndim - 1; i > 0; --i)
strides[i - 1] = strides[i] * shape[i];
return strides;

View File

@ -102,6 +102,9 @@ TEST_SUBMODULE(numpy_array, sm) {
sm.def("make_f_array", [] { return py::array_t<float>({ 2, 2 }, { 4, 8 }); });
sm.def("make_c_array", [] { return py::array_t<float>({ 2, 2 }, { 8, 4 }); });
// test_empty_shaped_array
sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); });
// test_wrap
sm.def("wrap", [](py::array a) {
return py::array(

View File

@ -135,6 +135,10 @@ def test_make_c_f_array():
assert not m.make_f_array().flags.c_contiguous
def test_make_empty_shaped_array():
m.make_empty_shaped_array()
def test_wrap():
def assert_references(a, b, base=None):
from distutils.version import LooseVersion