Author: Maciej Fijalkowski <[email protected]>
Branch: missing-ndarray-attributes
Changeset: r58648:758f72ee68e7
Date: 2012-10-31 17:18 +0200
http://bitbucket.org/pypy/pypy/changeset/758f72ee68e7/

Log:    data

diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py 
b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -7,9 +7,11 @@
      calculate_broadcast_strides, calculate_dot_strides
 from pypy.module.micronumpy.iter import Chunk, Chunks, NewAxisChunk, 
RecordChunk
 from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.buffer import RWBuffer
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib import jit
-from pypy.rlib.rawstorage import free_raw_storage
+from pypy.rlib.rawstorage import free_raw_storage, raw_storage_getitem,\
+     raw_storage_setitem
 from pypy.module.micronumpy.arrayimpl.sort import argsort_array
 from pypy.rlib.debug import make_sure_not_resized
 
@@ -244,6 +246,9 @@
     def get_storage(self):
         return self.storage
 
+    def get_buffer(self, space):
+        return ArrayBuffer(self)
+
 class ConcreteArray(BaseConcreteArray):
     def __init__(self, shape, dtype, order, strides, backstrides):
         make_sure_not_resized(shape)
@@ -356,3 +361,17 @@
             new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
         return SliceArray(self.start, new_strides, new_backstrides, new_shape,
                           self, orig_array)
+
+class ArrayBuffer(RWBuffer):
+    def __init__(self, impl):
+        self.impl = impl
+
+    def getitem(self, item):
+        return raw_storage_getitem(lltype.Char, self.impl.storage, item)
+
+    def setitem(self, item, v):
+        return raw_storage_setitem(self.impl.storage, item,
+                                   rffi.cast(lltype.Char, v))
+
+    def getlength(self):
+        return self.impl.size
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py 
b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -104,3 +104,8 @@
 
     def base(self):
         return None
+
+    def get_buffer(self, space):
+        raise OperationError(space.w_ValueError, space.wrap(
+            "cannot point buffer to a scalar"))
+
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
@@ -422,17 +422,12 @@
         loop.clip(space, self, shape, min, max, out)
         return out
 
-    def descr_ctypes(self, space):
+    def descr_get_ctypes(self, space):
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "ctypes not implemented yet"))
 
-    def descr_cumprod(self, space, w_axis=None, w_dtype=None, w_out=None): 
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "cumprod not implemented yet"))
-
-    def descr_data(self, space):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "data not implemented yet"))
+    def descr_get_data(self, space):
+        return self.implementation.get_buffer(space)
 
     def descr_diagonal(self, space, w_offset=0, w_axis1=0, w_axis2=1): 
         raise OperationError(space.w_NotImplementedError, space.wrap(
@@ -805,6 +800,9 @@
     byteswap = interp2app(W_NDimArray.descr_byteswap),
     choose   = interp2app(W_NDimArray.descr_choose),
     clip     = interp2app(W_NDimArray.descr_clip),
+    data     = GetSetProperty(W_NDimArray.descr_get_data),
+
+    ctypes = GetSetProperty(W_NDimArray.descr_get_ctypes), # XXX unimplemented
 
     __array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
 )
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
@@ -1608,6 +1608,16 @@
         assert (a.clip(-2, 13, out=a) == [1, 2, 13, -2, 12]).all()
         assert (a == [1, 2, 13, -2, 12]).all()
 
+    def test_data(self):
+        from _numpypy import array
+        a = array([1, 2, 3, 4], dtype='i4')
+        assert a.data[0] == '\x01'
+        assert a.data[1] == '\x00'
+        assert a.data[4] == '\x02'
+        a.data[4] = '\xff'
+        assert a[1] == 0xff
+        assert len(a.data) == 16
+
 class AppTestMultiDim(BaseNumpyAppTest):
     def test_init(self):
         import _numpypy
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to