From e45b29047a697798b6ad6007ab228a4933820bd5 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sun, 17 Jan 2016 22:36:41 +0100 Subject: [PATCH] numpy.h: fixed a leak, added some comments to buffer_info --- include/pybind11/common.h | 13 +++++++------ include/pybind11/numpy.h | 8 +++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index f00f16ba8..ad55be643 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -143,12 +143,13 @@ 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, size; - std::string format; // for dense contents, this should be set to format_descriptor::value - int ndim; - std::vector shape; - std::vector strides; + void *ptr; // Pointer to the underlying storage + size_t itemsize; // Size of individual items in bytes + size_t size; // Total number of entries + std::string format; // For homogeneous buffers, this should be set to format_descriptor::value + int ndim; // Number of dimensions + std::vector shape; // Shape of the tensor (1 entry per dimension) + std::vector strides; // Number of entries between adjacent entries (for each per dimension) buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim, const std::vector &shape, const std::vector &strides) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 11bb47f3e..2365a6086 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -128,9 +128,11 @@ public: return nullptr; API &api = lookup_api(); PyObject *descr = api.PyArray_DescrFromType_(npy_format_descriptor::value); - return api.PyArray_FromAny_(ptr, descr, 0, 0, - API::NPY_C_CONTIGUOUS_ | API::NPY_ENSURE_ARRAY_ | - API::NPY_ARRAY_FORCECAST_, nullptr); + PyObject *result = api.PyArray_FromAny_( + ptr, descr, 0, 0, API::NPY_C_CONTIGUOUS_ | API::NPY_ENSURE_ARRAY_ + | API::NPY_ARRAY_FORCECAST_, nullptr); + Py_DECREF(ptr); + return result; } };