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

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 20:52:56 CST 2010


Author: ptvirtan
Date: 2010-02-20 20:52:56 -0600 (Sat, 20 Feb 2010)
New Revision: 8222

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
BUG: ma: _check_fill_value shouldn't rely on implicit array() string casting

Since array([12345678.9, 'a']) == array(['12345678', 'a'], dtype='|S8')
ie., automatic string conversion uses only the size of the minimal data
type, not the size of the string representation, code should not rely on
array() casting items implicitly to object arrays.

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2010-02-21 02:52:39 UTC (rev 8221)
+++ trunk/numpy/ma/core.py	2010-02-21 02:52:56 UTC (rev 8222)
@@ -405,6 +405,7 @@
                 raise ValueError(err_msg % (fill_value, fdtype))
         else:
             descr = ndtype.descr
+            fill_value = np.asarray(fill_value, dtype=object)
             fill_value = np.array(_recursive_set_fill_value(fill_value, descr),
                                   dtype=ndtype)
     else:

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2010-02-21 02:52:39 UTC (rev 8221)
+++ trunk/numpy/ma/tests/test_core.py	2010-02-21 02:52:56 UTC (rev 8222)
@@ -1307,41 +1307,41 @@
         _check_fill_value = np.ma.core._check_fill_value
         ndtype = [('a', int), ('b', float), ('c', "|S3")]
         # A check on a list should return a single record
-        fval = _check_fill_value([-999, -999.9, "???"], ndtype)
+        fval = _check_fill_value([-999, -12345678.9, "???"], ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), [-999, -999.9, "???"])
+        assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
         # A check on None should output the defaults
         fval = _check_fill_value(None, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
         assert_equal(fval.item(), [default_fill_value(0),
                                    default_fill_value(0.),
-                                   default_fill_value("0")])
+                                   asbytes(default_fill_value("0"))])
         #.....Using a structured type as fill_value should work
-        fill_val = np.array((-999, -999.9, "???"), dtype=ndtype)
+        fill_val = np.array((-999, -12345678.9, "???"), dtype=ndtype)
         fval = _check_fill_value(fill_val, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), [-999, -999.9, "???"])
+        assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
         #.....Using a flexible type w/ a different type shouldn't matter
-        fill_val = np.array((-999, -999.9, "???"),
+        fill_val = np.array((-999, -12345678.9, "???"),
                             dtype=[("A", int), ("B", float), ("C", "|S3")])
         fval = _check_fill_value(fill_val, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), [-999, -999.9, "???"])
+        assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
         #.....Using an object-array shouldn't matter either
-        fill_value = np.array((-999, -999.9, "???"), dtype=object)
+        fill_value = np.array((-999, -12345678.9, "???"), dtype=object)
         fval = _check_fill_value(fill_val, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), [-999, -999.9, "???"])
+        assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
         #
-        fill_value = np.array((-999, -999.9, "???"))
+        fill_value = np.array((-999, -12345678.9, "???"))
         fval = _check_fill_value(fill_val, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), [-999, -999.9, "???"])
+        assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
         #.....One-field-only flexible type should work as well
         ndtype = [("a", int)]
-        fval = _check_fill_value(-999, ndtype)
+        fval = _check_fill_value(-999999999, ndtype)
         self.assertTrue(isinstance(fval, ndarray))
-        assert_equal(fval.item(), (-999,))
+        assert_equal(fval.item(), (-999999999,))
 
 
     def test_fillvalue_conversion(self):



More information about the Numpy-svn mailing list