Python/Fortran interoperability

glen herrmannsfeldt gah at ugcs.caltech.edu
Mon Aug 24 15:23:53 EDT 2009


In comp.lang.fortran nmm1 at cam.ac.uk wrote:
(snip)
 
< Precisely.  And the kludge does NOT work under all circumstances,
< which is why I said that it doesn't work very well.
 
< Consider, for example:
 
<    SUBROUTINE Fred (X) BIND(C)
<    CHARACTER*(*) :: X
<    END SUBROUTINE Fred
 
<    CHARACTER(LEN=100) :: string
<    CALL Fred(string(40:60))
<    CALL Fred(string(5:50))
 
< This is not currently allowed and raises all sorts of 'interesting'
< implementation and portability questions.  For example, I defy anyone
< to write Fred portably in C :-)

You mean, how does FRED know the length?  It seems to me the
usual question for Fortran assumed size arrays.  Assuming that
FRED can tell from the passed string, it seems fine to me.
If not, it is a problem.  

Null terminated strings are a C convention, supported by
the library and compiler (string constants).  Others are legal C,
though you have to be careful which library routines you use.
 
< It gets really hairy if you have functions that have assumed length
< results, but those are obsolescent.
 
< Even when Fred has an explicit length, there are some problematic
< cases, which could catch out programmers in one language that don't
< know the other fairly well.  But those are much less of a problem
< than the common need for assumed length CHARACTER arguments.

Maybe Fortran programmers who started in Fortran 66 will not
have so much problem with this.  The usual way would be to
pass the length, as with assumed size arrays.  I believe terminating
strings with unusual (likely not null) characters was also done.

-- glen



More information about the Python-list mailing list