summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2021-03-18 20:32:20 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2021-03-18 20:32:20 -0500
commitc9e7ff76780480ed8fdba30df5dde07ac6736323 (patch)
tree052fe4855f4ead76369d351342a630ce46251199
parent6b840a49345fc3c90e1c49e10b1ddc946abbf2b8 (diff)
downloadnumpy-c9e7ff76780480ed8fdba30df5dde07ac6736323.tar.gz
BUG,TST: Fix error path and argument name (and add test coverage)
The argument name for the python side defined function was always `a` and not `object` so fix it and add a test. Also add a test to ensure the error paths are taken for all of the function. The missing NULL returns cannot be tested easily (but thanks to code coverage its nice to look at the uncovered stuff closer...)
-rw-r--r--numpy/core/src/multiarray/arrayfunction_override.c2
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c8
-rw-r--r--numpy/core/tests/test_multiarray.py27
3 files changed, 33 insertions, 4 deletions
diff --git a/numpy/core/src/multiarray/arrayfunction_override.c b/numpy/core/src/multiarray/arrayfunction_override.c
index 899919ff7..31415e4f2 100644
--- a/numpy/core/src/multiarray/arrayfunction_override.c
+++ b/numpy/core/src/multiarray/arrayfunction_override.c
@@ -408,6 +408,7 @@ array_implement_c_array_function_creation(
kwargs = PyDict_New();
if (kwargs == NULL) {
Py_DECREF(args);
+ return NULL;
}
Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwnames);
for (Py_ssize_t i = 0; i < nkwargs; i++) {
@@ -416,6 +417,7 @@ array_implement_c_array_function_creation(
if (PyDict_SetItem(kwargs, key, value) < 0) {
Py_DECREF(args);
Py_DECREF(kwargs);
+ return NULL;
}
}
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 0da27ac17..7514c4d5d 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1704,7 +1704,7 @@ array_asarray(PyObject *NPY_UNUSED(ignored),
if (len_args != 1 || (kwnames != NULL)) {
if (npy_parse_arguments("asarray", args, len_args, kwnames,
- "object", NULL, &op,
+ "a", NULL, &op,
"|dtype", &PyArray_DescrConverter2, &type,
"|order", &PyArray_OrderConverter, &order,
"$like", NULL, &like,
@@ -1743,7 +1743,7 @@ array_asanyarray(PyObject *NPY_UNUSED(ignored),
if (len_args != 1 || (kwnames != NULL)) {
if (npy_parse_arguments("asanyarray", args, len_args, kwnames,
- "object", NULL, &op,
+ "a", NULL, &op,
"|dtype", &PyArray_DescrConverter2, &type,
"|order", &PyArray_OrderConverter, &order,
"$like", NULL, &like,
@@ -1782,7 +1782,7 @@ array_ascontiguousarray(PyObject *NPY_UNUSED(ignored),
if (len_args != 1 || (kwnames != NULL)) {
if (npy_parse_arguments("ascontiguousarray", args, len_args, kwnames,
- "object", NULL, &op,
+ "a", NULL, &op,
"|dtype", &PyArray_DescrConverter2, &type,
"$like", NULL, &like,
NULL, NULL, NULL) < 0) {
@@ -1820,7 +1820,7 @@ array_asfortranarray(PyObject *NPY_UNUSED(ignored),
if (len_args != 1 || (kwnames != NULL)) {
if (npy_parse_arguments("asfortranarray", args, len_args, kwnames,
- "object", NULL, &op,
+ "a", NULL, &op,
"|dtype", &PyArray_DescrConverter2, &type,
"$like", NULL, &like,
NULL, NULL, NULL) < 0) {
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index cffb1af99..b2f948d30 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -484,6 +484,33 @@ class TestArrayConstruction:
assert_(np.ascontiguousarray(d).flags.c_contiguous)
assert_(np.asfortranarray(d).flags.f_contiguous)
+ @pytest.mark.parametrize("func",
+ [np.array,
+ np.asarray,
+ np.asanyarray,
+ np.ascontiguousarray,
+ np.asfortranarray])
+ def test_bad_arguments_error(self, func):
+ with pytest.raises(TypeError):
+ func(3, dtype="bad dtype")
+ with pytest.raises(TypeError):
+ func() # missing arguments
+ with pytest.raises(TypeError):
+ func(1, 2, 3, 4, 5, 6, 7, 8) # too many arguments
+
+ @pytest.mark.parametrize("func",
+ [np.array,
+ np.asarray,
+ np.asanyarray,
+ np.ascontiguousarray,
+ np.asfortranarray])
+ def test_array_as_keyword(self, func):
+ # This should likely be made positional only, but do not change
+ # the name accidentally.
+ if func is np.array:
+ func(object=3)
+ else:
+ func(a=3)
class TestAssignment:
def test_assignment_broadcasting(self):