[pypy-commit] pypy numpy-1.10: fix for most all of the 1.10 changes

mattip noreply at buildbot.pypy.org
Wed Nov 11 14:47:12 EST 2015


Author: mattip <matti.picus at gmail.com>
Branch: numpy-1.10
Changeset: r80640:d8f179999b92
Date: 2015-11-11 21:34 +0200
http://bitbucket.org/pypy/pypy/changeset/d8f179999b92/

Log:	fix for most all of the 1.10 changes

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
@@ -44,7 +44,7 @@
         from pypy.module.micronumpy.strides import calc_strides
         if len(shape) > NPY.MAXDIMS:
             raise oefmt(space.w_ValueError,
-                "sequence too large; must be smaller than %d", NPY.MAXDIMS)
+                "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
         try:
             ovfcheck(support.product_check(shape) * dtype.elsize)
         except OverflowError as e:
@@ -69,7 +69,7 @@
         isize = dtype.elsize
         if len(shape) > NPY.MAXDIMS:
             raise oefmt(space.w_ValueError,
-                "sequence too large; must be smaller than %d", NPY.MAXDIMS)
+                "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
         try:
             totalsize = ovfcheck(support.product_check(shape) * isize)
         except OverflowError as e:
diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -444,7 +444,7 @@
 
     @unwrap_spec(axis1=int, axis2=int)
     def descr_swapaxes(self, space, axis1, axis2):
-        return self
+        raise oefmt(space.w_ValueError, 'bad axis1 argument to swapaxes')
 
     def descr_fill(self, space, w_value):
         self.get_dtype(space).coerce(space, w_value)
@@ -573,7 +573,7 @@
         try:
             ofs, dtype = self.dtype.fields[item]
         except KeyError:
-            raise oefmt(space.w_IndexError, "invalid index")
+            raise oefmt(space.w_ValueError, "no field of name %s", item)
 
         from pypy.module.micronumpy.types import VoidType
         if isinstance(dtype.itemtype, VoidType):
diff --git a/pypy/module/micronumpy/concrete.py b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -457,7 +457,7 @@
     def set_shape(self, space, orig_array, new_shape):
         if len(new_shape) > NPY.MAXDIMS:
             raise oefmt(space.w_ValueError,
-                "sequence too large; must be smaller than %d", NPY.MAXDIMS)
+                "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
         try:
             ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
         except OverflowError as e:
@@ -601,7 +601,7 @@
     def set_shape(self, space, orig_array, new_shape):
         if len(new_shape) > NPY.MAXDIMS:
             raise oefmt(space.w_ValueError,
-                "sequence too large; must be smaller than %d", NPY.MAXDIMS)
+                "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
         try:
             ovfcheck(support.product_check(new_shape) * self.dtype.elsize)
         except OverflowError as e:
diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -18,7 +18,7 @@
         raise oefmt(space.w_TypeError,
                     "argument 1 must be numpy.dtype, not %T", w_dtype)
     if w_dtype.elsize == 0:
-        raise oefmt(space.w_ValueError, "itemsize cannot be zero")
+        raise oefmt(space.w_TypeError, "Empty data-type")
     if not space.isinstance_w(w_state, space.w_str):
         raise oefmt(space.w_TypeError, "initializing object must be a string")
     if space.len_w(w_state) != w_dtype.elsize:
diff --git a/pypy/module/micronumpy/descriptor.py b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -217,6 +217,8 @@
             endian = ignore
         if self.num == NPY.UNICODE:
             size >>= 2
+        if self.num == NPY.OBJECT:
+            return "%s%s" %(endian, basic)
         return "%s%s%s" % (endian, basic, size)
 
     def descr_get_descr(self, space, style='descr', force_dict=False):
@@ -485,7 +487,12 @@
 
     def descr_str(self, space):
         if self.fields:
