[pypy-commit] pypy pypy-in-a-box: a hackish support for int -> numpy array

fijal noreply at buildbot.pypy.org
Wed Jul 18 18:32:16 CEST 2012


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: pypy-in-a-box
Changeset: r56183:09738793ae38
Date: 2012-07-18 18:31 +0200
http://bitbucket.org/pypy/pypy/changeset/09738793ae38/

Log:	a hackish support for int -> numpy array

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -77,12 +77,16 @@
         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):
@@ -1004,13 +1008,18 @@
     """
     _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:
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1923,6 +1923,15 @@
         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


More information about the pypy-commit mailing list