From 2a7acb6d55c0f1e59df1ddb518de7fe6c2fa6456 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Wed, 22 Jun 2016 00:33:56 +0100 Subject: [PATCH] Incref descriptors properly when creating arrays --- example/example20.py | 12 +++++------- example/example20.ref | 9 +++++++++ include/pybind11/numpy.h | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/example/example20.py b/example/example20.py index 78e717d91..7dd60e346 100644 --- a/example/example20.py +++ b/example/example20.py @@ -29,12 +29,10 @@ for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packe check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], simple_dtype) check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], packed_dtype) - # uncomment lines below to cause a segfault upon exit in Py_Finalize :( - - # if dtype == simple_dtype: - # print_rec_simple(arr) - # else: - # print_rec_packed(arr) + if dtype == simple_dtype: + print_rec_simple(arr) + else: + print_rec_packed(arr) nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)]) @@ -47,4 +45,4 @@ assert arr.dtype == nested_dtype check_eq(arr, [((False, 0, 0.0), (True, 1, 1.5)), ((True, 1, 1.5), (False, 2, 3.0)), ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype) -# print_rec_nested(arr) +print_rec_nested(arr) diff --git a/example/example20.ref b/example/example20.ref index 6023a87f2..315a82a00 100644 --- a/example/example20.ref +++ b/example/example20.ref @@ -1,3 +1,12 @@ T{?:x:xxxI:y:f:z:} T{?:x:=I:y:f:z:} T{T{?:x:xxxI:y:f:z:}:a:T{?:x:=I:y:f:z:}:b:} +0,0,0 +1,1,1.5 +0,2,3 +0,0,0 +1,1,1.5 +0,2,3 +0,0,0|1,1,1.5 +1,1,1.5|0,2,3 +0,2,3|1,3,4.5 diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 1d957d1a3..41d0fea90 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -96,7 +96,7 @@ public: template array(size_t size, const Type *ptr) { API& api = lookup_api(); - PyObject *descr = detail::npy_format_descriptor::descr(); + PyObject *descr = object(detail::npy_format_descriptor::descr(), true).release().ptr(); Py_intptr_t shape = (Py_intptr_t) size; object tmp = object(api.PyArray_NewFromDescr_( api.PyArray_Type_, descr, 1, &shape, nullptr, (void *) ptr, 0, nullptr), false); @@ -147,7 +147,7 @@ public: if (ptr == nullptr) return nullptr; API &api = lookup_api(); - PyObject *descr = detail::npy_format_descriptor::descr(); + PyObject *descr = object(detail::npy_format_descriptor::descr(), true).release().ptr(); PyObject *result = api.PyArray_FromAny_(ptr, descr, 0, 0, API::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr); if (!result) PyErr_Clear();