[Numpy-svn] r3221 - trunk/numpy/core/src

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Sep 27 12:40:35 CDT 2006


Author: oliphant
Date: 2006-09-27 12:40:14 -0500 (Wed, 27 Sep 2006)
New Revision: 3221

Modified:
   trunk/numpy/core/src/arraytypes.inc.src
Log:
Fix setting string and unicode arrays so that non-string sequences are not allowed.

Modified: trunk/numpy/core/src/arraytypes.inc.src
===================================================================
--- trunk/numpy/core/src/arraytypes.inc.src	2006-09-27 17:01:35 UTC (rev 3220)
+++ trunk/numpy/core/src/arraytypes.inc.src	2006-09-27 17:40:14 UTC (rev 3221)
@@ -92,7 +92,14 @@
         else {
                 temp = (@typ@)@func2@(op);
         }
-        if (PyErr_Occurred()) return -1;
+        if (PyErr_Occurred()) {
+                if (PySequence_Check(op)) {
+                        PyErr_Clear();
+                        PyErr_SetString(PyExc_TypeError, "setting an array"\
+                                        " element with a sequence.");
+                }
+                return -1;
+        }
         if (ap == NULL || PyArray_ISBEHAVED(ap))
                 *((@typ@ *)ov)=temp;
         else {
@@ -265,6 +272,13 @@
         char *buffer;
 #endif
 
+        if (!PyString_Check(op) && !PyUnicode_Check(op) && 
+            PySequence_Check(op)) {
+                PyErr_SetString(PyExc_TypeError, 
+                                "setting an array element with a sequence");
+                return -1;
+        }
+
         if ((temp=PyObject_Unicode(op)) == NULL) return -1;
         ptr = PyUnicode_AS_UNICODE(temp);
         if ((ptr == NULL) || (PyErr_Occurred())) {
@@ -320,9 +334,15 @@
 {
         char *ptr;
         int len;
-        PyObject *temp=PyObject_Str(op);
+        PyObject *temp=NULL;
 
-        if (temp == NULL) return -1;
+        if (!PyString_Check(op) && !PyUnicode_Check(op) && 
+            PySequence_Check(op)) {
+                PyErr_SetString(PyExc_TypeError, 
+                                "setting an array element with a sequence");
+                return -1;
+        }
+        if ((temp = PyObject_Str(op)) == NULL) return -1;
 
         if (PyString_AsStringAndSize(temp, &ptr, &len) == -1) {
                 Py_DECREF(temp);



More information about the Numpy-svn mailing list