[Tutor] Reading arrays through an extension module

sessile@in-gen.net sessile@in-gen.net
Thu, 04 Nov 1999 22:07:28 -0500


At 08:16 AM 11/4/1999 -0500, Michael P. Reilly <arcege@shore.net> wrote:
<snip>
>
>      if ((tmp_float2_arr_py = PyList_New(index1)) != NULL)
>        { for (x=0; x<index1; x++)
>            { if ((tmplist = PyList_New(index2)) != NULL)
>                { PyList_SetItem(tmp_float2_arr_py, x, tmplist);
>                  for (y=0; y<index2; y++)
>                      PyList_SetItem(tmplist, y,
>                          PyFloat_AsDouble(tmp_float2_arr_c[(index2*x)+y])
>                        );
>                }
>              else
>                { Py_DECREF(tmp_float2_arr_py);
>                  return NULL;  /* raise NoMemoryError */
>                }
>            }
>        }
>      else
>        return NULL; /* else raise NoMemoryError */
>
<snip>

Thanks for the idea!  I played around with it for a while and ended
up getting the following version to work:

    if ((tmp_float2_arr_py = PyList_New(index1)) != NULL)
    {   for (x=0; x<index1; x++)
        {   if ((tmplist = PyList_New(index2)) != NULL)
            {   PyList_SetItem(tmp_float2_arr_py, x, tmplist);
                for (y=0; y<index2; y++)
                {   PyList_SetItem(tmplist, y, Py_BuildValue("f",
                                                 tmp_float2_arr[x][y]));
                }
            }
            else
            {    Py_DECREF(tmp_float2_arr_py);
                 return NULL;  
            }
        }
    }
    else
       return NULL;  
    
I'm thinking you meant for me to use PyFloat_FromDouble as the third
argument to PyList_SetItem rather than PyFloat_AsDouble... though I
didn't realize this until I had already gotten the Py_BuildValue version
to work.

Tomorrow I'm going to see what I can do with an array of 4 x 4 matrices!

Dean.