[Matrix-SIG] fromfunction broken in LLNL Distribution #10 ?

Ted Horst Ted.Horst@wdr.com
Mon, 29 Mar 99 12:06:14 -0600


When I try fromfunction, I get the following exception:

>>> Numeric.fromfunction(lambda x:x, 10)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/home/horst/tmp/python/NumPy/Numeric.py", line 156, in fromfunction
    return apply(function, tuple(indices(dimensions)))
  File "/home/horst/tmp/python/NumPy/Numeric.py", line 149, in indices
    tmp = ones(dimensions, typecode)
  File "/home/horst/tmp/python/NumPy/Numeric.py", line 306, in ones
    return zeros(shape, typecode)+array(1, typecode)
TypeError: argument 2: expected string, None found

In distribution #6 there was a check in indices for typecode == None, this will fix the problem, but a better solution is probably to put the checking for Py_None in array_zeros like it is in array_array.

Also the doc string for zeros incorrectly states that the default type is 'd'.  It is 'l'.  Same for fromstring.

Here is a patch:

*** multiarraymodule.c.orig     Mon Mar 29 11:39:45 1999
--- multiarraymodule.c  Mon Mar 29 11:54:32 1999
***************
*** 880,889 ****
        return ret;
  }
  
! static char doc_zeros[] = "zeros(d1,...,dn,typecode='d') will return a new array of shape (d1,...,dn) and type typecode with all it's entries initialized to zero.";
  
  static PyObject *array_zeros(PyObject *ignored, PyObject *args) {
!       PyObject *op, *sequence;
        PyArrayObject *ret;
        char type_char='l';
        char *type = &type_char, *dptr;
--- 880,889 ----
        return ret;
  }
  
! static char doc_zeros[] = "zeros(d1,...,dn,typecode='l') will return a new array of shape (d1,...,dn) and type typecode with all it's entries initialized to zero.";
  
  static PyObject *array_zeros(PyObject *ignored, PyObject *args) {
!       PyObject *op, *sequence, *tpo=Py_None;
        PyArrayObject *ret;
        char type_char='l';
        char *type = &type_char, *dptr;
***************
*** 890,897 ****
        int i, nd, n, dimensions[MAX_DIMS];
        char all_zero[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        
!       if (PyArg_ParseTuple(args, "O|s", &sequence, &type) == NULL) return NULL;
  
        if ((nd=PySequence_Length(sequence)) == -1) {
                PyErr_Clear();
                if (!(op = PyNumber_Int(sequence))) return NULL;
--- 890,905 ----
        int i, nd, n, dimensions[MAX_DIMS];
        char all_zero[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        
!       if (PyArg_ParseTuple(args, "O|O", &sequence, &tpo) == NULL) return NULL;
  
+       if (tpo != Py_None) {
+               type = PyString_AsString(tpo);
+               if (!type)
+                       return NULL;
+               if (!*type)
+                       type = &type_char;
+       }
+ 
        if ((nd=PySequence_Length(sequence)) == -1) {
                PyErr_Clear();
                if (!(op = PyNumber_Int(sequence))) return NULL;
***************
*** 924,930 ****
        return (PyObject *)ret;
  }
  
! static char doc_fromString[] = "fromString(string, count=None, typecode='d') returns a new 1d array initialized from the raw binary data in string.  If count is not equal to None, the new array will have count elements, otherwise it's size is determined by the siuze of string.";
  
  static PyObject *array_fromString(PyObject *ignored, PyObject *args) {
        PyArrayObject *ret;
--- 932,938 ----
        return (PyObject *)ret;
  }
  
! static char doc_fromString[] = "fromstring(string, count=None, typecode='l') returns a new 1d array initialized from the raw binary data in string.  If count is not equal to None, the new array will have count elements, otherwise it's size is determined by the size of string.";
  
  static PyObject *array_fromString(PyObject *ignored, PyObject *args) {
        PyArrayObject *ret;