[pypy-commit] pypy ndarray-view: implement for zeroD arrays, skip for dtype scalars

mattip noreply at buildbot.pypy.org
Fri Jun 28 14:40:44 CEST 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: ndarray-view
Changeset: r65061:e6bc534ed503
Date: 2013-06-28 15:34 +0300
http://bitbucket.org/pypy/pypy/changeset/e6bc534ed503/

Log:	implement for zeroD arrays, skip for dtype scalars

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
@@ -63,6 +63,11 @@
     def transpose(self, _):
         return self
 
+    def get_view(self, orig_array, dtype, new_shape):
+        scalar = Scalar(dtype)
+        scalar.value = self.value.convert_to(dtype)
+        return scalar
+
     def get_real(self, orig_array):
         if self.dtype.is_complex_type():
             scalar = Scalar(self.dtype.float_type)
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -240,6 +240,16 @@
         v = self.convert_to(self.get_dtype(space))
         return self.get_dtype(space).itemtype.round(v, decimals)
 
+    def descr_view(self, space, w_dtype):
+        from pypy.module.micronumpy.interp_dtype import W_Dtype
+        dtype = space.interp_w(W_Dtype,
+            space.call_function(space.gettypefor(W_Dtype), w_dtype))
+        if dtype.get_size() != self.get_dtype(space).get_size():
+            raise OperationError(space.w_ValueError, space.wrap(
+                "new type not compatible with array."))
+        raise OperationError(space.w_NotImplementedError, space.wrap(
+            "view not implelemnted yet"))
+
 class W_BoolBox(W_GenericBox, PrimitiveBox):
     descr__new__, _get_dtype, descr_reduce = new_dtype_getter("bool")
 
@@ -507,6 +517,7 @@
     all = interp2app(W_GenericBox.descr_all),
     ravel = interp2app(W_GenericBox.descr_ravel),
     round = interp2app(W_GenericBox.descr_round),
+    view = interp2app(W_GenericBox.descr_view),
 )
 
 W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
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
@@ -628,23 +628,27 @@
         else:
             dtype = self.get_dtype()
         factor = float(dtype.get_size()) / self.get_dtype().get_size()
-        print 'new dtype',dtype,'self dtype',self.get_dtype(),'factor',factor
         if self.get_size() % factor != 0:
             raise OperationError(space.w_ValueError, space.wrap(
                 "new type not compatible with array."))
+        impl = self.implementation
         new_shape = self.get_shape()
-        impl = self.implementation
-        if impl.get_strides()[0] < impl.get_strides()[-1]:
-            new_shape[0] = int(new_shape[0] / factor)
-            if new_shape[0] == 0:
+        if len(new_shape) > 0:
+            if impl.get_strides()[0] < impl.get_strides()[-1]:
+                new_shape[0] = int(new_shape[0] / factor)
+                if new_shape[0] == 0:
+                    raise OperationError(space.w_ValueError, space.wrap(
+                        "new type not compatible with array shape"))
+            else:
+                new_shape[-1] = int(new_shape[-1] / factor)
+                if new_shape[-1] == 0:
+                    raise OperationError(space.w_ValueError, space.wrap(
+                        "new type not compatible with array shape"))
+        else:
+            if factor != 1:
                 raise OperationError(space.w_ValueError, space.wrap(
                     "new type not compatible with array shape"))
-        else:
-            new_shape[-1] = int(new_shape[-1] / factor)
-            if new_shape[-1] == 0:
-                raise OperationError(space.w_ValueError, space.wrap(
-                    "new type not compatible with array shape"))
-        return W_NDimArray(self.implementation.get_view(self, dtype, new_shape))
+        return W_NDimArray(impl.get_view(self, dtype, new_shape))
 
 
     # --------------------- operations ----------------------------
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
@@ -1433,10 +1433,15 @@
         assert x.view('int8').shape == (10, 3)
 
     def test_scalar_view(self):
-        from numpypy import int64
+        from numpypy import int64, array
+        a = array(0, dtype='int32')
+        b = a.view(dtype='float32')
+        assert b.shape == ()
+        assert b == 0
         s = int64(12)
-        exc = raises(TypeError, s.view, dtype='int8')
-        assert exc.value[0] == "view() takes no keyword arguments"
+        exc = raises(ValueError, s.view, 'int8')
+        assert exc.value[0] == "new type not compatible with array."
+        skip('not implemented yet')
         assert s.view('double') < 7e-323
 
     def test_tolist_scalar(self):


More information about the pypy-commit mailing list