mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-27 23:52:00 +00:00
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:
parent
abf916d845
commit
a50125ff85
@ -764,8 +764,9 @@ 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);
|
||||
for (size_t i = ndim - 1; i > 0; --i)
|
||||
strides[i - 1] = strides[i] * shape[i];
|
||||
if (ndim > 0)
|
||||
for (size_t i = ndim - 1; i > 0; --i)
|
||||
strides[i - 1] = strides[i] * shape[i];
|
||||
return strides;
|
||||
}
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user