Commits

Antonio Cuni  committed 193128b

add a new implementation of arrays which keeps alive the original buffer

  • Participants
  • Parent commits b9d813c
  • Branches ndarray-buffer

Comments (0)

Files changed (4)

File pypy/module/micronumpy/arrayimpl/concrete.py

     def __del__(self):
         free_raw_storage(self.storage, track_allocation=False)
 
+class ConcreteArrayWithBase(ConcreteArrayNotOwning):
+    def __init__(self, shape, dtype, order, strides, backstrides, storage, orig_base):
+        ConcreteArrayNotOwning.__init__(self, shape, dtype, order,
+                                        strides, backstrides, storage)
+        self.orig_base = orig_base
 
+    def base(self):
+        return self.orig_base
+        
 class NonWritableArray(ConcreteArray):
     def descr_setitem(self, space, orig_array, w_index, w_value):
         raise OperationError(space.w_ValueError, space.wrap(

File pypy/module/micronumpy/base.py

         return W_NDimArray(impl)
 
     @staticmethod
-    def from_shape_and_storage(space, shape, storage, dtype, order='C', owning=False, w_subtype=None):
+    def from_shape_and_storage(space, shape, storage, dtype, order='C', owning=False,
+                               w_subtype=None, w_base=None):
         from pypy.module.micronumpy.arrayimpl import concrete
         assert shape
         strides, backstrides = calc_strides(shape, dtype, order)
-        if owning:
+        if w_base is not None:
+            if owning:
+                raise OperationError(space.w_ValueError, 
+                        space.wrap("Cannot have owning=True when specifying a buffer"))
+            impl = concrete.ConcreteArrayWithBase(shape, dtype, order, strides,
+                                                  backstrides, storage, w_base)
+        elif owning:
             # Will free storage when GCd
             impl = concrete.ConcreteArray(shape, dtype, order, strides,
                                                 backstrides, storage=storage)

File pypy/module/micronumpy/interp_numarray.py

             raise OperationError(space.w_TypeError, space.wrap(
                 "numpy scalars from buffers not supported yet"))
         storage = rffi.cast(RAW_STORAGE_PTR, raw_ptr)
-        return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype)
+        return W_NDimArray.from_shape_and_storage(space, shape, storage, dtype,
+                                                  w_base=w_buffer)
 
     if not shape:
         return W_NDimArray.new_scalar(space, dtype)

File pypy/module/micronumpy/test/test_numarray.py

         a[1] = ord('a')
         a[2] = ord('r')
         assert list(buf) == ['b', '\x00', 'a', '\x00', 'r', '\x00']
-        
+        assert a.base is buf
+
 
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):