Python/Fortran interoperability

Richard Maine nospam at see.signature
Mon Aug 24 14:04:56 EDT 2009


sturlamolden <sturlamolden at yahoo.no> wrote:

> You also said we can only interop with
> length-1 character strings. My kludge was valid Fortran and works with
> strings of any length up to some sane limit that you can specify.

There might be a confusion here (and I'm not even sure on whose part) on
a picky but important detail of wording. I have seen multiple people
confused by this one before. In fact, some potential confusion was
forseen, which is why there are notes specifically about it in the
Fortran standard (see below). Those notes do tend to get overlooked
though.

Only character strings of length 1 are interoperable, as the term
"interoperable" is defined in the Fortran standard. However, that does
not mean that only character strings of length 1 will work with C. The
distinction might be picky, but it is important.

See Note 15.19 (which in turn cites an example in Note 15.23) of f2003.
You can pass a Fortran string of length n as an actual argument
corrseponding to a dummy argument that is an array of n character*1
elements. This isn't considered a question of "interoperability", as it
is a feature purely within Fortran, but it does impact on what kinds of
things work.

One might plausibly regard this as a kludge, but it is a kludge that is
part of the Fortran standard and is guaranteed to work with all Fortran
compilers. I almost said all f2003-compliant compilers (admittedly a
limited set), but then I recalled that the feature actually dates back
to f77 when character type was introduced; f2003 just extends it to the
C character kind for the obscure case where the C character kind might
be different from the default character kind (I don't know of any
compilers where this is so, but the standard allows for it).

-- 
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



More information about the Python-list mailing list