diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2013-06-09 16:57:01 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-06-09 17:10:13 +0200 |
commit | 163f6df5a6668d06cb7abfe38dbd03d19b26d6f3 (patch) | |
tree | 6bda663c589b10ff4bde8e0a4b7bf0263481ea2f /numpy | |
parent | e67269a5c14954e03d8afaa30a5fe4299f2fd062 (diff) | |
download | numpy-163f6df5a6668d06cb7abfe38dbd03d19b26d6f3.tar.gz |
MAINT: Disallow np.bool_ being used as integer
This makes the deprecation of bools used as integers complete
from the numpy perspective. It would not be unsensible to move
the bool checks into indexing specific code though.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 11 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 4 |
2 files changed, 14 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index a177e49dd..79093467f 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -698,7 +698,7 @@ PyArray_PyIntAsIntp(PyObject *o) return -1; } - /* Be a bit stricter and not allow bools */ + /* Be a bit stricter and not allow bools, np.bool_ is handled later */ if (PyBool_Check(o)) { if (DEPRECATE("using a boolean instead of an integer" " will result in an error in the future") < 0) { @@ -730,6 +730,15 @@ PyArray_PyIntAsIntp(PyObject *o) #endif return (npy_intp)long_value; } + + /* Disallow numpy.bool_. Boolean arrays do not currently support index. */ + if (PyArray_IsScalar(o, Bool)) { + if (DEPRECATE("using a boolean instead of an integer" + " will result in an error in the future") < 0) { + return -1; + } + } + /* * The most general case. PyNumber_Index(o) covers everything * including arrays. In principle it may be possible to replace diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 2b9fcdb69..2bedbca09 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -255,6 +255,10 @@ class TestBooleanArgumentDeprecation(_DeprecationTestCase): a = np.array([[[1]]]) self.assert_deprecated(np.reshape, args=(a, (True, -1))) + self.assert_deprecated(np.reshape, args=(a, (np.bool_(True), -1))) + # Note that operator.index(np.array(True)) does not work, a boolean + # array is thus also deprecated, but not with the same message: + assert_raises(TypeError, operator.index, np.array(True)) self.assert_deprecated(np.take, args=(a, [0], False)) self.assert_deprecated(lambda: a[False:True:True], exceptions=IndexError, num=3) self.assert_deprecated(lambda: a[False,0], exceptions=IndexError) |