Author: Amaury Forgeot d'Arc <[email protected]>
Branch: 
Changeset: r65071:d77e4dd8ef04
Date: 2013-06-28 16:47 +0200
http://bitbucket.org/pypy/pypy/changeset/d77e4dd8ef04/

Log:    Fix translation, int%float is not RPython. Don't use floats when
        integer arithmetics is possible, I'm sure there were possible
        rounding errors as well.

        This also fixes a bug for empty arrays.

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
@@ -627,27 +627,29 @@
                                                                    w_dtype))
         else:
             dtype = self.get_dtype()
-        factor = float(dtype.get_size()) / self.get_dtype().get_size()
-        if self.get_size() % factor != 0:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "new type not compatible with array."))
+        old_itemsize = self.get_dtype().get_size()
+        new_itemsize = dtype.get_size()
         impl = self.implementation
         new_shape = self.get_shape()
-        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:
+        dims = len(new_shape)
+        if dims == 0:
+            # Cannot resize scalars
+            if old_itemsize != new_itemsize:
                 raise OperationError(space.w_ValueError, space.wrap(
                     "new type not compatible with array shape"))
+        else:
+            if dims == 1 or impl.get_strides()[0] < impl.get_strides()[-1]:
+                # Column-major, resize first dimension
+                if new_shape[0] * old_itemsize % new_itemsize != 0:
+                    raise OperationError(space.w_ValueError, space.wrap(
+                        "new type not compatible with array."))
+                new_shape[0] = new_shape[0] * old_itemsize / new_itemsize
+            else:
+                # Row-major, resize last dimension
+                if new_shape[-1] * old_itemsize % new_itemsize != 0:
+                    raise OperationError(space.w_ValueError, space.wrap(
+                        "new type not compatible with array."))
+                new_shape[-1] = new_shape[-1] * old_itemsize / new_itemsize
         return W_NDimArray(impl.get_view(self, dtype, new_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
@@ -1432,6 +1432,11 @@
         x = array(range(15), dtype='int16').reshape(3,5).T
         assert x.view('int8').shape == (10, 3)
 
+    def test_ndarray_view_empty(self):
+        from numpypy import array, int8, int16, dtype
+        x = array([], dtype=[('a', int8), ('b', int8)])
+        y = x.view(dtype=int16)
+
     def test_scalar_view(self):
         from numpypy import int64, array
         a = array(0, dtype='int32')
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to