472,328 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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

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:

.....
StringVector = c_char_p * len(id) # id is a list of strings

Id_dat=StringVector()
for i in range(len(Id)):
....Id_dat[i]=id[i]

n=c_int(len(Id_dat))

myDll = windll.LoadLibrary(org)

myDll.myFunc(byref(n), byref(Id_dat))

and then

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

In a similar way I have bo problemns with int or double arryas

Some suggestions are wellcome !

Jun 4 '07 #1
2 4240
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
Jun 5 '07 #2
On 5 jun, 06:15, Charles Sanders <C.delete_this.Sand...@BoM.GOv.AU>
wrote:
luis wrote:
I'm using ctypes to call afortrandllfrom 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 MicrosoftFortrancompilers (your mention
ofdllindicates you are probably using MS), nor much about
Python, but the C equivalent of a givenFortrancall is operating
system and compiler dependent. You should consult theFortran
compiler manual for the compiler used to create theDLL.

Despite this, many (but not all) C toFortraninterfaces have
the following characteristics

+ C name isFortranname in lower case
+FortranREAL, DOUBLE PRECISION, INTEGER etc parameters
are pointers to the parameter in C, ie float*, etc
+FortranREAL etc arrays are pointers in C (same as
C arrays decay to pointers).
+FortranCHARACTER 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 aFORTRANdeclaration

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).Fortranuses 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 yourFortrancompiler 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 thatFortrancan 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. YourFortranmay (but probably won't) have extensions
that allow you to pass an array of character pointers.

Charles
The solution proposed by Jugoslav Dujic, from comp lang fortran is

#Python script calling fortran subroutine
from ctypes import *
ap = windll.LoadLibrary(self.locationDll)
ap.TEST_02.restype=None
myCadena='D:\BBDD\PythonScripts\pru.txt'
strLen=len(myCadena)
pf_myCadena = c_char_p(myCadena)
pf_srLen = c_int(strLen)
ap.TEST_02(pf_myCadena,pf_srLen)

!fortran dll
subroutine TEST_02(s)
!DEC$ ATTRIBUTES DLLEXPORT :: TEST_02
!DEC$ATTRIBUTES REFERENCE:: s
!INTEGER(4):: n
CHARACTER(*):: s

open (unit=31,file=trim(s))
write(31,'(f0.1)') 1.0
write(31,*) trim(s)
write(31,'(i0)') len_trim(s)
close(31)
return
END subroutine

Regards

Jun 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: achrist | last post by:
I'm aving some trouble getting callbacks from a Delphi DLL back to python working through ctypes. The call from python to the DLL works fine. ...
2
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden,...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file...
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at:...
3
by: Martin P. Hellwig | last post by:
Hey all, I'd like to wrap libpam so that I can use that for authentication and password management. I build ctypes (0.9.9.6) on my platform via...
2
by: Jacob Rael | last post by:
Hello, I was following along with this site: http://www.brunningonline.net/simon/blog/archives/000659.html and I got a error. It boils down...
2
by: Sells, Fred | last post by:
Diez wrote... You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters that the c api uses. ...
0
by: Robert Kern | last post by:
dcharno wrote: Hmm. Okay. Start your program under gdb, find what data it's crashing on, check the alignment on it, and if it's misaligned,...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.