[Numpy-svn] r5071 - in trunk/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Wed Apr 23 09:46:36 CDT 2008


Author: pierregm
Date: 2008-04-23 09:46:29 -0500 (Wed, 23 Apr 2008)
New Revision: 5071

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
   trunk/numpy/ma/tests/test_old_ma.py
Log:
__float__ : raises a TypeError exception for arrays longer than 1
__int__   : raises a TypeError exception for arrays longer than 1

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-04-23 09:58:58 UTC (rev 5070)
+++ trunk/numpy/ma/core.py	2008-04-23 14:46:29 UTC (rev 5071)
@@ -1740,14 +1740,20 @@
     #............................................
     def __float__(self):
         "Convert to float."
-        if self._mask is not nomask:
+        if self.size > 1:
+            raise TypeError,\
+                   "Only length-1 arrays can be converted to Python scalars"
+        elif self._mask:
             warnings.warn("Warning: converting a masked element to nan.")
             return numpy.nan
         return float(self.item())
 
     def __int__(self):
         "Convert to int."
-        if self._mask is not nomask:
+        if self.size > 1:
+            raise TypeError,\
+                   "Only length-1 arrays can be converted to Python scalars"
+        elif self._mask:
             raise MAError, 'Cannot convert masked element to a Python int.'
         return int(self.item())
     #............................................
@@ -3366,3 +3372,4 @@
 indices = numpy.indices
 
 ###############################################################################
+        
\ No newline at end of file

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-04-23 09:58:58 UTC (rev 5070)
+++ trunk/numpy/ma/tests/test_core.py	2008-04-23 14:46:29 UTC (rev 5071)
@@ -702,16 +702,19 @@
         assert_equal(1.0, float(array(1)))
         assert_equal(1, int(array([[[1]]])))
         assert_equal(1.0, float(array([[1]])))
-        self.failUnlessRaises(ValueError, float, array([1,1]))
+        self.assertRaises(TypeError, float, array([1,1]))
 
         warnings.simplefilter('ignore',UserWarning)
         assert numpy.isnan(float(array([1],mask=[1])))
         warnings.simplefilter('default',UserWarning)
-#TODO: Check how bool works...
-#TODO:        self.failUnless(bool(array([0,1])))
-#TODO:        self.failUnless(bool(array([0,0],mask=[0,1])))
-#TODO:        self.failIf(bool(array([0,0])))
-#TODO:        self.failIf(bool(array([0,0],mask=[0,0])))
+        #
+        a = array([1,2,3],mask=[1,0,0])
+        self.assertRaises(TypeError, lambda:float(a))
+        assert_equal(float(a[-1]), 3.)
+        assert(numpy.isnan(float(a[0])))
+        self.assertRaises(TypeError, int, a)
+        assert_equal(int(a[-1]), 3)
+        self.assertRaises(MAError, lambda:int(a[0]))
     #........................
     def test_arraymethods(self):
         "Tests some MaskedArray methods."

Modified: trunk/numpy/ma/tests/test_old_ma.py
===================================================================
--- trunk/numpy/ma/tests/test_old_ma.py	2008-04-23 09:58:58 UTC (rev 5070)
+++ trunk/numpy/ma/tests/test_old_ma.py	2008-04-23 14:46:29 UTC (rev 5071)
@@ -577,7 +577,7 @@
         self.assertEqual(1.0, float(array(1)))
         self.assertEqual(1, int(array([[[1]]])))
         self.assertEqual(1.0, float(array([[1]])))
-        self.failUnlessRaises(ValueError, float, array([1,1]))
+        self.failUnlessRaises(TypeError, float, array([1,1]))
         self.failUnlessRaises(ValueError, bool, array([0,1]))
         self.failUnlessRaises(ValueError, bool, array([0,0],mask=[0,1]))
 



More information about the Numpy-svn mailing list