diff options
author | Tim Burgess <timburgess@mac.com> | 2013-05-29 10:40:40 +1000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2014-03-28 13:24:45 -0600 |
commit | 06aafa56b25ba15063b3cbc701eb67b352d1e14f (patch) | |
tree | 2829dc6517530870ec229d089c13e97a338f9249 /numpy | |
parent | 46767a2ffc6bf7b3c6841bd9b10f1f26543d22b7 (diff) | |
download | numpy-06aafa56b25ba15063b3cbc701eb67b352d1e14f.tar.gz |
BUG: Fix for issue #3213
Two changes to numpy.ma.core._check_fill_value:
1. When the fill value is a string and the array type is not one of
'OSUV', raise TypeError instead of using the default fill value.
2. When the fill value overflows the type, raise TypeError instead
of OverflowError.
Closes #3213.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 13 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 9 |
2 files changed, 13 insertions, 9 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 9c3c6a615..faf9896a5 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -417,13 +417,18 @@ def _check_fill_value(fill_value, ndtype): dtype=ndtype) else: if isinstance(fill_value, basestring) and (ndtype.char not in 'OSVU'): - fill_value = default_fill_value(ndtype) + err_msg = "Cannot set fill value of string with array of dtype %s" + raise TypeError(err_msg % ndtype) else: - # In case we want to convert 1e+20 to int... + # In case we want to convert 1e20 to int... try: - fill_value = np.array(fill_value, copy=False, dtype=ndtype)#.item() + fill_value = np.array(fill_value, copy=False, dtype=ndtype) except OverflowError: - fill_value = default_fill_value(ndtype) + # Raise TypeError instead of OverflowError. OverflowError + # is seldom used, and the real problem here is that the + # passed fill_value is not compatible with the ndtype. + err_msg = "Fill value %s overflows dtype %s" + raise TypeError(err_msg % (fill_value, ndtype)) return np.array(fill_value) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b9b84d7b2..6bb8d5b2e 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1312,9 +1312,9 @@ class TestMaskedArrayAttributes(TestCase): #------------------------------------------------------------------------------ class TestFillingValues(TestCase): - # + def test_check_on_scalar(self): - # Test _check_fill_value + # Test _check_fill_value set to valid and invalid values _check_fill_value = np.ma.core._check_fill_value # fval = _check_fill_value(0, int) @@ -1326,9 +1326,8 @@ class TestFillingValues(TestCase): assert_equal(fval, asbytes("0")) fval = _check_fill_value(None, "|S3") assert_equal(fval, default_fill_value("|S3")) - # - fval = _check_fill_value(1e+20, int) - assert_equal(fval, default_fill_value(0)) + self.assertRaises(TypeError, _check_fill_value, 1e+20, int) + self.assertRaises(TypeError, _check_fill_value, 'stuff', int) def test_check_on_fields(self): # Tests _check_fill_value with records |