[pypy-commit] pypy numpy-refactor: clean up reduce axis arg errors

bdkearns noreply at buildbot.pypy.org
Thu Feb 27 07:11:24 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: numpy-refactor
Changeset: r69494:6a347d1b5fb7
Date: 2014-02-27 00:45 -0500
http://bitbucket.org/pypy/pypy/changeset/6a347d1b5fb7/

Log:	clean up reduce axis arg errors

diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -3,7 +3,6 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.module.micronumpy import descriptor, loop
 from rpython.rlib.rstring import strip_spaces
-from rpython.rlib.rarithmetic import maxint
 from pypy.module.micronumpy.base import W_NDimArray
 
 FLOAT_SIZE = rffi.sizeof(lltype.Float)
@@ -85,16 +84,3 @@
         return _fromstring_bin(space, s, count, length, dtype)
     else:
         return _fromstring_text(space, s, count, sep, length, dtype)
-
-def unwrap_axis_arg(space, shapelen, w_axis):
-    if space.is_none(w_axis):
-        axis = maxint
-    else:
-        axis = space.int_w(w_axis)
-        if axis < -shapelen or axis >= shapelen:
-            raise oefmt(space.w_ValueError,
-                        "axis entry %d is out of bounds [%d, %d)",
-                        axis, -shapelen, shapelen)
-        if axis < 0:
-            axis += shapelen
-    return axis
diff --git a/pypy/module/micronumpy/test/test_ufuncs.py b/pypy/module/micronumpy/test/test_ufuncs.py
--- a/pypy/module/micronumpy/test/test_ufuncs.py
+++ b/pypy/module/micronumpy/test/test_ufuncs.py
@@ -763,8 +763,17 @@
         assert add.reduce(1) == 1
 
         assert list(maximum.reduce(zeros((2, 0)), axis=0)) == []
-        raises(ValueError, maximum.reduce, zeros((2, 0)), axis=None)
-        raises(ValueError, maximum.reduce, zeros((2, 0)), axis=1)
+        exc = raises(ValueError, maximum.reduce, zeros((2, 0)), axis=None)
+        assert exc.value[0] == ('zero-size array to reduction operation '
+                                'maximum which has no identity')
+        exc = raises(ValueError, maximum.reduce, zeros((2, 0)), axis=1)
+        assert exc.value[0] == ('zero-size array to reduction operation '
+                                'maximum which has no identity')
+
+        a = zeros((2, 2)) + 1
+        assert (add.reduce(a, axis=1) == [2, 2]).all()
+        exc = raises(ValueError, add.reduce, a, axis=2)
+        assert exc.value[0] == "'axis' entry is out of bounds"
 
     def test_reduce_1d(self):
         from numpypy import array, add, maximum, less, float16, complex64
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
@@ -4,9 +4,8 @@
 from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
 from pypy.module.micronumpy import boxes, descriptor, loop
 from rpython.rlib import jit
-from rpython.rlib.rarithmetic import LONG_BIT
+from rpython.rlib.rarithmetic import LONG_BIT, maxint
 from rpython.tool.sourcetools import func_with_new_name
-from pypy.module.micronumpy.interp_support import unwrap_axis_arg
 from pypy.module.micronumpy.strides import shape_agreement
 from pypy.module.micronumpy.base import convert_to_array, W_NDimArray
 from pypy.module.micronumpy import constants as NPY
@@ -175,7 +174,14 @@
         if obj.is_scalar():
             return obj.get_scalar_value()
         shapelen = len(obj_shape)
-        axis = unwrap_axis_arg(space, shapelen, w_axis)
+        if space.is_none(w_axis):
+            axis = maxint
+        else:
+            axis = space.int_w(w_axis)
+            if axis < -shapelen or axis >= shapelen:
+                raise oefmt(space.w_ValueError, "'axis' entry is out of bounds")
+            if axis < 0:
+                axis += shapelen
         assert axis >= 0
         dtype = descriptor.decode_w_dtype(space, dtype)
         if dtype is None:
@@ -192,8 +198,9 @@
             for i in range(shapelen):
                 if space.is_none(w_axis) or i == axis:
                     if obj_shape[i] == 0:
-                        raise oefmt(space.w_ValueError, "zero-size array to "
-                                    "%s.reduce without identity", self.name)
+                        raise oefmt(space.w_ValueError,
+                            "zero-size array to reduction operation %s "
+                            "which has no identity", self.name)
         if shapelen > 1 and axis < shapelen:
             temp = None
             if cumulative:


More information about the pypy-commit mailing list