ctypes: error passing a list of str to a fortran dll

Charles Sanders C.delete_this.Sanders at BoM.GOv.AU
Tue Jun 5 06:15:59 CEST 2007


luis wrote:
> I'm using ctypes to call a fortran dll from python. I have no problems
> passing integer and double arryas, but I have an error with str arrys.
> For example:
[snip]

I do not know about Microsoft Fortran compilers (your mention
of dll indicates you are probably using MS), nor much about
Python, but the C equivalent of a given Fortran call is operating
system and compiler dependent. You should consult the Fortran
compiler manual for the compiler used to create the DLL.

Despite this, many (but not all) C to Fortran interfaces have
the following characteristics

	+ C name is Fortran name in lower case
	+ Fortran REAL, DOUBLE PRECISION, INTEGER etc parameters
	  are pointers to the parameter in C, ie float*, etc
	+ Fortran REAL etc arrays are pointers in C (same as
	  C arrays decay to pointers).
	+ Fortran CHARACTER and CHARACTER arrays are passed as TWO
	  parameters, one a pointer to the start of the variable
	  or array, and the other the length as an integer, not
	  a pointer. The length parameters follow all other
	  parameters, in order of the character variables/arrays.

Variations I have seen (all on Unix) include using upper case
instead of lower, prepending (or postpending) an underscore (or
other character(s)) to the subroutine or function name, and
using special "character descriptors" (packing address and
length into one "word") for character variables. There are
almost certainly more variations that I have not seen.

For example, given a FORTRAN declaration

	SUBROUTINE X( CV, RV, CA, N )
	CHARACTER*(*) CV
  	REAL          RV
	CHARACTER*(*) CA(*)
	INTEGER       N

The C equivalent is likely to be

	void x( char *pcv, float *prv, char *pca, int *pn,
	    int lv, int la)

Where lv will hold the length  of cv and la the length of
each element of ca (required to be the same for all elements
of the array). Fortran uses fixed length character strings,
padded with blanks.

Given the error message

 > ValueError: Procedure probably called with not enough
 > arguments (4 bytes missing)

I suspect that your Fortran compiler is one of the many which
do this, and the missing 4 bytes are the integer length of
each element of the character array.

Also, I noticed you seem to be passing an array of character
pointers rather than an array of characters. It is doubtful that
Fortran can handle this. You will probably have to pad the strings
to a maximal length with spaces, concatanate then into one big
string, and pass this by reference together with their padded
length. Your Fortran may (but probably won't) have extensions
that allow you to pass an array of character pointers.


Charles



More information about the Python-list mailing list