[pypy-commit] pypy default: support ndarray.reshape(()) to reshape to scalar

bdkearns noreply at buildbot.pypy.org
Wed Nov 13 04:38:09 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r68001:b89a1dc6a9e1
Date: 2013-11-12 22:32 -0500
http://bitbucket.org/pypy/pypy/changeset/b89a1dc6a9e1/

Log:	support ndarray.reshape(()) to reshape to scalar

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
@@ -73,6 +73,8 @@
             return SliceArray(self.start, new_strides, new_backstrides,
                               new_shape, self, orig_array)
         else:
+            if self.get_size() == 1 and len(new_shape) == 0:
+                return scalar.Scalar(self.dtype, self.getitem(0))
             return None
 
     def get_view(self, orig_array, 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
@@ -710,7 +710,14 @@
 
     def test_reshape(self):
         from numpypy import array, zeros
+        for a in [array(1), array([1])]:
+            for s in [(), (1,)]:
+                b = a.reshape(s)
+                assert b.shape == s
+                assert (b == [1]).all()
         a = array(range(12))
+        exc = raises(ValueError, "b = a.reshape(())")
+        assert str(exc.value) == "total size of new array must be unchanged"
         exc = raises(ValueError, "b = a.reshape((3, 10))")
         assert str(exc.value) == "total size of new array must be unchanged"
         b = a.reshape((3, 4))


More information about the pypy-commit mailing list