diff options
author | mattip <matti.picus@gmail.com> | 2019-07-04 10:03:15 -0700 |
---|---|---|
committer | mattip <matti.picus@gmail.com> | 2019-07-04 10:03:15 -0700 |
commit | 6fdf488e9f4c5c129e40adc239ac2b74ae0b53f4 (patch) | |
tree | b13b13b53a483e6824c405b8c213ccd02ca9731c | |
parent | deea4983aedfa96905bbaee64e3d1de84144303f (diff) | |
download | numpy-6fdf488e9f4c5c129e40adc239ac2b74ae0b53f4.tar.gz |
ENH: improve error message for ragged-array creation failure
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 7 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 5 |
2 files changed, 11 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index c17266251..53efb1cea 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1849,6 +1849,13 @@ PyArray_GetArrayParamsFromObject(PyObject *op, *out_arr = NULL; return 0; } + if (is_object && (requested_dtype != NULL) && + (requested_dtype->type_num != NPY_OBJECT)) { + PyErr_SetString(PyExc_ValueError, + "cannot create an array from unequal-length (ragged) sequences"); + Py_DECREF(*out_dtype); + return -1; + } /* If object arrays are forced */ if (is_object) { Py_DECREF(*out_dtype); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 3a5a5b939..53e538f7d 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -44,7 +44,7 @@ from numpy.testing import ( assert_, assert_raises, assert_warns, assert_equal, assert_almost_equal, assert_array_equal, assert_raises_regex, assert_array_almost_equal, assert_allclose, IS_PYPY, HAS_REFCOUNT, assert_array_less, runstring, - temppath, suppress_warnings, break_cycles, + temppath, suppress_warnings, break_cycles, assert_raises_regex, ) from numpy.core.tests._locales import CommaDecimalPointLocale @@ -497,6 +497,9 @@ class TestArrayConstruction(object): assert_(np.ascontiguousarray(d).flags.c_contiguous) assert_(np.asfortranarray(d).flags.f_contiguous) + def test_ragged(self): + assert_raises_regex(ValueError, 'ragged', + np.array, [[1], [2, 3]], dtype=int) class TestAssignment(object): def test_assignment_broadcasting(self): |