summaryrefslogtreecommitdiff
path: root/numpy/f2py/src/fortranobject.c
diff options
context:
space:
mode:
authorWarren Weckesser <warren.weckesser@gmail.com>2021-12-20 10:35:31 -0500
committerWarren Weckesser <warren.weckesser@gmail.com>2021-12-20 10:35:31 -0500
commit271010f1037150e95017f803f4214b8861e528f2 (patch)
tree6da1e5a79eb418e38e21b7aecd7aa0dbda7691cb /numpy/f2py/src/fortranobject.c
parente3b831cd85e2f4d73b0db8e8d501828b50a96c64 (diff)
downloadnumpy-271010f1037150e95017f803f4214b8861e528f2.tar.gz
BUG: f2py: Simplify creation of an exception message. Closes gh-19000.
Diffstat (limited to 'numpy/f2py/src/fortranobject.c')
-rw-r--r--numpy/f2py/src/fortranobject.c28
1 files changed, 12 insertions, 16 deletions
diff --git a/numpy/f2py/src/fortranobject.c b/numpy/f2py/src/fortranobject.c
index 0b32137ef..c96378170 100644
--- a/numpy/f2py/src/fortranobject.c
+++ b/numpy/f2py/src/fortranobject.c
@@ -702,15 +702,14 @@ check_and_fix_dimensions(const PyArrayObject *arr, const int rank,
npy_intp *dims);
static int
-count_negative_dimensions(const int rank, const npy_intp *dims)
+find_first_negative_dimension(const int rank, const npy_intp *dims)
{
- int i = 0, r = 0;
- while (i < rank) {
- if (dims[i] < 0)
- ++r;
- ++i;
+ for (int i = 0; i < rank; ++i) {
+ if (dims[i] < 0) {
+ return i;
+ }
}
- return r;
+ return -1;
}
#ifdef DEBUG_COPY_ND_ARRAY
@@ -795,15 +794,12 @@ array_from_pyobj(const int type_num, npy_intp *dims, const int rank,
((intent & F2PY_INTENT_CACHE) && (obj == Py_None)) ||
((intent & F2PY_OPTIONAL) && (obj == Py_None))) {
/* intent(cache), optional, intent(hide) */
- if (count_negative_dimensions(rank, dims) > 0) {
- int i;
- strcpy(mess,
- "failed to create intent(cache|hide)|optional array"
- "-- must have defined dimensions but got (");
- for (i = 0; i < rank; ++i)
- sprintf(mess + strlen(mess), "%" NPY_INTP_FMT ",", dims[i]);
- strcat(mess, ")");
- PyErr_SetString(PyExc_ValueError, mess);
+ int i = find_first_negative_dimension(rank, dims);
+ if (i >= 0) {
+ PyErr_Format(PyExc_ValueError,
+ "failed to create intent(cache|hide)|optional array"
+ " -- must have defined dimensions, but dims[%d] = %"
+ NPY_INTP_FMT, i, dims[i]);
return NULL;
}
arr = (PyArrayObject *)PyArray_New(&PyArray_Type, rank, dims, type_num,