[pypy-commit] pypy ndarray-subtype: cleanup, seperate class instances from class types

mattip noreply at buildbot.pypy.org
Fri Jul 5 14:37:27 CEST 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: ndarray-subtype
Changeset: r65198:b82565576443
Date: 2013-07-05 15:36 +0300
http://bitbucket.org/pypy/pypy/changeset/b82565576443/

Log:	cleanup, seperate class instances from class types

diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py
--- a/pypy/module/micronumpy/base.py
+++ b/pypy/module/micronumpy/base.py
@@ -10,19 +10,14 @@
             space.isinstance_w(w_obj, space.w_list) or
             isinstance(w_obj, W_NDimArray))
 
-def wrap_impl(space, cls, impl):
-    if cls is None or space.is_w(space.type(cls), space.gettypefor(W_NDimArray)):
-        ret = W_NDimArray(impl)
+def wrap_impl(space, w_cls, w_instance, impl):
+    if w_cls is None or space.is_w(w_cls, space.gettypefor(W_NDimArray)):
+        w_ret = W_NDimArray(impl)
     else:
-        if space.isinstance_w(cls, space.w_type):
-            #got type, either from __new__ or from view casting
-            ret = space.allocate_instance(W_NDimArray, cls)
-        else:
-            ret = space.allocate_instance(W_NDimArray, space.type(cls))
-        W_NDimArray.__init__(ret, impl)
-        space.call_function(space.getattr(ret, space.wrap('__array_finalize__')),
-                        cls)
-    return ret
+        w_ret = space.allocate_instance(W_NDimArray, w_cls)
+        W_NDimArray.__init__(w_ret, impl)
+        space.call_method(w_ret, space.wrap('__array_finalize__'), w_instance)
+    return w_ret
 
 class ArrayArgumentException(Exception):
     pass
@@ -106,7 +101,7 @@
 
         impl = concrete.SliceArray(offset, strides, backstrides, shape, parent,
                                    orig_arr, dtype)
-        return wrap_impl(space, orig_arr, impl)
+        return wrap_impl(space, space.type(orig_arr), orig_arr, impl)
 
     @staticmethod
     def new_scalar(space, dtype, w_val=None):
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
@@ -260,14 +260,16 @@
         return self.implementation.get_scalar_value()
 
     def descr_copy(self, space):
-        return wrap_impl(space, self, self.implementation.copy(space))
+        return wrap_impl(space, space.type(self),
+                         self, self.implementation.copy(space))
 
     def descr_get_real(self, space):
-        return wrap_impl(space, self, self.implementation.get_real(self))
+        return wrap_impl(space, space.type(self), self,
+                         self.implementation.get_real(self))
 
     def descr_get_imag(self, space):
         ret = self.implementation.get_imag(self)
-        return wrap_impl(space, self, ret)
+        return wrap_impl(space, space.type(self), self, ret)
 
     def descr_set_real(self, space, w_value):
         # copy (broadcast) values into self
@@ -299,7 +301,7 @@
         new_shape = get_shape_from_iterable(space, self.get_size(), w_shape)
         new_impl = self.implementation.reshape(space, self, new_shape)
         if new_impl is not None:
-            return wrap_impl(space, self, new_impl)
+            return wrap_impl(space, space.type(self), self, new_impl)
         # Create copy with contiguous data
         arr = self.descr_copy(space)
         if arr.get_size() > 0:
@@ -464,7 +466,7 @@
             return W_NDimArray.new_scalar(space, dtype, impl.value)
         else:
             new_impl = impl.astype(space, dtype)
-            return wrap_impl(space, self, new_impl)
+            return wrap_impl(space, space.type(self), self, new_impl)
 
     def descr_get_base(self, space):
         impl = self.implementation
@@ -664,7 +666,8 @@
                         "new type not compatible with array."))
                 new_shape[-1] = new_shape[-1] * old_itemsize / new_itemsize
         v = impl.get_view(self, dtype, new_shape)
-        return wrap_impl(space, w_type, v)
+        w_ret = wrap_impl(space, w_type, self, v)
+        return w_ret
 
     # --------------------- operations ----------------------------
 
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
@@ -8,7 +8,7 @@
         cls.w_NoNew = cls.space.appexec([], '''():
             from numpypy import ndarray
             class NoNew(ndarray):
-                def __new__(cls):
+                def __new__(cls, subtype):
                     raise ValueError('should not call __new__')
                 def __array_finalize(self, obj):
                     self.called_finalize = True
@@ -36,15 +36,17 @@
 
             def __array_finalize__(self, obj):
                 if obj is None:
-                    print 'finazlize with None'
+                    print 'finalize with None'
                     return
-                print 'finalize with something'
+                # printing the object itself will crash the test
+                print 'finalize with something',type(obj)
                 self.info = getattr(obj, 'info', None)
         obj = InfoArray(shape=(3,))
         assert isinstance(obj, InfoArray)
         assert obj.info is None
         obj = InfoArray(shape=(3,), info='information')
         assert obj.info == 'information'
+        print 'a'
         v = obj[1:]
         assert isinstance(v, InfoArray)
         assert v.base is obj
@@ -62,19 +64,19 @@
         v = a.view(self.NoNew)
         assert False
 
-    def test_repeat(self):
+    def test_sub_repeat(self):
         assert False
 
-    def test_flatiter(self):
+    def test_sub_flatiter(self):
         assert False
 
-    def test_getitem_filter(self):
+    def test_sub_getitem_filter(self):
         assert False
 
-    def test_getitem_array_int(self):
+    def test_sub_getitem_array_int(self):
         assert False
 
-    def test_round(self):
+    def test_sub_round(self):
         from numpypy import array
         a = array(range(10), dtype=float).view(self.NoNew)
         # numpy compatibility
@@ -85,24 +87,28 @@
         b = a.round(decimal=-1)
         assert not isinstance(b, self.NoNew)
 
-    def test_dot(self):
+    def test_sub_dot(self):
         # the returned type is that of the first argument
         assert False
 
-    def test_reduce(self):
+    def test_sub_reduce(self):
         # i.e. sum, max
         # test for out as well
         assert False
 
-    def test_call2(self):
+    def test_sub_call2(self):
         # c + a vs. a + c, what about array priority?
         assert False
 
-    def test_call1(self):
+    def test_sub_call1(self):
         assert False
 
-    def test_astype(self):
+    def test_sub_astype(self):
         assert False
 
-    def test_reshape(self):
-        assert False
+    def test_sub_reshape(self):
+        from numpypy import array
+        a = array(range(12)).view(self.NoNew)
+        b = a.reshape(3, 4)
+        assert b.called_finalize == True
+


More information about the pypy-commit mailing list