[Numpy-svn] r3273 - in trunk/numpy: core/src lib

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Oct 6 21:35:20 EDT 2006


Author: oliphant
Date: 2006-10-06 20:35:17 -0500 (Fri, 06 Oct 2006)
New Revision: 3273

Modified:
   trunk/numpy/core/src/arraytypes.inc.src
   trunk/numpy/lib/function_base.py
Log:
Fix ticket #325

Modified: trunk/numpy/core/src/arraytypes.inc.src
===================================================================
--- trunk/numpy/core/src/arraytypes.inc.src	2006-10-07 01:34:24 UTC (rev 3272)
+++ trunk/numpy/core/src/arraytypes.inc.src	2006-10-07 01:35:17 UTC (rev 3273)
@@ -404,6 +404,10 @@
 static PyObject *
 OBJECT_getitem(char *ip, PyArrayObject *ap)
 {
+        if (*(PyObject **)ip == NULL) {
+                Py_INCREF(Py_None);
+                return Py_None;
+        }
         if (!ap || PyArray_ISALIGNED(ap)) {
                 Py_INCREF(*(PyObject **)ip);
                 return *(PyObject **)ip;
@@ -771,7 +775,12 @@
         register intp i;
         int skip=@skip@;
         for(i=0;i<n;i++,ip++,op+=skip) {
-                @to at _setitem(*ip, (char *)op, aop);
+                if (*ip == NULL) {
+                        @to at _setitem(Py_False, (char *)op, aop);
+                }
+                else {
+                        @to at _setitem(*ip, (char *)op, aop);
+                }
         }
 }
 /**end repeat**/
@@ -1222,7 +1231,7 @@
                 sstride /= sizeof(PyObject **);
                 if (__ALIGNED(dst,sizeof(PyObject **)) && __ALIGNED(src, sizeof(PyObject **))) {
                         for (i=0; i<n; i++) {
-                                Py_INCREF(*src);
+                                Py_XINCREF(*src);
                                 Py_XDECREF(*dst);
                                 *dst = *src;
                                 dst += dstride;
@@ -1234,7 +1243,7 @@
                         for (i=0; i<n; i++) {
                                 dp = dst;
                                 sp = src;
-                                Py_INCREF(*sp);
+                                Py_XINCREF(*sp);
                                 Py_XDECREF(*dp);
                                 memcpy(dst, src, sizeof(PyObject *));
                                 dst += dstride;
@@ -1252,13 +1261,13 @@
 
         if (src != NULL) {
                 if (__ALIGNED(dst,sizeof(PyObject **)) && __ALIGNED(src,sizeof(PyObject **))) {
-                        Py_INCREF(*src);
+                        Py_XINCREF(*src);
                         Py_XDECREF(*dst);
                         *dst = *src;
                 }
                 else {
                         PyObject **dp=dst, **sp=src;
-                        Py_INCREF(*sp);
+                        Py_XINCREF(*sp);
                         Py_XDECREF(*dp);
                         memcpy(dst, src, sizeof(PyObject *));
                 }
@@ -1523,6 +1532,7 @@
 OBJECT_nonzero (PyObject **ip, PyArrayObject *ap)
 {
 
+        if (*ip == NULL) return FALSE;
         if (PyArray_ISALIGNED(ap)) {
                 return (Bool) PyObject_IsTrue(*ip);
         }
@@ -1623,6 +1633,11 @@
 static int
 OBJECT_compare(PyObject **ip1, PyObject **ip2, PyArrayObject *ap)
 {
+        if ((*ip1 == NULL) || (*ip2 == NULL)) {
+                if (ip1 == ip2) return 1;
+                if (ip1 == NULL) return -1;
+                return 1;
+        }                
         return PyObject_Compare(*ip1, *ip2);
 }
 
@@ -1698,9 +1713,15 @@
 {
         register intp i;
         PyObject *mp=ip[0]; *max_ind=0;
-        for(i=1; i<n; i++) {
+        i = 1;
+        while(i<n && mp==NULL) {
+                mp=ip[i];
+                i++;
+        }
+        if (i==n) return 0;
+        for(i=n; i<n; i++) {
                 ip++;
-                if (PyObject_Compare(*ip,mp) > 0) {
+                if (*ip != NULL && PyObject_Compare(*ip,mp) > 0) {
                         mp = *ip;
                         *max_ind=i;
                 }

Modified: trunk/numpy/lib/function_base.py
===================================================================
--- trunk/numpy/lib/function_base.py	2006-10-07 01:34:24 UTC (rev 3272)
+++ trunk/numpy/lib/function_base.py	2006-10-07 01:35:17 UTC (rev 3273)
@@ -822,6 +822,12 @@
             if (nargs > self.nin) or (nargs < self.nin_wo_defaults):
                 raise ValueError, "mismatch between python function inputs"\
                       " and received arguments"
+
+        if (self.lastcallargs != nargs):
+            self.lastcallargs = nargs
+            self.ufunc = None
+            self.nout = None
+
         if self.nout is None or self.otypes == '':
             newargs = []
             for arg in args:
@@ -832,21 +838,20 @@
             else:
                 self.nout = 1
                 theout = (theout,)
-            if self.otypes == '':
-                otypes = []
-                for k in range(self.nout):
-                    otypes.append(asarray(theout[k]).dtype.char)
-                self.otypes = ''.join(otypes)
+            otypes = []
+            for k in range(self.nout):
+                otypes.append(asarray(theout[k]).dtype.char)
+            self.otypes = ''.join(otypes)
 
-        if (self.ufunc is None) or (self.lastcallargs != nargs):
+        if (self.ufunc is None):
             self.ufunc = frompyfunc(self.thefunc, nargs, self.nout)
-            self.lastcallargs = nargs
 
         if self.nout == 1:
-            return array(self.ufunc(*args),copy=False).astype(self.otypes[0])
+            _res = array(self.ufunc(*args),copy=False).astype(self.otypes[0])
         else:
-            return tuple([array(x,copy=False).astype(c) \
+            _res = tuple([array(x,copy=False).astype(c) \
                           for x, c in zip(self.ufunc(*args), self.otypes)])
+        return _res
 
 def cov(m,y=None, rowvar=1, bias=0):
     """Estimate the covariance matrix.




More information about the Numpy-svn mailing list