[Numpy-svn] r3031 - in trunk/numpy/core: . src

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Aug 16 20:01:19 CDT 2006


Author: oliphant
Date: 2006-08-16 20:01:14 -0500 (Wed, 16 Aug 2006)
New Revision: 3031

Modified:
   trunk/numpy/core/_internal.py
   trunk/numpy/core/numerictypes.py
   trunk/numpy/core/records.py
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/multiarraymodule.c
Log:
Fix ambiguity of list data-type objects.  Lists are always interpeted as array_descriptor format.  Use a comma-separated string for the other approach.  Add 'int', 'float', etc. to the sctypeDict so they can be interpreted in strings.

Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py	2006-08-16 23:14:10 UTC (rev 3030)
+++ trunk/numpy/core/_internal.py	2006-08-17 01:01:14 UTC (rev 3031)
@@ -120,7 +120,8 @@
     newlist = []
     hold = ''
 
-    for element in input.split(','):
+    listinput = input.split(',')
+    for element in listinput:
         if hold != '':
             item = hold + ',' + element
         else:

Modified: trunk/numpy/core/numerictypes.py
===================================================================
--- trunk/numpy/core/numerictypes.py	2006-08-16 23:14:10 UTC (rev 3030)
+++ trunk/numpy/core/numerictypes.py	2006-08-17 01:01:14 UTC (rev 3031)
@@ -433,6 +433,19 @@
     if val not in sctypeDict:
         sctypeDict[val] = key
 
+# Add additional strings to the sctypeDict
+
+_toadd = ['int', 'float', 'complex', 'bool', 'object', 'string', ('str', allTypes['string_']),
+          'unicode', 'object']
+
+for name in _toadd:
+    if isinstance(name, tuple):
+        sctypeDict[name[0]] = name[1]
+    else:
+        sctypeDict[name] = allTypes['%s_' % name]
+
+del _toadd, name
+
 # Now add the types we've determined to this module
 for key in allTypes:
     globals()[key] = allTypes[key]

Modified: trunk/numpy/core/records.py
===================================================================
--- trunk/numpy/core/records.py	2006-08-16 23:14:10 UTC (rev 3030)
+++ trunk/numpy/core/records.py	2006-08-17 01:01:14 UTC (rev 3031)
@@ -50,10 +50,14 @@
 
         if formats is None:
             raise ValueError, "Need formats argument"
+        if isinstance(formats, list):
+            if len(formats) < 2:
+                formats.append('')
+            formats = ','.join(formats)
         dtype = sb.dtype(formats, aligned)
         fields = dtype.fields
         if fields is None:
-            dtype = sb.dtype([formats], aligned)
+            dtype = sb.dtype([('f1', dtype)], aligned)
             fields = dtype.fields
         keys = dtype.names
         self._f_formats = [fields[key][0] for key in keys]

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-08-16 23:14:10 UTC (rev 3030)
+++ trunk/numpy/core/src/arrayobject.c	2006-08-17 01:01:14 UTC (rev 3031)
@@ -10596,7 +10596,6 @@
         {NULL, NULL, NULL, NULL},
 };
 
-static PyArray_Descr *_convert_from_list(PyObject *obj, int align, int try_descr);
 static PyArray_Descr *_convert_from_dict(PyObject *obj, int align);
 static PyArray_Descr *_convert_from_commastring(PyObject *obj, int align);
 static PyArray_Descr *_convert_from_array_descr(PyObject *obj);
@@ -10620,33 +10619,12 @@
                 if PyDict_Check(odescr)
                         conv =  _convert_from_dict(odescr, 1);
                 else if PyList_Check(odescr) {
-                        conv = _convert_from_list(odescr, 1, 0);
-                        if (conv == NULL) {
-                                /* There is an errror.  Possibly it's 
-                                   because we have an array_descriptor.
-                                   Try converting from an array_descriptor. 
-                                   If that fails then raise the old error. 
-                                */
-                                PyObject *type, *value, *traceback;
-                                PyArray_Descr *temp;
-                                PyErr_Fetch(&type, &value, &traceback);
-                                temp = _convert_from_array_descr(odescr);
-                                if (!PyErr_Occurred()) {
-                                        Py_DECREF(temp);
-                                        Py_XDECREF(type);
-                                        Py_XDECREF(value);
-                                        Py_XDECREF(traceback);
-                                        PyErr_SetString(PyExc_ValueError,
-                                                        "align cannot be True" \
-                                                        " with array_descriptor " \
-                                                        "specification.");
-                                }
-                                else {
-                                        PyErr_Restore(type, value, traceback);
-                                }
-                                return NULL;
-                        }
-                }
+			PyErr_SetString(PyExc_ValueError,
+					"align cannot be True"		\
+					" with array_descriptor "	\
+					"specification.");
+			return NULL;
+		}
                 else if PyString_Check(odescr)
                         conv = _convert_from_commastring(odescr, 1);
                 else {
@@ -10662,15 +10640,6 @@
                 return NULL;
         }
 
-        if PyList_Check(odescr) {
-                conv = _convert_from_array_descr(odescr);
-                if (!conv) {
-                        PyErr_Clear();
-                        conv = _convert_from_list(odescr, 0, 0);
-                }
-                return (PyObject *)conv;
-        }
-
         if (!PyArray_DescrConverter(odescr, &conv))
                 return NULL;
         /* Get a new copy of it unless it's already a copy */

Modified: trunk/numpy/core/src/multiarraymodule.c
===================================================================
--- trunk/numpy/core/src/multiarraymodule.c	2006-08-16 23:14:10 UTC (rev 3030)
+++ trunk/numpy/core/src/multiarraymodule.c	2006-08-17 01:01:14 UTC (rev 3031)
@@ -4246,7 +4246,7 @@
 */
 
 static PyArray_Descr *
-_convert_from_list(PyObject *obj, int align, int try_descr)
+_convert_from_list(PyObject *obj, int align)
 {
 	int n, i;
 	int totalsize;
@@ -4260,6 +4260,11 @@
         int hasobject=0;
 	
 	n = PyList_GET_SIZE(obj);
+	/* Ignore any empty string at end which _internal._commastring
+	   can produce  */
+	key = PyList_GET_ITEM(obj, n-1);
+	if (PyString_Check(key) && PyString_GET_SIZE(key) == 0) n = n-1;
+	/* End ignore code.*/ 
 	totalsize = 0;
 	if (n==0) return NULL;
 	nameslist = PyTuple_New(n);
@@ -4304,16 +4309,7 @@
  fail:
 	Py_DECREF(nameslist);
 	Py_DECREF(fields);
-	if (!try_descr) return NULL;
-	if (align) {
-		PyErr_SetString(PyExc_ValueError, 
-				"failed to convert from list of formats "\
-				"and align cannot be 1 for conversion from "\
-				"array_descr structure");
-		return NULL;
-	}
-	PyErr_Clear();
-	return _convert_from_array_descr(obj);
+	return NULL;
 }
 
 
@@ -4348,7 +4344,7 @@
 		}
 	}
 	else {
-		res = _convert_from_list(listobj, align, 0);
+		res = _convert_from_list(listobj, align);
 	}
 	Py_DECREF(listobj);
 	if (!res && !PyErr_Occurred()) {
@@ -4716,7 +4712,7 @@
 	}
 	/* or a list */
 	else if (PyList_Check(obj)) {
-		*at = _convert_from_list(obj,0,1);
+		*at = _convert_from_array_descr(obj);
 		if (*at == NULL) {
 			if (PyErr_Occurred()) return PY_FAIL;
 			goto fail;



More information about the Numpy-svn mailing list