-            return space.str(self.descr_get_descr(space, style='str'))
+            r = self.descr_get_descr(space, style='str')
+            name = space.str_w(space.str(self.w_box_type))
+            if name != "<type 'numpy.void'>":
+                boxname = space.str(self.w_box_type)
+                r = space.newtuple([self.w_box_type, r])
+            return space.str(r)
         elif self.subdtype is not None:
             return space.str(space.newtuple([
                 self.subdtype.descr_get_str(space),
@@ -501,6 +508,9 @@
             return space.wrap("dtype('S1')")
         if self.fields:
             r = self.descr_get_descr(space, style='repr')
+            name = space.str_w(space.str(self.w_box_type))
+            if name != "<type 'numpy.void'>":
+                r = space.newtuple([space.wrap(self.w_box_type), r])
         elif self.subdtype is not None:
             r = space.newtuple([self.subdtype.descr_get_str(space),
                                 self.descr_get_shape(space)])
@@ -944,7 +954,7 @@
     shape_w = space.fixedview(w_shape)
     if len(shape_w) < 1:
         return None
-    elif len(shape_w) == 1 and space.isinstance_w(shape_w[0], space.w_tuple):
+    elif space.isinstance_w(shape_w[0], space.w_tuple):
         # (base_dtype, new_dtype) dtype spectification
         return None
     shape = []
@@ -999,12 +1009,16 @@
         if len(spec) > 0:
             # this is (base_dtype, new_dtype) so just make it a union by setting both
             # parts' offset to 0
-            try:
-                dtype1 = make_new_dtype(space, w_subtype, w_shape, alignment)
-            except:
-                raise
-            raise oefmt(space.w_NotImplementedError, 
-                "(base_dtype, new_dtype) dtype spectification discouraged, not implemented")
+            w_dtype1 = make_new_dtype(space, w_subtype, w_shape, alignment)
+            assert isinstance(w_dtype, W_Dtype)
+            assert isinstance(w_dtype1, W_Dtype)
+            if (w_dtype.elsize != 0 and w_dtype1.elsize != 0 and 
+                    w_dtype1.elsize != w_dtype.elsize):
+                raise oefmt(space.w_ValueError,
+                    'mismatch in size of old and new data-descriptor')
+            retval = W_Dtype(w_dtype.itemtype, w_dtype.w_box_type,
+                    names=w_dtype1.names[:], fields=w_dtype1.fields.copy())
+            return retval
     if space.is_none(w_dtype):
         return cache.w_float64dtype
     if space.isinstance_w(w_dtype, w_subtype):
@@ -1034,19 +1048,22 @@
     elif space.isinstance_w(w_dtype, space.w_tuple):
         w_dtype0 = space.getitem(w_dtype, space.wrap(0))
         w_dtype1 = space.getitem(w_dtype, space.wrap(1))
-        if space.isinstance_w(w_dtype0, space.w_type) and \
-           space.isinstance_w(w_dtype1, space.w_list):
-            #obscure api - (subclass, spec). Ignore the subclass
-            return make_new_dtype(space, w_subtype, w_dtype1, alignment, 
-                        copy=copy, w_shape=w_shape, w_metadata=w_metadata)
-        subdtype = make_new_dtype(space, w_subtype, w_dtype0, alignment, copy)
-        assert isinstance(subdtype, W_Dtype)
-        if subdtype.elsize == 0:
-            name = "%s%d" % (subdtype.kind, space.int_w(w_dtype1))
+        # create a new dtype object
+        l_side = make_new_dtype(space, w_subtype, w_dtype0, alignment, copy)
+        assert isinstance(l_side, W_Dtype)
+        if l_side.elsize == 0 and space.isinstance_w(w_dtype1, space.w_int):
+            #(flexible_dtype, itemsize)
+            name = "%s%d" % (l_side.kind, space.int_w(w_dtype1))
             retval = make_new_dtype(space, w_subtype, space.wrap(name), alignment, copy)
-        else:
-            retval = make_new_dtype(space, w_subtype, w_dtype0, alignment, copy, w_shape=w_dtype1)
-        return _set_metadata_and_copy(space, w_metadata, retval, copy)
+            return _set_metadata_and_copy(space, w_metadata, retval, copy)
+        elif (space.isinstance_w(w_dtype1, space.w_int) or
+                space.isinstance_w(w_dtype1, space.w_tuple) or 
+                space.isinstance_w(w_dtype1, space.w_list) or 
+                isinstance(w_dtype1, W_NDimArray)):
+            #(fixed_dtype, shape) or (base_dtype, new_dtype)
+            retval = make_new_dtype(space, w_subtype, l_side, alignment,
+                                    copy, w_shape=w_dtype1)
+            return _set_metadata_and_copy(space, w_metadata, retval, copy)
     elif space.isinstance_w(w_dtype, space.w_dict):
         return _set_metadata_and_copy(space, w_metadata,
                 dtype_from_dict(space, w_dtype, alignment), copy)
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
@@ -278,7 +278,7 @@
     def getfield(self, space, field):
         dtype = self.get_dtype()
         if field not in dtype.fields:
-            raise oefmt(space.w_ValueError, "field named %s not found", field)
+            raise oefmt(space.w_ValueError, "no field of name %s", field)
         arr = self.implementation
         ofs, subdtype = arr.dtype.fields[field][:2]
         # ofs only changes start
@@ -489,10 +489,8 @@
         numpy.swapaxes : equivalent function
         """
         if axis1 == axis2:
-            return self
+            return self.descr_view(space)
         n = self.ndims()
-        if n <= 1:
-            return self
         if axis1 < 0:
             axis1 += n
         if axis2 < 0:
@@ -501,6 +499,8 @@
             raise oefmt(space.w_ValueError, "bad axis1 argument to swapaxes")
         if axis2 < 0 or axis2 >= n:
             raise oefmt(space.w_ValueError, "bad axis2 argument to swapaxes")
+        if n <= 1:
+            return self
         return self.implementation.swapaxes(space, self, axis1, axis2)
 
     def descr_nonzero(self, space):
@@ -899,7 +899,7 @@
                     if cur_shape[i] != 1:
                         raise OperationError(space.w_ValueError, space.wrap(
                             "cannot select an axis to squeeze out "
-                            "which has size greater than one"))
+                            "which has size not equal to one"))
                 else:
                     new_shape.append(cur_shape[i])
         else:
@@ -1374,7 +1374,7 @@
     shape = shape_converter(space, w_shape, dtype)
     if len(shape) > NPY.MAXDIMS:
         raise oefmt(space.w_ValueError,
-            "sequence too large; must be smaller than %d", NPY.MAXDIMS)
+            "sequence too large; cannot be greater than %d", NPY.MAXDIMS)
     if not space.is_none(w_buffer):
         if (not space.is_none(w_strides)):
             strides = [space.int_w(w_i) for w_i in
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -487,10 +487,6 @@
         for o in [object, O]:
             assert np.dtype(o).str == '|O'
         # Issue gh-2798
-        if '__pypy__' in sys.builtin_module_names:
-            a = np.array(['a'], dtype="O")
-            raises(NotImplementedError, a.astype, ("O", [("name", "O")]))
-            skip("(base_dtype, new_dtype) dtype specification discouraged")
         a = np.array(['a'], dtype="O").astype(("O", [("name", "O")]))
         assert a[0] == 'a'
         assert a != 'a'
diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -838,13 +838,19 @@
 
     def test_build_scalar(self):
         from numpy import dtype
+        import sys
         try:
             from numpy.core.multiarray import scalar
         except ImportError:
             from numpy import scalar
         exc = raises(TypeError, scalar, int, 2)
         assert exc.value[0] == 'argument 1 must be numpy.dtype, not type'
-        a = scalar(dtype('void'), 'abc')
+        if '__pypy__' in sys.builtin_module_names:
+            exc = raises(TypeError, scalar, dtype('void'), 'abc')
+        else:
+            a = scalar(dtype('void'), 'abc')
+            exc = raises(TypeError, str, a)
+        assert exc.value[0] == 'Empty data-type'
         exc = raises(TypeError, scalar, dtype(float), 2.5)
         assert exc.value[0] == 'initializing object must be a string'
         exc = raises(ValueError, scalar, dtype(float), 'abc')
@@ -2398,6 +2404,7 @@
 
     def test_data(self):
         from numpy import array
+        import sys
         a = array([1, 2, 3, 4], dtype='i4')
         assert a.data[0] == '\x01'
         assert a.data[1] == '\x00'
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -450,10 +450,12 @@
 
     @specialize.argtype(1)
     def round(self, v, decimals=0):
-        if decimals != 0:
-            # numpy 1.9.0 compatible
-            return v
-        return Float64(self.space).box(self.unbox(v))
+        if decimals == 0:
+            return Float64(self.space).box(self.unbox(v))
+        # numpy 1.10 compatibility
+        raise oefmt(self.space.w_TypeError, "ufunc casting failure")
+            
+            
 
 class Integer(Primitive):
     _mixin_ = True


More information about the pypy-commit mailing list