swig double[], under the gun and need help

Diez B. Roggisch deets at nospam.web.de
Mon Aug 25 08:00:57 EDT 2008


Sells, Fred wrote:

> I'm using python 2.4 under linux (centos 5.1).
> I need to pass an array of doubles to a c function
> but am getting an error, shown near the bottom of
> this post.
> ----------------------------------------------------
> my swig interface file looks like this
> 
> * File: rug520.i */
> %module rug520
> %include "typemaps.i"
> %include "carrays.i"
> %array_class(double, doubleArray);
> %{
> #define SWIG_FILE_WITH_INIT
> #include "rug520.h"
> extern double[] nCmiArray;
> %}
> 
> 
> %apply int *OUTPUT {     char *sRugHier,
>                          char * sRugMax,
>                          int * iRugHier,
>                          int * iRugMax,
>                          double * nCmiValueHier,
>                          double * nCmiValueMax,
>                          int * iAdlSum,
>                          int * iCpsCode,
>                          char * sRugsVersion,
>                          char * sDllVersion,
>                          int * iError };
> 
> 
> int RugCalc( char * sMdsRecord,
>              char * sRehabType,
>              char * sModel,
>              int iQuarterlyFlag,
>              double nCmiArray[],
> 
>               char * sRugHier,
>              char * sRugMax,
>              int * iRugHier,
>              int * iRugMax,
>              double * nCmiValueHier,
>              double * nCmiValueMax,
>              int * iAdlSum,
>              int * iCpsCode,
>              char * sRugsVersion,
>              char * sDllVersion,
>              int * iError );
> 
> ------------------------------------------------------------------
> my test code looks like this:
> import sys, os, rug520
> cmi=[0.0] *59
> 
> def getrug(mds):
>     results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
>     print 'results', results
> 
> datafile = open('mdsdata.txt')
> for d in datafile:
>     if d[0]=='B':
>         getrug(d)
> ------------------------------------------------------------------------
> I get this error message
> File "testrug520.py", line 11, in ?
>     getrug(d)
>   File "testrug520.py", line 5, in getrug
>     results = rug520.RugCalc(mds, 'mcare', '34', 0, cmi)
> TypeError: in method 'RugCalc', argument 5 of type 'double []'
> 
> --------------------------------------------------------------------------
> 
> I'm guessing that I am not passing a double array to the c code.  I cannot
> change
> the c code due to politics.  I could write a c "wrapper" if I had to, but
> would rather stay within python or the swig interface definitions if
> possible/practical.
> 
> I'm not much of a c programmer; mostly java and python with a little c++.
> I've looked in the swig docs and tried google, but either have not found
> it or just don't understand what they are telling me here.

I don't know swig, but if all you have is a real C-API, try & use ctypes.
It's much easier to create bindings for, keeps you fully in the warm and
cozy womb of python programming and doesn't need no compilation to create
the actual binding.

Diez



More information about the Python-list mailing list