[python-arrayfire] 112/250: Index array wrapper class to avoid premature Index destruction

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 66402ef50f2803d489a3490781b19d0d89c7fe01
Author: Filipe Maia <filipe.c.maia at gmail.com>
Date:   Wed Sep 9 22:05:47 2015 +0200

    Index array wrapper class to avoid premature Index destruction
---
 arrayfire/array.py |  8 ++++----
 arrayfire/index.py | 30 ++++++++++++++++++++++++++----
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/arrayfire/array.py b/arrayfire/array.py
index b8ea142..c4d6681 100644
--- a/arrayfire/array.py
+++ b/arrayfire/array.py
@@ -17,6 +17,7 @@ from .util import *
 from .bcast import *
 from .base import *
 from .index import *
+from .index import _Index4
 
 def _create_array(buf, numdims, idims, dtype):
     out_arr = ct.c_void_p(0)
@@ -152,9 +153,8 @@ def _get_info(dims, buf_len):
 
 def _get_indices(key):
 
-    index_vec = Index * 4
     S = Index(slice(None))
-    inds = index_vec(S, S, S, S)
+    inds = _Index4(S, S, S, S)
 
     if isinstance(key, tuple):
         n_idx = len(key)
@@ -874,7 +874,7 @@ class Array(BaseArray):
             inds = _get_indices(key)
 
             safe_call(backend.get().af_index_gen(ct.pointer(out.arr),
-                                        self.arr, ct.c_longlong(n_dims), ct.pointer(inds)))
+                                    self.arr, ct.c_longlong(n_dims), inds.pointer))
             return out
         except RuntimeError as e:
             raise IndexError(str(e))
@@ -903,7 +903,7 @@ class Array(BaseArray):
             inds  = _get_indices(key)
 
             safe_call(backend.get().af_assign_gen(ct.pointer(out_arr),
-                                         self.arr, ct.c_longlong(n_dims), ct.pointer(inds),
+                                         self.arr, ct.c_longlong(n_dims), inds.pointer,
                                          other_arr))
             safe_call(backend.get().af_release_array(self.arr))
             if del_other:
diff --git a/arrayfire/index.py b/arrayfire/index.py
index 5dc2c36..ed7061c 100644
--- a/arrayfire/index.py
+++ b/arrayfire/index.py
@@ -208,7 +208,29 @@ class Index(ct.Structure):
         else:
             self.idx.seq = Seq(idx)
 
-    # def __del__(self):
-    #     if not self.isSeq:
-    #         arr = self.idx.arr
-    #         backend.get().af_release_array(arr)
+    def __del__(self):
+        if not self.isSeq:
+            # ctypes field variables are automatically
+            # converted to basic C types so we have to
+            # build the void_p from the value again.
+            arr = ct.c_void_p(self.idx.arr)
+            backend.get().af_release_array(arr)
+
+class _Index4(object):
+    def __init__(self, idx0, idx1, idx2, idx3):
+        index_vec = Index * 4
+        self.array = index_vec(idx0, idx1, idx2, idx3)
+        # Do not lose those idx as self.array keeps
+        # no reference to them. Otherwise the destructor
+        # is prematurely called
+        self.idxs = [idx0,idx1,idx2,idx3]
+    @property
+    def pointer(self):
+        return ct.pointer(self.array)
+
+    def __getitem__(self, idx):
+        return self.array[idx]
+
+    def __setitem__(self, idx, value):
+        self.array[idx] = value
+        self.idxs[idx] = value

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list