Commits

Maciej Fijalkowski committed 0973879

a hackish support for int -> numpy array

  • Participants
  • Parent commits c359afa
  • Branches pypy-in-a-box

Comments (0)

Files changed (2)

File pypy/module/micronumpy/interp_numarray.py

         if get_numarray_cache(space).enable_invalidation:
             self.invalidates.append(other)
         
-    def descr__new__(space, w_subtype, w_size, w_dtype=None):
+    def descr__new__(space, w_subtype, w_size, w_dtype=None, w_buffer=None):
         dtype = space.interp_w(interp_dtype.W_Dtype,
             space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
         )
         shape = _find_shape(space, w_size)
-        return space.wrap(W_NDimArray(shape[:], dtype=dtype))
+        if w_buffer is not None and not space.is_w(w_buffer, space.w_None):
+            buffer = space.int_w(w_buffer)
+        else:
+            buffer = 0
+        return space.wrap(W_NDimArray(shape[:], dtype=dtype, buffer=buffer))
 
     def _unaryop_impl(ufunc_name):
         def impl(self, space, w_out=None):
     """
     _immutable_fields_ = ['storage']
 
-    def __init__(self, shape, dtype, order='C', parent=None):
+    def __init__(self, shape, dtype, order='C', parent=None, buffer=0):
+        from pypy.module.micronumpy.types import VOID_STORAGE
+        
         self.parent = parent
         self.size = support.product(shape) * dtype.get_size()
         if parent is not None:
             self.storage = parent.storage
         else:
-            self.storage = dtype.itemtype.malloc(self.size)
+            if buffer != 0:
+                self.storage = rffi.cast(lltype.Ptr(VOID_STORAGE), buffer)
+            else:
+                self.storage = dtype.itemtype.malloc(self.size)
         self.order = order
         self.dtype = dtype
         if self.strides is None:

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

         assert isinstance(i['data'][0], int)
         raises(TypeError, getattr, array(3), '__array_interface__')
 
+    def test_buffer(self):
+        from _numpypy import ndarray, array
+
+        a = array([1, 2, 3])
+        b = ndarray([3], 'i8', buffer=a.__array_interface__['data'][0])
+        assert b[1] == 2
+        b[1] = 13
+        assert a[1] == 13
+
     def test_array_indexing_one_elem(self):
         skip("not yet")
         from _numpypy import array, arange