diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2019-07-15 15:14:29 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-15 15:14:29 -0500 |
commit | 28701059c62817bbf32f52c75ab91d0166531c96 (patch) | |
tree | afec6e35809f0d5fc8eec15eeb126e5e7762a515 | |
parent | 16315fdfe94168a60aae83d895b0073f272d7f16 (diff) | |
parent | 6fdf488e9f4c5c129e40adc239ac2b74ae0b53f4 (diff) | |
download | numpy-28701059c62817bbf32f52c75ab91d0166531c96.tar.gz |
Merge pull request #13913 from mattip/improve-error
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): |