[Numpy-svn] r8144 - trunk/numpy/core/src/multiarray

numpy-svn@scip... numpy-svn@scip...
Sat Feb 20 12:07:26 CST 2010


Author: ptvirtan
Date: 2010-02-20 12:07:26 -0600 (Sat, 20 Feb 2010)
New Revision: 8144

Modified:
   trunk/numpy/core/src/multiarray/ctors.c
Log:
ENH: core: make PyArray_FromAny error message more clear

When building string/unicode arrays, PyArray_FromAny would try building
a 0-d array as a last resort. If the object is a finite-length sequence,
this always fails with an error "ValueError: setting an array element
with a sequence", as STRING/UNICODE_setitem do not allow this.

In some cases this message is confusing: for example on Py3,

    array([['a', 'b'], ['c', '\u03a3']], dtype='S2')

would fail because of this, even though the real error is that '\u03a3'
cannot be unambiguously cast to bytes().

Modified: trunk/numpy/core/src/multiarray/ctors.c
===================================================================
--- trunk/numpy/core/src/multiarray/ctors.c	2010-02-20 18:07:10 UTC (rev 8143)
+++ trunk/numpy/core/src/multiarray/ctors.c	2010-02-20 18:07:26 UTC (rev 8144)
@@ -1797,14 +1797,27 @@
                  * If object was explicitly requested,
                  * then try nested list object array creation
                  */
-                PyErr_Clear();
                 if (isobject) {
+                    PyErr_Clear();
                     Py_INCREF(newtype);
                     r = ObjectArray_FromNestedList
                         (op, newtype, flags & FORTRAN);
                     seq = TRUE;
                     Py_DECREF(newtype);
                 }
+                else if ((newtype->type_num == PyArray_STRING ||
+                          newtype->type_num == PyArray_UNICODE)
+                         && PySequence_Size(op) > 0)
+                {
+                    /* It is not possible to set a sequence into
+                     * a string/unicode 0-d array item -- bail out early
+                     * so that the error message is more clear.
+                     */
+                    seq = TRUE;
+                }
+                else {
+                    PyErr_Clear();
+                }
             }
             else {
                 seq = TRUE;



More information about the Numpy-svn mailing list