[pypy-commit] pypy default: cleanups

bdkearns noreply at buildbot.pypy.org
Fri Feb 28 18:20:10 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r69562:a6b53dfd3c04
Date: 2014-02-28 11:59 -0500
http://bitbucket.org/pypy/pypy/changeset/a6b53dfd3c04/

Log:	cleanups

diff --git a/pypy/module/micronumpy/arrayops.py b/pypy/module/micronumpy/arrayops.py
--- a/pypy/module/micronumpy/arrayops.py
+++ b/pypy/module/micronumpy/arrayops.py
@@ -8,7 +8,6 @@
     shape_agreement_multiple
 
 
-
 def where(space, w_arr, w_x=None, w_y=None):
     """where(condition, [x, y])
 
@@ -199,8 +198,7 @@
     choices = [convert_to_array(space, w_item) for w_item
                in space.listview(w_choices)]
     if not choices:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("choices list cannot be empty"))
+        raise oefmt(space.w_ValueError, "choices list cannot be empty")
     if space.is_none(w_out):
         w_out = None
     elif not isinstance(w_out, W_NDimArray):
@@ -219,11 +217,9 @@
     mode = clipmode_converter(space, w_mode)
 
     if not w_indices:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("indice list cannot be empty"))
+        raise oefmt(space.w_ValueError, "indices list cannot be empty")
     if not w_values:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("value list cannot be empty"))
+        raise oefmt(space.w_ValueError, "value list cannot be empty")
 
     dtype = arr.get_dtype()
 
@@ -243,8 +239,9 @@
 
         if index < 0 or index >= arr.get_size():
             if mode == NPY.RAISE:
-                raise OperationError(space.w_IndexError, space.wrap(
-                    "index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size())))
+                raise oefmt(space.w_IndexError,
+                    "index %d is out of bounds for axis 0 with size %d",
+                    index, arr.get_size())
             elif mode == NPY.WRAP:
                 index = index % arr.get_size()
             elif mode == NPY.CLIP:
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
@@ -18,7 +18,6 @@
 from pypy.module.micronumpy.concrete import VoidBoxStorage
 from pypy.module.micronumpy.flagsobj import W_FlagsObject
 
-
 MIXIN_32 = (W_IntObject.typedef,) if LONG_BIT == 32 else ()
 MIXIN_64 = (W_IntObject.typedef,) if LONG_BIT == 64 else ()
 
@@ -474,16 +473,13 @@
             except IndexError:
                 if indx < 0:
                     indx += len(self.dtype.names)
-                raise OperationError(space.w_IndexError, space.wrap(
-                    "invalid index (%d)" % indx))
+                raise oefmt(space.w_IndexError, "invalid index (%d)", indx)
         else:
-            raise OperationError(space.w_IndexError, space.wrap(
-                "invalid index"))
+            raise oefmt(space.w_IndexError, "invalid index")
         try:
             ofs, dtype = self.dtype.fields[item]
         except KeyError:
-            raise OperationError(space.w_IndexError, space.wrap(
-                "invalid index"))
+            raise oefmt(space.w_IndexError, "invalid index")
 
         from pypy.module.micronumpy.types import VoidType
         if isinstance(dtype.itemtype, VoidType):
@@ -499,13 +495,11 @@
         if space.isinstance_w(w_item, space.w_basestring):
             item = space.str_w(w_item)
         else:
-            raise OperationError(space.w_IndexError, space.wrap(
-                "invalid index"))
+            raise oefmt(space.w_IndexError, "invalid index")
         try:
             ofs, dtype = self.dtype.fields[item]
         except KeyError:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap("field named %s not found" % item))
+            raise oefmt(space.w_ValueError, "field named %s not found", item)
         dtype.itemtype.store(self.arr, self.ofs, ofs,
                              dtype.coerce(space, w_value))
 
@@ -531,10 +525,8 @@
 
 class W_UnicodeBox(W_CharacterBox):
     def descr__new__unicode_box(space, w_subtype, w_arg):
-        raise OperationError(space.w_NotImplementedError, space.wrap("Unicode is not supported yet"))
-
+        raise oefmt(space.w_NotImplementedError, "Unicode is not supported yet")
         from pypy.module.micronumpy.descriptor import new_unicode_dtype
-
         arg = space.unicode_w(space.unicode_from_object(w_arg))
         # XXX size computations, we need tests anyway
         arr = VoidBoxStorage(len(arg), new_unicode_dtype(space, len(arg)))
@@ -543,6 +535,7 @@
         #    arr.storage[i] = arg[i]
         return W_UnicodeBox(arr, 0, arr.dtype)
 
+
 W_GenericBox.typedef = TypeDef("generic",
     __module__ = "numpy",
 
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
@@ -191,8 +191,7 @@
                         count -= 1
                 if count == shape_len:
                     raise IndexError # but it's still not a single item
-                raise OperationError(space.w_IndexError,
-                                     space.wrap("invalid index"))
+                raise oefmt(space.w_IndexError, "invalid index")
             # check for arrays
             for w_item in view_w:
                 if (isinstance(w_item, W_NDimArray) or
@@ -212,8 +211,7 @@
             idx = space.str_w(w_idx)
             dtype = self.dtype
             if not dtype.is_record() or idx not in dtype.fields:
-                raise OperationError(space.w_ValueError, space.wrap(
-                    "field named %s not found" % idx))
+                raise oefmt(space.w_ValueError, "field named %s not found", idx)
             return RecordChunk(idx)
         elif (space.isinstance_w(w_idx, space.w_int) or
                 space.isinstance_w(w_idx, space.w_slice)):
@@ -455,8 +453,8 @@
                                        self.get_strides(),
                                        self.order)
         if new_strides is None:
-            raise OperationError(space.w_AttributeError, space.wrap(
-                          "incompatible shape for a non-contiguous array"))
+            raise oefmt(space.w_AttributeError,
+                "incompatible shape for a non-contiguous array")
         new_backstrides = [0] * len(new_shape)
         for nd in range(len(new_shape)):
             new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
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
@@ -295,16 +295,16 @@
             try:
                 item = self.names[indx]
             except IndexError:
-                raise OperationError(space.w_IndexError, space.wrap(
-                    "Field index %d out of range." % indx))
+                raise oefmt(space.w_IndexError,
+                    "Field index %d out of range.", indx)
         else:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "Field key must be an integer, string, or unicode."))
+            raise oefmt(space.w_ValueError,
+                "Field key must be an integer, string, or unicode.")
         try:
             return self.fields[item][1]
         except KeyError:
-            raise OperationError(space.w_KeyError, space.wrap(
-                "Field named '%s' not found." % item))
+            raise oefmt(space.w_KeyError,
+                "Field named '%s' not found.", item)
 
     def descr_len(self, space):
         if not self.fields:
@@ -535,6 +535,7 @@
             "cannot create dtype with type '%N'", w_dtype)
     raise oefmt(space.w_TypeError, "data type not understood")
 
+
 W_Dtype.typedef = TypeDef("dtype",
     __module__ = "numpy",
     __new__ = interp2app(descr__new__),
diff --git a/pypy/module/micronumpy/iterators.py b/pypy/module/micronumpy/iterators.py
--- a/pypy/module/micronumpy/iterators.py
+++ b/pypy/module/micronumpy/iterators.py
@@ -118,17 +118,17 @@
         if step == 0:
             return
         self.index += step
-        for i in xrange(len(self.shape_m1) - 1, -1, -1):
+        for i in xrange(self.ndim_m1, -1, -1):
             if self.indices[i] < (self.shape_m1[i] + 1) - step:
                 self.indices[i] += step
                 self.offset += self.strides[i] * step
                 break
             else:
-                remaining_step = (self.indices[i] + step) // (self.shape_m1[i] + 1)
-                this_i_step = step - remaining_step * (self.shape_m1[i] + 1)
-                self.indices[i] = self.indices[i] + this_i_step
-                self.offset += self.strides[i] * this_i_step
-                step = remaining_step
+                rem_step = (self.indices[i] + step) // (self.shape_m1[i] + 1)
+                cur_step = step - rem_step * (self.shape_m1[i] + 1)
+                self.indices[i] += cur_step
+                self.offset += self.strides[i] * cur_step
+                step = rem_step
                 assert step > 0
 
     def done(self):
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
@@ -127,11 +127,11 @@
                 "index out of range for array"))
         size = loop.count_all_true(idx)
         if size > val.get_size() and val.get_size() != 1:
-            raise OperationError(space.w_ValueError, space.wrap(
+            raise oefmt(space.w_ValueError,
                 "NumPy boolean array indexing assignment "
                 "cannot assign %d input values to "
-                "the %d output values where the mask is true" %
-                (val.get_size(), size)))
+                "the %d output values where the mask is true",
+                val.get_size(), size)
         loop.setitem_filter(space, self, idx, val)
 
     def _prepare_array_index(self, space, w_index):
@@ -994,14 +994,14 @@
     def _reduce_argmax_argmin_impl(op_name):
         def impl(self, space, w_axis=None, w_out=None):
             if not space.is_none(w_axis):
-                raise OperationError(space.w_NotImplementedError, space.wrap(
-                    "axis unsupported for %s" % op_name))
+                raise oefmt(space.w_NotImplementedError,
+                    "axis unsupported for %s", op_name)
             if not space.is_none(w_out):
-                raise OperationError(space.w_NotImplementedError, space.wrap(
-                    "out unsupported for %s" % op_name))
+                raise oefmt(space.w_NotImplementedError,
+                    "out unsupported for %s", op_name)
             if self.get_size() == 0:
-                raise OperationError(space.w_ValueError,
-                    space.wrap("Can't call %s on zero-size arrays" % op_name))
+                raise oefmt(space.w_ValueError,
+                    "Can't call %s on zero-size arrays", op_name)
             op = getattr(loop, op_name)
             try:
                 res = op(self)
@@ -1096,15 +1096,16 @@
         elif lens == 4:
             base_index = 0
         else:
-            raise OperationError(space.w_ValueError, space.wrap(
-                 "__setstate__ called with len(args[1])==%d, not 5 or 4" % lens))
+            raise oefmt(space.w_ValueError,
+                "__setstate__ called with len(args[1])==%d, not 5 or 4", lens)
         shape = space.getitem(w_state, space.wrap(base_index))
         dtype = space.getitem(w_state, space.wrap(base_index+1))
         #isfortran = space.getitem(w_state, space.wrap(base_index+2))
         storage = space.getitem(w_state, space.wrap(base_index+3))
         if not isinstance(dtype, descriptor.W_Dtype):
-            raise OperationError(space.w_ValueError, space.wrap(
-                 "__setstate__(self, (shape, dtype, .. called with improper dtype '%r'" % dtype))
+            raise oefmt(space.w_ValueError,
+                "__setstate__(self, (shape, dtype, .. called with "
+                "improper dtype '%R'", dtype)
         self.implementation = W_NDimArray.from_shape_and_storage(space,
                 [space.int_w(i) for i in space.listview(shape)],
                 rffi.str2charp(space.str_w(storage), track_allocation=False),
diff --git a/pypy/module/micronumpy/sort.py b/pypy/module/micronumpy/sort.py
--- a/pypy/module/micronumpy/sort.py
+++ b/pypy/module/micronumpy/sort.py
@@ -146,8 +146,7 @@
             if axis < 0:
                 axis = len(shape) + axis
             if axis < 0 or axis >= len(shape):
-                raise OperationError(space.w_IndexError, space.wrap(
-                                                    "Wrong axis %d" % axis))
+                raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
             arr_iter = AllButAxisIter(arr, axis)
             index_impl = index_arr.implementation
             index_iter = AllButAxisIter(index_impl, axis)
@@ -291,8 +290,7 @@
             if axis < 0:
                 axis = len(shape) + axis
             if axis < 0 or axis >= len(shape):
-                raise OperationError(space.w_IndexError, space.wrap(
-                                                    "Wrong axis %d" % axis))
+                raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
             arr_iter = AllButAxisIter(arr, axis)
             stride_size = arr.strides[axis]
             axis_size = arr.shape[axis]
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -310,16 +310,15 @@
         if (self.int_only and not dtype.is_int() or
                 not self.allow_bool and dtype.is_bool() or
                 not self.allow_complex and dtype.is_complex()):
-            raise OperationError(space.w_TypeError, space.wrap(
-                "ufunc %s not supported for the input type" % self.name))
+            raise oefmt(space.w_TypeError,
+                "ufunc %s not supported for the input type", self.name)
         calc_dtype = find_unaryop_result_dtype(space,
                                   w_obj.get_dtype(),
                                   promote_to_float=self.promote_to_float,
                                   promote_bools=self.promote_bools)
         if out is not None:
             if not isinstance(out, W_NDimArray):
-                raise OperationError(space.w_TypeError, space.wrap(
-                                                'output must be an array'))
+                raise oefmt(space.w_TypeError, 'output must be an array')
             res_dtype = out.get_dtype()
             #if not w_obj.get_dtype().can_cast_to(res_dtype):
             #    raise oefmt(space.w_TypeError,
@@ -424,13 +423,12 @@
                                          w_rdtype.is_bool()) or
                 not self.allow_complex and (w_ldtype.is_complex() or
                                             w_rdtype.is_complex())):
-            raise OperationError(space.w_TypeError, space.wrap(
-                "ufunc '%s' not supported for the input types" % self.name))
+            raise oefmt(space.w_TypeError,
+                "ufunc '%s' not supported for the input types", self.name)
         if space.is_none(w_out):
             out = None
         elif not isinstance(w_out, W_NDimArray):
-            raise OperationError(space.w_TypeError, space.wrap(
-                    'output must be an array'))
+            raise oefmt(space.w_TypeError, 'output must be an array')
         else:
             out = w_out
             calc_dtype = out.get_dtype()
@@ -578,7 +576,8 @@
             return descriptor.get_dtype_cache(space).w_float64dtype
         for bytes, dtype in descriptor.get_dtype_cache(space).float_dtypes_by_num_bytes:
             if (dtype.kind == NPY.FLOATINGLTR and
-                dtype.itemtype.get_element_size() > dt.itemtype.get_element_size()):
+                    dtype.itemtype.get_element_size() >
+                    dt.itemtype.get_element_size()):
                 return dtype
     return dt
 
@@ -763,7 +762,7 @@
         identity = extra_kwargs.get("identity")
         if identity is not None:
             identity = \
-                 descriptor.get_dtype_cache(space).w_longdtype.box(identity)
+                descriptor.get_dtype_cache(space).w_longdtype.box(identity)
         extra_kwargs["identity"] = identity
 
         func = ufunc_dtype_caller(space, ufunc_name, op_name, argcount,


More information about the pypy-commit mailing list