summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeno Goertz <keno@goertz-berlin.com>2017-10-05 01:17:14 +0200
committerKeno Goertz <keno@goertz-berlin.com>2017-10-05 01:18:42 +0200
commitaa7b339d6172775db71834677d68ec89da1aaf4e (patch)
tree43828f7f72bc81047592be6e526d60394d209d46
parent1f4ed32fdf0cf2f197d9a8da5698580ed6ec7683 (diff)
downloadnumpy-aa7b339d6172775db71834677d68ec89da1aaf4e.tar.gz
BUG: Added exception for casting numpy.ma.masked to long
TST: Added test_coercion_long and removed knownfailure from test_coercion_int
-rw-r--r--numpy/ma/core.py12
-rw-r--r--numpy/ma/tests/test_core.py6
2 files changed, 17 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index b6e2edf5a..e4118e5dc 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -4230,6 +4230,18 @@ class MaskedArray(ndarray):
elif self._mask:
raise MaskError('Cannot convert masked element to a Python int.')
return int(self.item())
+
+ def __long__(self):
+ """
+ Convert to long.
+ """
+ if self.size > 1:
+ raise TypeError("Only length-1 arrays can be conveted "
+ "to Python scalars")
+ elif self._mask:
+ raise MaskError('Cannot convert masked element to a Python long.')
+ return long(self.item())
+
def get_imag(self):
"""
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index fe0271580..e36c78a77 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -4858,10 +4858,14 @@ class TestMaskedConstant(object):
assert_raises(ValueError, operator.setitem, view.data, (), 1)
assert_raises(ValueError, operator.setitem, view.mask, (), False)
- @dec.knownfailureif(sys.version_info.major == 2, "See gh-9751")
def test_coercion_int(self):
a_i = np.zeros((), int)
assert_raises(MaskError, operator.setitem, a_i, (), np.ma.masked)
+ assert_raises(MaskError, int, np.ma.masked)
+
+ @dec.skipif(sys.version_info.major == 3, "long doesn't exist in Python 3")
+ def test_coercion_long(self):
+ assert_raises(MaskError, long, np.ma.masked)
def test_coercion_float(self):
a_f = np.zeros((), float)