[pypy-commit] pypy numpypy-issue1137: pep8, swallow exceptions in __int__ and __index__ like numpy and better error

timo_jbo noreply at buildbot.pypy.org
Sat May 5 15:31:42 CEST 2012


Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpypy-issue1137
Changeset: r54896:7ab172b5fbf2
Date: 2012-05-05 15:30 +0200
http://bitbucket.org/pypy/pypy/changeset/7ab172b5fbf2/

Log:	pep8, swallow exceptions in __int__ and __index__ like numpy and
	better error when the index doesn't supply a working __len__,
	either.

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
@@ -354,19 +354,27 @@
             try:
                 value = space.int_w(space.index(w_idx))
                 return True
-            except: pass
+            except OperationError:
+                pass
 
             try:
                 value = space.int_w(w_idx)
                 return True
-            except: pass
+            except OperationError:
+                pass
 
             if space.isinstance_w(w_idx, space.w_slice):
                 return False
         elif (space.isinstance_w(w_idx, space.w_slice) or
               space.isinstance_w(w_idx, space.w_int)):
             return False
-        lgt = space.len_w(w_idx)
+
+        try:
+            lgt = space.len_w(w_idx)
+        except OperationError:
+            raise OperationError(space.w_IndexError,
+                                 space.wrap("index must be either an int or a sequence."))
+
         if lgt > shape_len:
             raise OperationError(space.w_IndexError,
                                  space.wrap("invalid index"))
@@ -1045,13 +1053,15 @@
         try:
             idx = space.int_w(space.index(w_idx))
             is_valid = True
-        except: pass
+        except OperationError:
+            pass
 
         if not is_valid:
             try:
                 idx = space.int_w(w_idx)
                 is_valid = True
-            except: pass
+            except OperationError:
+                pass
 
         if is_valid:
             if idx < 0:
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
@@ -424,6 +424,23 @@
 
         assert a[1] == 100
 
+    def test_access_swallow_exception(self):
+        class ErrorIndex(object):
+            def __index__(self):
+                return 1 / 0
+
+        class ErrorInt(object):
+            def __int__(self):
+                return 1 / 0
+
+        # numpy will swallow errors in __int__ and __index__ and
+        # just raise IndexError.
+
+        from _numpypy import arange
+        a = arange(10)
+        raises(IndexError, "a[ErrorIndex()] == 0")
+        raises(IndexError, "a[ErrorInt()] == 0")
+
     def test_setslice_array(self):
         from _numpypy import array
         a = array(range(5))


More information about the pypy-commit mailing list