[pypy-commit] pypy default: fix astype creation to match memory layout of source array

mattip noreply at buildbot.pypy.org
Thu Apr 23 21:25:11 CEST 2015


Author: mattip <matti.picus at gmail.com>
Branch: 
Changeset: r76907:d198d926afb8
Date: 2015-04-23 22:24 +0300
http://bitbucket.org/pypy/pypy/changeset/d198d926afb8/

Log:	fix astype creation to match memory layout of source array

diff --git a/pypy/module/micronumpy/concrete.py b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -328,8 +328,11 @@
         return ArrayBuffer(self, readonly)
 
     def astype(self, space, dtype):
-        strides, backstrides = calc_strides(self.get_shape(), dtype,
-                                                    self.order)
+        # we want to create a new array, but must respect the strides
+        # in self. So find a factor of the itemtype.elsize, and use this
+        factor = float(dtype.elsize) / self.dtype.elsize
+        strides = [int(factor*s) for s in self.get_strides()]
+        backstrides = [int(factor*s) for s in self.get_backstrides()]
         impl = ConcreteArray(self.get_shape(), dtype, self.order,
                              strides, backstrides)
         loop.setslice(space, impl.get_shape(), impl, self)
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
@@ -2183,7 +2183,8 @@
         assert b.dtype == 'bool'
 
         a = arange(6, dtype='f4').reshape(2,3)
-        b = a.astype('i4')
+        b = a.T.astype('i4')
+        assert (a.T.strides == b.strides)
 
         a = array('x').astype('S3').dtype
         assert a.itemsize == 3


More information about the pypy-commit mailing list