[Matrix-SIG] More fixes for type 'O' arrays

Charles G Waldman cgw@fnal.gov
Thu, 20 Jan 2000 13:59:13 -0600 (CST)


The bug:

Python 1.5.2+ (#7, Jan 13 2000, 16:18:27)  [GCC 2.95.2 19991024 (release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from Numeric import *
>>> x=array(['a','b','c'],'O')
>>> y=array(['d','e','f'],'O')
>>> dot(x,y)
Segmentation fault


The fix:  (the patch to multiarraymodule is the real fix, the patch to
   Numeric.py is just to give a more meaningful message with the exception)

Index: Lib/Numeric.py
===================================================================
RCS file: /cvsroot/numpy/Numerical/Lib/Numeric.py,v
retrieving revision 1.7
diff -u -r1.7 Numeric.py
--- Lib/Numeric.py	2000/01/19 23:13:41	1.7
+++ Lib/Numeric.py	2000/01/20 19:56:38
@@ -212,11 +212,11 @@
     """
     try:
         return multiarray.innerproduct(a,b)
-    except(TypeError):
+    except TypeError,detail:
         if array(a).shape == () or array(b).shape == ():
             return a*b
         else:
-            raise TypeError, "invalid types for dot"
+            raise TypeError, detail or "invalid types for dot"
 
 def dot(a, b):
     """dot(a,b) returns matrix-multiplication between a and b.  The product-sum
Index: Src/multiarraymodule.c
===================================================================
RCS file: /cvsroot/numpy/Numerical/Src/multiarraymodule.c,v
retrieving revision 1.5
diff -u -r1.5 multiarraymodule.c
--- Src/multiarraymodule.c	2000/01/19 23:13:41	1.5
+++ Src/multiarraymodule.c	2000/01/20 19:56:39
@@ -574,10 +574,14 @@
 	PyObject *tmp1, *tmp2, *tmp;
 	for(i=0;i<n;i++,ip1+=is1,ip2+=is2) { 
 		tmp1 = PyNumber_Multiply(*((PyObject **)ip1), *((PyObject **)ip2));
+		if (!tmp1)
+		    return;  
 		if (i == 0) {
 			tmp = tmp1;
 		} else {
 			tmp2 = PyNumber_Add(tmp, tmp1);
+			if (!tmp2)
+			    return; 
 			Py_XDECREF(tmp);
 			tmp = tmp2;
 			Py_XDECREF(tmp1);