diff options
author | gfyoung <gfyoung@mit.edu> | 2015-12-11 04:24:16 +0000 |
---|---|---|
committer | gfyoung <gfyoung17@gmail.com> | 2015-12-18 03:02:21 -0800 |
commit | 088e20e272389395fb3fd24fed144ed19bae8cdb (patch) | |
tree | 918a324b3ffb9b1abff63ee7c35d668783667cbc /numpy/core | |
parent | f7b07521ca811baa2fcc649a6dc5cf56f5c65fd0 (diff) | |
download | numpy-088e20e272389395fb3fd24fed144ed19bae8cdb.tar.gz |
DEP: Stricter arg checking for array ordering
The bug traces to the PyArray_OrderConverter
method in conversion_utils.c, where no errors
are thrown if the ORDER parameter passed in
is not of the string data-type or has a string
value of length greater than one. This commit
causes a DeprecationWarning to be raised, which
will later be turned into a TypeError or another
type of error in a future release.
Closes gh-6598.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/conversion_utils.c | 21 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 30 |
2 files changed, 51 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 88064c1d6..d7a617875 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -540,6 +540,15 @@ PyArray_OrderConverter(PyObject *object, NPY_ORDER *val) return ret; } else if (!PyBytes_Check(object) || PyBytes_GET_SIZE(object) < 1) { + /* 2015-12-14, 1.11 */ + int ret = DEPRECATE("Non-string object detected for " + "the array ordering. Please pass " + "in 'C', 'F', 'A', or 'K' instead"); + + if (ret < 0) { + return -1; + } + if (PyObject_IsTrue(object)) { *val = NPY_FORTRANORDER; } @@ -553,6 +562,18 @@ PyArray_OrderConverter(PyObject *object, NPY_ORDER *val) } else { str = PyBytes_AS_STRING(object); + if (strlen(str) != 1) { + /* 2015-12-14, 1.11 */ + int ret = DEPRECATE("Non length-one string passed " + "in for the array ordering. " + "Please pass in 'C', 'F', 'A', " + "or 'K' instead"); + + if (ret < 0) { + return -1; + } + } + if (str[0] == 'C' || str[0] == 'c') { *val = NPY_CORDER; } diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index f6dc3d842..65ddc1e77 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -400,6 +400,36 @@ class TestNonCContiguousViewDeprecation(_DeprecationTestCase): self.assert_deprecated(np.ones((2,2)).T.view, args=(np.int8,)) +class TestInvalidOrderParameterInputForFlattenArrayDeprecation(_DeprecationTestCase): + """Invalid arguments to the ORDER parameter in array.flatten() should not be + allowed and should raise an error. However, in the interests of not breaking + code that may inadvertently pass invalid arguments to this parameter, a + DeprecationWarning will be issued instead for the time being to give developers + time to refactor relevant code. + """ + + def test_flatten_array_non_string_arg(self): + x = np.zeros((3, 5)) + self.message = ("Non-string object detected for " + "the array ordering. Please pass " + "in 'C', 'F', 'A', or 'K' instead") + self.assert_deprecated(x.flatten, args=(np.pi,)) + + def test_flatten_array_invalid_string_arg(self): + # Tests that a DeprecationWarning is raised + # when a string of length greater than one + # starting with "C", "F", "A", or "K" (case- + # and unicode-insensitive) is passed in for + # the ORDER parameter. Otherwise, a TypeError + # will be raised! + + x = np.zeros((3, 5)) + self.message = ("Non length-one string passed " + "in for the array ordering. Please " + "pass in 'C', 'F', 'A', or 'K' instead") + self.assert_deprecated(x.flatten, args=("FACK",)) + + class TestTestDeprecated(object): def test_assert_deprecated(self): test_case_instance = _DeprecationTestCase() |