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

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Oct 18 02:08:19 CDT 2006


Author: oliphant
Date: 2006-10-18 02:08:13 -0500 (Wed, 18 Oct 2006)
New Revision: 3356

Modified:
   trunk/numpy/core/src/ufuncobject.c
Log:
Allow entry in self->types to type= keyword.  Change from dtype= keyword because it doesn't always follow the rules of data-type conversion.

Modified: trunk/numpy/core/src/ufuncobject.c
===================================================================
--- trunk/numpy/core/src/ufuncobject.c	2006-10-18 06:38:26 UTC (rev 3355)
+++ trunk/numpy/core/src/ufuncobject.c	2006-10-18 07:08:13 UTC (rev 3356)
@@ -674,6 +674,7 @@
         PyArray_Descr *dtype;
         int nargs;
         int i, j;
+        int strtype=0;
 
         nargs = self->nargs;
  
@@ -681,14 +682,35 @@
                 n = PyTuple_GET_SIZE(type_tup);
                 if (n != 1 && n != nargs) {
                         PyErr_Format(PyExc_ValueError, 
-                                     "a type-tuple must be specified "  \
+                                     "a type-tuple must be specified " \
                                      "of length 1 or %d for %s", nargs, 
                                      self->name ? self->name : "(unknown)");
                         return -1;
                 }
         }
-        else {
-                n = 1;
+        else if PyString_Check(type_tup) {
+                int slen;
+                char *thestr;
+                slen = PyString_GET_SIZE(type_tup);
+                thestr = PyString_AS_STRING(type_tup);
+                for (i=0; i < slen-2; i++) {
+                        if (thestr[i] == '-' && thestr[i+1] == '>')
+                                break;
+                }
+                if (i < slen-2) {
+                        strtype = 1;
+                        n = slen-2;
+                        if (i != self->nin || 
+                            slen-2-i != self->nout) {
+                                PyErr_Format(PyExc_ValueError, 
+                                             "a type-string for %s, "   \
+                                             "requires %d typecode(s) before " \
+                                             "and %d after the -> sign", 
+                                             self->name ? self->name : "(unknown)",
+                                             self->nin, self->nout);
+                                return -1;
+                        } 
+                }
         }
         rtypenums = (int *)_pya_malloc(n*sizeof(int));
         if (rtypenums==NULL) {
@@ -696,7 +718,23 @@
                 return -1;
         }
         
-        if (PyTuple_Check(type_tup)) {
+        if (strtype) {
+                char *ptr;
+                ptr = PyString_AS_STRING(type_tup);
+                i = 0;
+                while (i < n) {
+                        if (*ptr == '-' || *ptr == '>') { 
+                                ptr++;
+                                continue;
+                        }
+                        dtype = PyArray_DescrFromType((int) *ptr);
+                        if (dtype == NULL) goto fail;
+                        rtypenums[i] = dtype->type_num;
+                        Py_DECREF(dtype);
+                        ptr++; i++;
+                }
+        }
+        else if (PyTuple_Check(type_tup)) {
                 for (i=0; i<n; i++) {
                         if (PyArray_DescrConverter(PyTuple_GET_ITEM     \
                                                    (type_tup, i),
@@ -781,6 +819,7 @@
         }
         PyErr_SetString(PyExc_TypeError, msg);
 
+
  fail:
         _pya_free(rtypenums);
         return -1;
@@ -788,7 +827,6 @@
  finish:
         _pya_free(rtypenums);
         return 0;
-
                 
 }
 
@@ -1534,8 +1572,9 @@
 	loop->errobj = NULL;
         loop->notimplemented = 0;
 
+        name = self->name ? self->name : "";
 
-        /* Extract dtype= keyword and
+        /* Extract type= keyword and
            extobj= keyword if present
            Raise an error if anything else present in the keyword dictionary
         */
@@ -1550,18 +1589,18 @@
                         if (strncmp(PyString_AS_STRING(key), "extobj", 6) == 0) {
                                 extobj = value;
                         }
-                        else if (strncmp(PyString_AS_STRING(key), "dtype", 5) == 0) {
+                        else if (strncmp(PyString_AS_STRING(key), "type", 5) == 0) {
                                 typetup = value;
                         }
                         else {
-                                PyErr_Format(PyExc_TypeError, "'%s' is an invalid keyword",
-                                             PyString_AS_STRING(key));
+                                PyErr_Format(PyExc_TypeError, 
+                                             "'%s' is an invalid keyword to %s",
+                                             PyString_AS_STRING(key), name);
                                 goto fail;
                         }
                 }
         }
 
-        name = self->name ? self->name : "";
         if (extobj == NULL) {
                 if (PyUFunc_GetPyValues(name,
                                         &(loop->bufsize), &(loop->errormask),



More information about the Numpy-svn mailing list