[Numpy-svn] r8247 - in trunk/numpy: core/tests ma/tests

numpy-svn@scip... numpy-svn@scip...
Sun Feb 21 12:32:03 CST 2010


Author: charris
Date: 2010-02-21 12:32:03 -0600 (Sun, 21 Feb 2010)
New Revision: 8247

Modified:
   trunk/numpy/core/tests/test_numeric.py
   trunk/numpy/core/tests/test_umath_complex.py
   trunk/numpy/ma/tests/test_old_ma.py
Log:
BUG: Fix possibly unbalanced seterr calls. Fixes change of divide state after
running np.test().

Modified: trunk/numpy/core/tests/test_numeric.py
===================================================================
--- trunk/numpy/core/tests/test_numeric.py	2010-02-21 18:31:55 UTC (rev 8246)
+++ trunk/numpy/core/tests/test_numeric.py	2010-02-21 18:32:03 UTC (rev 8247)
@@ -223,26 +223,32 @@
 class TestSeterr(TestCase):
     def test_set(self):
         err = seterr()
-        old = seterr(divide='warn')
-        self.assertTrue(err == old)
-        new = seterr()
-        self.assertTrue(new['divide'] == 'warn')
-        seterr(over='raise')
-        self.assertTrue(geterr()['over'] == 'raise')
-        self.assertTrue(new['divide'] == 'warn')
-        seterr(**old)
-        self.assertTrue(geterr() == old)
+        try:
+            old = seterr(divide='warn')
+            self.assertTrue(err == old)
+            new = seterr()
+            self.assertTrue(new['divide'] == 'warn')
+            seterr(over='raise')
+            self.assertTrue(geterr()['over'] == 'raise')
+            self.assertTrue(new['divide'] == 'warn')
+            seterr(**old)
+            self.assertTrue(geterr() == old)
+        finally:
+            seterr(**err)
 
     def test_divide_err(self):
-        seterr(divide='raise')
+        err = seterr(divide='raise')
         try:
+            try:
+                array([1.]) / array([0.])
+            except FloatingPointError:
+                pass
+            else:
+                self.fail()
+            seterr(divide='ignore')
             array([1.]) / array([0.])
-        except FloatingPointError:
-            pass
-        else:
-            self.fail()
-        seterr(divide='ignore')
-        array([1.]) / array([0.])
+        finally:
+            seterr(**err)
 
 
 class TestFromiter(TestCase):

Modified: trunk/numpy/core/tests/test_umath_complex.py
===================================================================
--- trunk/numpy/core/tests/test_umath_complex.py	2010-02-21 18:31:55 UTC (rev 8246)
+++ trunk/numpy/core/tests/test_umath_complex.py	2010-02-21 18:32:03 UTC (rev 8247)
@@ -465,12 +465,16 @@
         assert_almost_equal(f(z1), x)
 
 def check_complex_value(f, x1, y1, x2, y2, exact=True):
+    err = np.seterr(invalid='ignore')
     z1 = np.array([complex(x1, y1)])
     z2 = np.complex(x2, y2)
-    if exact:
-        assert_equal(f(z1), z2)
-    else:
-        assert_almost_equal(f(z1), z2)
+    try:
+        if exact:
+            assert_equal(f(z1), z2)
+        else:
+            assert_almost_equal(f(z1), z2)
+    finally:
+        np.seterr(**err)
 
 if __name__ == "__main__":
     run_module_suite()

Modified: trunk/numpy/ma/tests/test_old_ma.py
===================================================================
--- trunk/numpy/ma/tests/test_old_ma.py	2010-02-21 18:31:55 UTC (rev 8246)
+++ trunk/numpy/ma/tests/test_old_ma.py	2010-02-21 18:32:03 UTC (rev 8247)
@@ -89,14 +89,18 @@
             self.assertTrue(eq(x - y, xm - ym))
             self.assertTrue(eq(x * y, xm * ym))
             olderr = numpy.seterr(divide='ignore', invalid='ignore')
-            self.assertTrue(eq(x / y, xm / ym))
-            numpy.seterr(**olderr)
+            try:
+                self.assertTrue(eq(x / y, xm / ym))
+            finally:
+                numpy.seterr(**olderr)
             self.assertTrue(eq(a10 + y, a10 + ym))
             self.assertTrue(eq(a10 - y, a10 - ym))
             self.assertTrue(eq(a10 * y, a10 * ym))
             olderr = numpy.seterr(divide='ignore', invalid='ignore')
-            self.assertTrue(eq(a10 / y, a10 / ym))
-            numpy.seterr(**olderr)
+            try:
+                self.assertTrue(eq(a10 / y, a10 / ym))
+            finally:
+                numpy.seterr(**olderr)
             self.assertTrue(eq(x + a10, xm + a10))
             self.assertTrue(eq(x - a10, xm - a10))
             self.assertTrue(eq(x * a10, xm * a10))
