[pypy-commit] pypy numpy-fixes: test, fix for calling __array_finalize__ on set_shape (needed for subtypes)

mattip noreply at buildbot.pypy.org
Sat May 16 20:42:57 CEST 2015


Author: mattip <matti.picus at gmail.com>
Branch: numpy-fixes
Changeset: r77346:0d5d19ecc111
Date: 2015-05-16 19:57 +0300
http://bitbucket.org/pypy/pypy/changeset/0d5d19ecc111/

Log:	test, fix for calling __array_finalize__ on set_shape (needed for
	subtypes)

diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -53,6 +53,11 @@
     def descr_set_shape(self, space, w_new_shape):
         shape = get_shape_from_iterable(space, self.get_size(), w_new_shape)
         self.implementation = self.implementation.set_shape(space, self, shape)
+        w_cls = space.type(self)
+        if not space.is_w(w_cls, space.gettypefor(W_NDimArray)):
+            # numpy madness - allow __array_finalize__(self, obj)
+            # to run, in MaskedArray this modifies obj._mask
+            wrap_impl(space, w_cls, self, self.implementation)
 
     def descr_get_strides(self, space):
         strides = self.implementation.get_strides()
diff --git a/pypy/module/micronumpy/test/test_subtype.py b/pypy/module/micronumpy/test/test_subtype.py
--- a/pypy/module/micronumpy/test/test_subtype.py
+++ b/pypy/module/micronumpy/test/test_subtype.py
@@ -126,7 +126,7 @@
         import numpy as np
         class InfoArray(np.ndarray):
             def __new__(subtype, shape, dtype=float, buffer=None, offset=0,
-                          strides=None, order='C', info=None):
+                          strides=None, order='C', info=1):
                 obj = np.ndarray.__new__(subtype, shape, dtype, buffer,
                          offset, strides, order)
                 obj.info = info
@@ -134,25 +134,31 @@
 
             def __array_finalize__(self, obj):
                 if obj is None:
-                    print 'finalize with None'
                     return
                 # printing the object itself will crash the test
-                print 'finalize with something',type(obj)
-                self.info = getattr(obj, 'info', None)
+                self.info = 1 + getattr(obj, 'info', 0)
+                if hasattr(obj, 'info'):
+                    obj.info += 100
+
         obj = InfoArray(shape=(3,))
         assert isinstance(obj, InfoArray)
-        assert obj.info is None
-        obj = InfoArray(shape=(3,), info='information')
-        assert obj.info == 'information'
+        assert obj.info == 1
+        obj = InfoArray(shape=(3,), info=10)
+        assert obj.info == 10
         v = obj[1:]
         assert isinstance(v, InfoArray)
         assert v.base is obj
-        assert v.info == 'information'
+        assert v.info == 11
         arr = np.arange(10)
         cast_arr = arr.view(InfoArray)
         assert isinstance(cast_arr, InfoArray)
         assert cast_arr.base is arr
-        assert cast_arr.info is None
+        assert cast_arr.info == 1
+        # Test that setshape calls __array_finalize__
+        cast_arr.shape = (5,2)
+        z = cast_arr.info
+        assert z == 101
+
 
     def test_sub_where(self):
         from numpy import where, ones, zeros, array


More information about the pypy-commit mailing list