summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-05-02 12:51:41 -0500
committerSebastian Berg <sebastian@sipsolutions.net>2020-05-12 15:46:05 -0500
commit9cc3af7abf8728534939d37dcda07c66d68381a3 (patch)
tree13887920b3647ef7d744bc1aeb553699648668d5 /numpy
parentffe76ac8df65c8e7831df9924a782276e060f3e6 (diff)
downloadnumpy-9cc3af7abf8728534939d37dcda07c66d68381a3.tar.gz
MAINT: simplify iterator casting error creation
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/nditer_constr.c42
-rw-r--r--numpy/core/tests/test_api.py11
2 files changed, 22 insertions, 31 deletions
diff --git a/numpy/core/src/multiarray/nditer_constr.c b/numpy/core/src/multiarray/nditer_constr.c
index e40a2d594..620c7d593 100644
--- a/numpy/core/src/multiarray/nditer_constr.c
+++ b/numpy/core/src/multiarray/nditer_constr.c
@@ -1308,21 +1308,11 @@ npyiter_check_casting(int nop, PyArrayObject **op,
!PyArray_CanCastArrayTo(op[iop],
op_dtype[iop],
casting)) {
- PyObject *errmsg;
- errmsg = PyUString_FromFormat(
- "Iterator operand %d dtype could not be cast from ",
- iop);
- PyUString_ConcatAndDel(&errmsg,
- PyObject_Repr((PyObject *)PyArray_DESCR(op[iop])));
- PyUString_ConcatAndDel(&errmsg,
- PyUString_FromString(" to "));
- PyUString_ConcatAndDel(&errmsg,
- PyObject_Repr((PyObject *)op_dtype[iop]));
- PyUString_ConcatAndDel(&errmsg,
- PyUString_FromFormat(" according to the rule %s",
- npyiter_casting_to_string(casting)));
- PyErr_SetObject(PyExc_TypeError, errmsg);
- Py_DECREF(errmsg);
+ PyErr_Format(PyExc_TypeError,
+ "Iterator operand %d dtype could not be cast from "
+ "%R to %R according to the rule %s",
+ iop, PyArray_DESCR(op[iop]), op_dtype[iop],
+ npyiter_casting_to_string(casting));
return 0;
}
/* Check write (temp -> op) casting */
@@ -1330,22 +1320,12 @@ npyiter_check_casting(int nop, PyArrayObject **op,
!PyArray_CanCastTypeTo(op_dtype[iop],
PyArray_DESCR(op[iop]),
casting)) {
- PyObject *errmsg;
- errmsg = PyUString_FromString(
- "Iterator requested dtype could not be cast from ");
- PyUString_ConcatAndDel(&errmsg,
- PyObject_Repr((PyObject *)op_dtype[iop]));
- PyUString_ConcatAndDel(&errmsg,
- PyUString_FromString(" to "));
- PyUString_ConcatAndDel(&errmsg,
- PyObject_Repr((PyObject *)PyArray_DESCR(op[iop])));
- PyUString_ConcatAndDel(&errmsg,
- PyUString_FromFormat(", the operand %d dtype, "
- "according to the rule %s",
- iop,
- npyiter_casting_to_string(casting)));
- PyErr_SetObject(PyExc_TypeError, errmsg);
- Py_DECREF(errmsg);
+ PyErr_Format(PyExc_TypeError,
+ "Iterator requested dtype could not be cast from "
+ "%R to %R, the operand %d dtype, "
+ "according to the rule %s",
+ op_dtype[iop], PyArray_DESCR(op[iop]), iop,
+ npyiter_casting_to_string(casting));
return 0;
}
diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py
index 71b46e551..5a801f61c 100644
--- a/numpy/core/tests/test_api.py
+++ b/numpy/core/tests/test_api.py
@@ -1,6 +1,7 @@
import sys
import numpy as np
+from numpy.core._rational_tests import rational
import pytest
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_raises, assert_warns,
@@ -141,6 +142,16 @@ def test_array_array():
assert_equal(np.array([(1.0,) * 10] * 10, dtype=np.float64),
np.ones((10, 10), dtype=np.float64))
+@pytest.mark.parametrize("array", [True, False])
+def test_array_impossible_casts(array):
+ # All builtin types can forst cast as least theoretically
+ # but user dtypes cannot necessarily.
+ rt = rational(1, 2)
+ if array:
+ rt = np.array(rt)
+ with assert_raises(ValueError):
+ np.array(rt, dtype="M8")
+
def test_fastCopyAndTranspose():
# 0D array