[Numpy-discussion] Error in NA_IoArray?

Todd Miller jmiller at stsci.edu
Fri Nov 19 13:57:03 CST 2004


On Fri, 2004-11-19 at 18:06 -0200, Paulo J. S. Silva wrote:
> Hello,
> 
> I am using swig to wrap some numerical code. I have managed to make swig
> understand and convert between numarrays and C arrays. However if I use
> NA_IoArray to make the convertion and pass an integer array when a
> double array is expected the changes are not copied back to the original
> array. Let me give a "minimal" example.
> 
> When I want to wrap the following function:
> 
> void inc(int len, double *v) {
>   int i;
>   for (i = 0; i < len; i++) v[i]++;
> }
> 
> Swig generates the following code, that looks right to me.
> 
> static PyObject *_wrap_inc(PyObject *self, PyObject *args) {
>     PyObject *resultobj;
>     int arg1 ;
>     double *arg2 = (double *) 0 ;
>     PyArrayObject *NAimg2 = NULL ;
>     PyObject * obj1 = 0 ;
>     
>     if(!PyArg_ParseTuple(args,(char *)"iO:inc",&arg1,&obj1)) goto fail;
>     {
>         PyArrayObject *NAimg2  = NA_IoArray(obj1, tFloat64,
> NUM_C_ARRAY);
>         if (!NAimg2) {
>             printf("**** no (double) numarray *****\n");
>             SWIG_exception(SWIG_RuntimeError, "No double Array Found");
>             return 0;
>         }
>         arg2 = (double *) NA_OFFSETDATA(NAimg2);
>     }
>     inc(arg1,arg2);
>     

I definitely see the problem now:  you have NAimg2 in two places,  and
the "real" NAimg2 goes out of scope so the one at function scope (NULL)
is the one that is DECREF'ed,  not the real one.  

Regards,
Todd

>     Py_INCREF(Py_None); resultobj = Py_None;
>     {
>         Py_XDECREF(NAimg2);
>     }
>     return resultobj;
>     fail:
>     {
>         Py_XDECREF(NAimg2);
>     }
>     return NULL;
> }
> 
> However if I run the following python code:
> 
> >>> import test_na_ioarray
> >>> from numarray import *
> >>> v = zeros((5,))
> >>> test_na_ioarray(5, v)
> >>> print v
> [0 0 0 0 0]
> 
> Shouldn't the resultant array be full of ones (when casting the Float64
> array of ones to int)?
> 
> Thanks,
> 
> Paulo
> 
> Obs: The code runs fine if the original vector is already a Float64
> array.
> 
> 
> 
> -------------------------------------------------------
> This SF.Net email is sponsored by: InterSystems CACHE
> FREE OODBMS DOWNLOAD - A multidimensional database that combines
> robust object and relational technologies, making it a perfect match
> for Java, C++,COM, XML, ODBC and JDBC. www.intersystems.com/match8
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the Numpy-discussion mailing list