99namespace cpp2py {
1010
1111 template <typename T> static void delete_pycapsule (PyObject *capsule) {
12- auto *ptr = static_cast <std::unique_ptr<T[] > *>(PyCapsule_GetPointer (capsule, " guard" ));
12+ auto *ptr = static_cast <std::vector<T > *>(PyCapsule_GetPointer (capsule, " guard" ));
1313 delete ptr;
1414 }
1515
1616 // Convert vector to numpy_proxy, WARNING: Deep Copy
17- template <typename T> numpy_proxy make_numpy_proxy_from_vector (std::vector<T> const & v) {
17+ template <typename T> numpy_proxy make_numpy_proxy_from_vector (std::vector<T> v) {
1818
19- auto *data_ptr = new std::unique_ptr<T[]>{new T[v.size ()]};
20- std::copy (begin (v), end (v), data_ptr->get ());
21- auto capsule = PyCapsule_New (data_ptr, " guard" , &delete_pycapsule<T>);
19+ auto *vec_heap = new std::vector<T>{std::move (v)};
20+ auto capsule = PyCapsule_New (vec_heap, " guard" , &delete_pycapsule<T>);
2221
2322 return {1 , // rank
2423 npy_type<std::remove_const_t <T>>,
25- (void *)data_ptr-> get (),
24+ (void *)vec_heap-> data (),
2625 std::is_const_v<T>,
2726 v_t {static_cast <long >(v.size ())}, // extents
2827 v_t {sizeof (T)}, // strides
@@ -46,14 +45,14 @@ namespace cpp2py {
4645
4746 template <typename T> struct py_converter <std::vector<T>> {
4847
49- static PyObject *c2py (std::vector<T> const & v) {
48+ static PyObject *c2py (std::vector<T> v) {
5049
5150 if constexpr (has_npy_type<T>) {
52- return make_numpy_proxy_from_vector (v ).to_python ();
51+ return make_numpy_proxy_from_vector (std::move (v) ).to_python ();
5352 } else { // Convert to Python List
5453 PyObject *list = PyList_New (0 );
5554 for (auto const &x : v) {
56- pyref y = py_converter<T>::c2py (x );
55+ pyref y = py_converter<T>::c2py (std::move (x) );
5756 if (y.is_null () or (PyList_Append (list, y) == -1 )) {
5857 Py_DECREF (list);
5958 return NULL ;
0 commit comments