@@ -108,8 +112,10 @@
             self.assertTrue(eq(numpy.subtract(x,y), subtract(xm, ym)))
             self.assertTrue(eq(numpy.multiply(x,y), multiply(xm, ym)))
             olderr = numpy.seterr(divide='ignore', invalid='ignore')
-            self.assertTrue(eq(numpy.divide(x,y), divide(xm, ym)))
-            numpy.seterr(**olderr)
+            try:
+                self.assertTrue(eq(numpy.divide(x,y), divide(xm, ym)))
+            finally:
+                numpy.seterr(**olderr)
 
 
     def test_testMixedArithmetic(self):
@@ -128,10 +134,12 @@
         self.assertTrue (eq(numpy.tan(x), tan(xm)))
         self.assertTrue (eq(numpy.tanh(x), tanh(xm)))
         olderr = numpy.seterr(divide='ignore', invalid='ignore')
-        self.assertTrue (eq(numpy.sqrt(abs(x)), sqrt(xm)))
-        self.assertTrue (eq(numpy.log(abs(x)), log(xm)))
-        self.assertTrue (eq(numpy.log10(abs(x)), log10(xm)))
-        numpy.seterr(**olderr)
+        try:
+            self.assertTrue (eq(numpy.sqrt(abs(x)), sqrt(xm)))
+            self.assertTrue (eq(numpy.log(abs(x)), log(xm)))
+            self.assertTrue (eq(numpy.log10(abs(x)), log10(xm)))
+        finally:
+            numpy.seterr(**olderr)
         self.assertTrue (eq(numpy.exp(x), exp(xm)))
         self.assertTrue (eq(numpy.arcsin(z), arcsin(zm)))
         self.assertTrue (eq(numpy.arccos(z), arccos(zm)))
@@ -658,25 +666,28 @@
 
 
     def test_testUfuncRegression(self):
+        f_invalid_ignore = ['sqrt', 'arctanh', 'arcsin', 'arccos',
+                'arccosh', 'arctanh', 'log', 'log10','divide',
+                'true_divide', 'floor_divide', 'remainder', 'fmod']
         for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
-                  'sin', 'cos', 'tan',
-                  'arcsin', 'arccos', 'arctan',
-                  'sinh', 'cosh', 'tanh',
-                  'arcsinh',
-                  'arccosh',
-                  'arctanh',
-                  'absolute', 'fabs', 'negative',
-                  # 'nonzero', 'around',
-                  'floor', 'ceil',
-                  # 'sometrue', 'alltrue',
-                  'logical_not',
-                  'add', 'subtract', 'multiply',
-                  'divide', 'true_divide', 'floor_divide',
-                  'remainder', 'fmod', 'hypot', 'arctan2',
-                  'equal', 'not_equal', 'less_equal', 'greater_equal',
-                  'less', 'greater',
-                  'logical_and', 'logical_or', 'logical_xor',
-                  ]:
+                'sin', 'cos', 'tan',
+                'arcsin', 'arccos', 'arctan',
+                'sinh', 'cosh', 'tanh',
+                'arcsinh',
+                'arccosh',
+                'arctanh',
+                'absolute', 'fabs', 'negative',
+                # 'nonzero', 'around',
+                'floor', 'ceil',
+                # 'sometrue', 'alltrue',
+                'logical_not',
+                'add', 'subtract', 'multiply',
+                'divide', 'true_divide', 'floor_divide',
+                'remainder', 'fmod', 'hypot', 'arctan2',
+                'equal', 'not_equal', 'less_equal', 'greater_equal',
+                'less', 'greater',
+                'logical_and', 'logical_or', 'logical_xor',
+                ]:
             try:
                 uf = getattr(umath, f)
             except AttributeError:
@@ -684,17 +695,15 @@
             mf = getattr(numpy.ma, f)
             args = self.d[:uf.nin]
             olderr = numpy.geterr()
-            f_invalid_ignore = ['sqrt', 'arctanh', 'arcsin', 'arccos',
-                                'arccosh', 'arctanh', 'log', 'log10','divide',
-                                'true_divide', 'floor_divide', 'remainder',
-                                'fmod']
-            if f in f_invalid_ignore:
-                numpy.seterr(invalid='ignore')
-            if f in ['arctanh', 'log', 'log10']:
-                numpy.seterr(divide='ignore')
-            ur = uf(*args)
-            mr = mf(*args)
-            numpy.seterr(**olderr)
+            try:
+                if f in f_invalid_ignore:
+                    numpy.seterr(invalid='ignore')
+                if f in ['arctanh', 'log', 'log10']:
+                    numpy.seterr(divide='ignore')
+                ur = uf(*args)
+                mr = mf(*args)
+            finally:
+                numpy.seterr(**olderr)
             self.assertTrue(eq(ur.filled(0), mr.filled(0), f))
             self.assertTrue(eqmask(ur.mask, mr.mask))
 



More information about the Numpy-svn mailing list