diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 742e24a0a..f00f16ba8 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -144,7 +144,7 @@ PYBIND11_DECL_FMT(float, "f"); PYBIND11_DECL_FMT(double, "d"); PYBIND11_DECL /// Information record describing a Python buffer object struct buffer_info { void *ptr; - size_t itemsize, count; + size_t itemsize, size; std::string format; // for dense contents, this should be set to format_descriptor::value int ndim; std::vector shape; @@ -152,10 +152,26 @@ struct buffer_info { buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim, const std::vector &shape, const std::vector &strides) - : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim), - shape(shape), strides(strides) { - count = 1; for (int i=0; ibuf), itemsize(view->itemsize), size(1), format(view->format), + ndim(view->ndim), shape(view->ndim), strides(view->ndim), view(view) { + for (int i = 0; i < view->ndim; ++i) { + shape[i] = (size_t) view->shape[i]; + strides[i] = (size_t) view->strides[i]; + size *= shape[i]; + } + } + + ~buffer_info() { + if (view) { PyBuffer_Release(view); delete view; } + } +private: + Py_buffer *view = nullptr; }; NAMESPACE_BEGIN(detail) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 3ff68f508..11bb47f3e 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -165,13 +165,13 @@ struct vectorize_helper { std::array buffers {{ args.request()... }}; /* Determine dimensions parameters of output array */ - int ndim = 0; size_t count = 0; + int ndim = 0; size_t size = 0; std::vector shape; for (size_t i=0; i count) { + if (buffers[i].size > size) { ndim = buffers[i].ndim; shape = buffers[i].shape; - count = buffers[i].count; + size = buffers[i].size; } } std::vector strides(ndim); @@ -183,10 +183,10 @@ struct vectorize_helper { /* Check if the parameters are actually compatible */ for (size_t i=0; i shape(view->ndim), strides(view->ndim); - for (int i=0; indim; ++i) { - shape[i] = (size_t) view->shape[i]; - strides[i] = (size_t) view->strides[i]; - } - return buffer_info(view->buf, view->itemsize, view->format, - view->ndim, shape, strides); + return buffer_info(view); } - ~buffer() { if (view) { PyBuffer_Release(view); delete view; } } -private: - Py_buffer *view = nullptr; }; NAMESPACE_BEGIN(detail)