[pypy-commit] pypy default: add multiarray.empty_like()

bdkearns noreply at buildbot.pypy.org
Wed Dec 18 10:15:45 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r68463:a0190dd8fc77
Date: 2013-12-18 03:52 -0500
http://bitbucket.org/pypy/pypy/changeset/a0190dd8fc77/

Log:	add multiarray.empty_like()

diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -11,6 +11,7 @@
         'zeros': 'interp_numarray.zeros',
         'empty': 'interp_numarray.zeros',
         'ones': 'interp_numarray.ones',
+        'empty_like': 'interp_numarray.empty_like',
         '_reconstruct' : 'interp_numarray._reconstruct',
         'scalar' : 'interp_numarray.build_scalar',
         'dot': 'interp_arrayops.dot',
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
@@ -1465,6 +1465,16 @@
     w_arr.fill(space, one)
     return space.wrap(w_arr)
 
+ at unwrap_spec(subok=bool)
+def empty_like(space, w_a, w_dtype=None, w_order=None, subok=True):
+    w_a = convert_to_array(space, w_a)
+    if subok and type(w_a) is not W_NDimArray:
+        raise OperationError(space.w_NotImplementedError, space.wrap(
+            "subtypes not implemented"))
+    if w_dtype is None:
+        w_dtype = w_a.get_dtype()
+    return zeros(space, w_a.descr_get_shape(space), w_dtype)
+
 def _reconstruct(space, w_subtype, w_shape, w_dtype):
     return descr_new_array(space, w_subtype, w_shape, w_dtype)
 
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
@@ -345,7 +345,7 @@
         # TypeError
         raises((TypeError, AttributeError), 'x.ndim = 3')
 
-    def test_init(self):
+    def test_zeros(self):
         from numpypy import zeros
         a = zeros(15)
         # Check that storage was actually zero'd.
@@ -355,6 +355,33 @@
         assert a[13] == 5.3
         assert zeros(()).shape == ()
 
+    def test_empty_like(self):
+        import numpy as np
+        a = np.zeros((2, 3))
+        assert a.shape == (2, 3)
+        a[0,0] = 1
+        b = np.empty_like(a)
+        assert b.shape == a.shape
+        assert b.dtype == a.dtype
+        assert b[0,0] != 1
+        b = np.empty_like(a, dtype='i4')
+        assert b.shape == a.shape
+        assert b.dtype == np.dtype('i4')
+        assert b[0,0] != 1
+        b = np.empty_like([1,2,3])
+        assert b.shape == (3,)
+        assert b.dtype == np.int_
+
+        class A(np.ndarray):
+            pass
+        import sys
+        if '__pypy__' not in sys.builtin_module_names:
+            b = np.empty_like(A((2, 3)))
+            assert b.shape == (2, 3)
+            assert type(b) is A
+        else:
+            raises(NotImplementedError, np.empty_like, A((2, 3)))
+
     def test_size(self):
         from numpypy import array,arange,cos
         assert array(3).size == 1


More information about the pypy-commit mailing list