Author: mattip <[email protected]>
Branch: array_interface
Changeset: r80596:fed39e09394c
Date: 2015-11-08 21:37 +0200
http://bitbucket.org/pypy/pypy/changeset/fed39e09394c/

Log:    start support for strides argument (use-case needed to implement
        more)

diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -71,9 +71,10 @@
         if w_descr is not None:
             raise oefmt(space.w_NotImplementedError,
                     "__array_interface__ descr not supported yet")
-        if w_strides is not None:
-            raise oefmt(space.w_NotImplementedError,
-                    "__array_interface__ strides not supported yet")
+        if w_strides is None or space.is_w(w_strides, space.w_None):
+            strides = None
+        else:
+            strides = [space.int_w(i) for i in space.listview(w_strides)]
         shape = [space.int_w(i) for i in space.listview(w_shape)]
         dtype = descriptor.decode_w_dtype(space, w_dtype)
         if dtype is None:
@@ -85,7 +86,8 @@
             data = rffi.cast(RAW_STORAGE_PTR, space.int_w(data_w[0]))
             read_only = True # XXX why not space.is_true(data_w[1])
             offset = 0
-            return W_NDimArray.from_shape_and_storage(space, shape, data, 
dtype, start=offset), read_only
+            return W_NDimArray.from_shape_and_storage(space, shape, data, 
+                                    dtype, strides=strides, start=offset), 
read_only
         if w_data is None:
             data = w_object
         else:
@@ -96,6 +98,9 @@
         else:
             offset = space.int_w(w_offset)
         #print 'create view from shape',shape,'dtype',dtype,'data',data
+        if strides is not None:
+            raise oefmt(space.w_NotImplementedError,
+                   "__array_interface__ strides not fully supported yet") 
         arr = frombuffer(space, data, dtype, support.product(shape), offset)
         new_impl = arr.implementation.reshape(arr, shape)
         return W_NDimArray(new_impl), False
diff --git a/pypy/module/micronumpy/test/test_ndarray.py 
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3070,7 +3070,7 @@
         assert (b == zeros(10)).all()
 
     def test_array_interface(self):
-        from numpy import array
+        from numpy import array, ones
         a = array(2.5)
         i = a.__array_interface__
         assert isinstance(i['data'][0], int)
@@ -3105,9 +3105,7 @@
         raises(TypeError, array, Dummy({'version': 3, 'typestr': 'f8', 
'shape': ('a', 3)}))
 
         a = array([1, 2, 3])
-        interface_a = a.__array_interface__
-        interface_a.pop('strides')
-        b = array(Dummy(interface_a))
+        b = array(Dummy(a.__array_interface__))
         b[1] = 200
         assert a[1] == 2 # upstream compatibility, is this a bug?
         interface_a = a.__array_interface__
@@ -3124,6 +3122,12 @@
         assert b.dtype == 'uint8'
         assert b.shape == (50,)
 
+        a = ones((1,), dtype='float16')
+        b = Dummy(a.__array_interface__)
+        c = array(b)
+        assert c.dtype == 'float16'
+        assert (a == c).all()
+
     def test_array_indexing_one_elem(self):
         from numpy import array, arange
         raises(IndexError, 'arange(3)[array([3.5])]')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to