Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b0d075

Browse files
committed
Merge pull request numpy#4565 from charris/fill_value_fix
Raise TypeError when setting inappropriate ma fill_value
2 parents 9fc98ae + 92a0a2c commit 7b0d075

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

doc/release/1.9.0-notes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ ndarray.tofile exception type
6767
All ``tofile`` exceptions are now ``IOError``, some were previously
6868
``ValueError``.
6969

70+
Invalid fill value exceptions
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
Two changes to numpy.ma.core._check_fill_value:
73+
74+
* When the fill value is a string and the array type is not one of
75+
'OSUV', TypeError is raised instead of the default fill value being used.
76+
77+
* When the fill value overflows the array type, TypeError is raised instead
78+
of OverflowError.
79+
80+
7081
The ``doc/swig`` directory moved
7182
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7283
The ``doc/swig`` directory has been moved to ``tools/swig``.

numpy/ma/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,18 @@ def _check_fill_value(fill_value, ndtype):
417417
dtype=ndtype)
418418
else:
419419
if isinstance(fill_value, basestring) and (ndtype.char not in 'OSVU'):
420-
fill_value = default_fill_value(ndtype)
420+
err_msg = "Cannot set fill value of string with array of dtype %s"
421+
raise TypeError(err_msg % ndtype)
421422
else:
422-
# In case we want to convert 1e+20 to int...
423+
# In case we want to convert 1e20 to int...
423424
try:
424-
fill_value = np.array(fill_value, copy=False, dtype=ndtype)#.item()
425+
fill_value = np.array(fill_value, copy=False, dtype=ndtype)
425426
except OverflowError:
426-
fill_value = default_fill_value(ndtype)
427+
# Raise TypeError instead of OverflowError. OverflowError
428+
# is seldom used, and the real problem here is that the
429+
# passed fill_value is not compatible with the ndtype.
430+
err_msg = "Fill value %s overflows dtype %s"
431+
raise TypeError(err_msg % (fill_value, ndtype))
427432
return np.array(fill_value)
428433

429434

numpy/ma/tests/test_core.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,9 @@ def test_flat(self):
13121312

13131313
#------------------------------------------------------------------------------
13141314
class TestFillingValues(TestCase):
1315-
#
1315+
13161316
def test_check_on_scalar(self):
1317-
# Test _check_fill_value
1317+
# Test _check_fill_value set to valid and invalid values
13181318
_check_fill_value = np.ma.core._check_fill_value
13191319
#
13201320
fval = _check_fill_value(0, int)
@@ -1326,9 +1326,8 @@ def test_check_on_scalar(self):
13261326
assert_equal(fval, asbytes("0"))
13271327
fval = _check_fill_value(None, "|S3")
13281328
assert_equal(fval, default_fill_value("|S3"))
1329-
#
1330-
fval = _check_fill_value(1e+20, int)
1331-
assert_equal(fval, default_fill_value(0))
1329+
self.assertRaises(TypeError, _check_fill_value, 1e+20, int)
1330+
self.assertRaises(TypeError, _check_fill_value, 'stuff', int)
13321331

13331332
def test_check_on_fields(self):
13341333
# Tests _check_fill_value with records

0 commit comments

Comments
 (0)