diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index bb4f39d3..7a0d8004 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -284,23 +284,25 @@ struct storage_access { python::list strides; std::size_t stride = 1; if (b.type_.id_ == 5) { - // cannot pass cpp_int to numpy, so make - // a double array, fill it, and pass that to python + // cannot pass cpp_int to numpy; make new + // double array, fill it and pass it npy_intp shapes2[BOOST_HISTOGRAM_AXIS_LIMIT]; - npy_intp strides2[BOOST_HISTOGRAM_AXIS_LIMIT]; stride = sizeof(double); d["typestr"] = python::str("|f") + python::str(stride); for (unsigned i = 0; i < self.dim(); ++i) { const auto s = shape(self.axis(i)); shapes2[i] = s; - strides2[i] = s; shapes.append(s); strides.append(stride); stride *= s; } PyObject* ptr = PyArray_SimpleNew(self.dim(), shapes2, NPY_DOUBLE); - for (unsigned i = 0; i < self.dim(); ++i) - PyArray_STRIDES((PyArrayObject*)ptr)[i] = strides2[i]; + stride = sizeof(double); + for (unsigned i = 0; i < self.dim(); ++i) { + const auto s = shape(self.axis(i)); + PyArray_STRIDES((PyArrayObject*)ptr)[i] = stride; + stride *= s; + } double* buf = (double*)PyArray_DATA((PyArrayObject*)ptr); for (unsigned i = 0; i < b.size_; ++i) buf[i] = static_cast(b.template at(i)); diff --git a/test/python_suite_test.py.in b/test/python_suite_test.py.in index c6c3b2b1..924399f9 100755 --- a/test/python_suite_test.py.in +++ b/test/python_suite_test.py.in @@ -647,10 +647,11 @@ class histogramtest(unittest.TestCase): @unittest.skipUnless(have_numpy, "requires build with numpy-support") def test_numpy_conversion_4(self): - a = histogram(integer_axis(0, 1, uoflow=False)) + a = histogram(integer_axis(0, 1, uoflow=False), + integer_axis(0, 3, uoflow=False)) a1 = numpy.asarray(a) self.assertEqual(a1.dtype, numpy.uint8) - self.assertEqual(a1.shape, (2,)) + self.assertEqual(a1.shape, (2, 4)) b = histogram() b1 = numpy.asarray(b)