[pypy-commit] pypy ndarray-subtype: helper function to create subtypes (when needed) without calling their __new__ (amaury)

mattip noreply at buildbot.pypy.org
Thu Jul 4 01:42:29 CEST 2013


Author: Matti Picus <matti.picus at gmail.com>
Branch: ndarray-subtype
Changeset: r65180:3e11268b64cf
Date: 2013-07-04 02:41 +0300
http://bitbucket.org/pypy/pypy/changeset/3e11268b64cf/

Log:	helper function to create subtypes (when needed) without calling
	their __new__ (amaury)

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,6 +10,14 @@
             space.isinstance_w(w_obj, space.w_list) or
             isinstance(w_obj, W_NDimArray))
 
+def wrap_impl(space, cls, impl):
+    if space.is_w(space.type(cls), space.gettypefor(W_NDimArray)):
+        ret = W_NDimArray(impl)
+    else:
+        ret = space.allocate_instance(W_NDimArray, space.type(cls))
+        print 'created',space.type(ret)
+        W_NDimArray.__init__(ret, impl)
+    return ret
 
 class ArrayArgumentException(Exception):
     pass
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
@@ -3,7 +3,7 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty, make_weakref_descr
 from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
 from pypy.module.micronumpy.base import W_NDimArray, convert_to_array,\
-     ArrayArgumentException, issequence_w
+     ArrayArgumentException, issequence_w, wrap_impl
 from pypy.module.micronumpy import interp_dtype, interp_ufuncs, interp_boxes,\
      interp_arrayops
 from pypy.module.micronumpy.strides import find_shape_and_elems,\
@@ -298,7 +298,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 W_NDimArray(new_impl)
+            return wrap_impl(space, self, new_impl)
         # Create copy with contiguous data
         arr = self.descr_copy(space)
         if arr.get_size() > 0:
@@ -902,7 +902,6 @@
     dtype = space.interp_w(interp_dtype.W_Dtype,
           space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
     shape = _find_shape(space, w_shape, dtype)
-    print 'desc_new_array(space,',w_subtype,',',shape,',',dtype,'...)'
     if not shape:
         return W_NDimArray.new_scalar(space, dtype)
     if space.is_w(w_subtype, space.gettypefor(W_NDimArray)):
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
@@ -1476,6 +1476,7 @@
             def __new__(subtype, shape, dtype):
                 self = ndarray.__new__(subtype, shape, dtype)
                 self.id = 'subtype'
+                print 'called new'
                 return self
         a = C([2, 2], int)
         assert isinstance(a, C)
@@ -1483,6 +1484,14 @@
         assert a.shape == (2, 2)
         assert a.dtype is dtype(int)
         assert a.id == 'subtype'
+        print '1'
+        a = a.reshape(1, 4)
+        print '2'
+        b = a.reshape(4, 1)
+        print '3'
+        assert isinstance(b, C)
+        #make sure __new__ was not called
+        assert not getattr(b, 'id', None)
         a.fill(3)
         b = a[0]
         assert isinstance(b, C)


More information about the pypy-commit mailing list