Author: Christian Tismer <tis...@stackless.com>
Branch: 
Changeset: r50254:469c4a362d73
Date: 2011-12-07 17:07 +0100
http://bitbucket.org/pypy/pypy/changeset/469c4a362d73/

Log:    Merge

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
@@ -916,6 +916,15 @@
     def descr_debug_repr(self, space):
         return space.wrap(self.debug_repr())
 
+    def descr_array_iface(self, space):
+        concrete = self.get_concrete()
+        storage = concrete.get_storage(space)
+        addr = rffi.cast(lltype.Signed, storage)
+        w_d = space.newdict()
+        space.setitem_str(w_d, 'data', space.newtuple([space.wrap(addr),
+                                                       space.w_False]))
+        return w_d
+
 def convert_to_array(space, w_obj):
     if isinstance(w_obj, BaseArray):
         return w_obj
@@ -976,6 +985,9 @@
         # so in order to have a consistent API, let it go through.
         pass
 
+    def get_storage(self, space):
+        raise OperationError(space.w_TypeError, space.wrap("Cannot get array 
interface on scalars in pypy"))
+
 class VirtualArray(BaseArray):
     """
     Class for representing virtual arrays, such as binary ops or ufuncs
@@ -1263,6 +1275,9 @@
             a_iter = a_iter.next(len(array.shape))
         return array
 
+    def get_storage(self, space):
+        return self.parent.get_storage(space)
+
 class W_NDimArray(BaseArray):
     """ A class representing contiguous array. We know that each iteration
     by say ufunc will increase the data index by one
@@ -1325,6 +1340,9 @@
     def debug_repr(self):
         return 'Array'
 
+    def get_storage(self, space):
+        return self.storage
+
     def __del__(self):
         lltype.free(self.storage, flavor='raw', track_allocation=False)
 
@@ -1444,6 +1462,7 @@
     __repr__ = interp2app(BaseArray.descr_repr),
     __str__ = interp2app(BaseArray.descr_str),
     __debug_repr__ = interp2app(BaseArray.descr_debug_repr),
+    __array_interface__ = GetSetProperty(BaseArray.descr_array_iface),
 
     dtype = GetSetProperty(BaseArray.descr_get_dtype),
     shape = GetSetProperty(BaseArray.descr_get_shape,
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
@@ -1114,6 +1114,16 @@
         b = a[0].copy()
         assert (b == zeros(10)).all()
 
+    def test_array_interface(self):
+        from numpypy import array
+        a = array([1, 2, 3])
+        i = a.__array_interface__
+        assert isinstance(i['data'][0], int)
+        a = a[::2]
+        i = a.__array_interface__
+        assert isinstance(i['data'][0], int)
+        raises(TypeError, getattr, array(3), '__array_interface__')
+
 class AppTestSupport(BaseNumpyAppTest):
     def setup_class(cls):
         import struct
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to