Python/Fortran interoperability

sturlamolden sturlamolden at
Mon Aug 24 15:46:02 CEST 2009

On 24 Aug, 10:24, n... at wrote:
> In article <5134d9f1-0e23-4e05-a817-bf0cc9e85... at>,
> sturlamolden  <sturlamol... at> wrote:
> >On 24 Aug, 02:26, nos... at see.signature (Richard Maine) wrote:
> >> You missed the word "OOP", which seemed like the whole point. Not that
> >> the particular word is used in the Fortran standard, but it isn't hard
> >> to guess that he means a derived type that uses some of the OOP
> >> features. Inheritance, polymorphism, and type-bound procedure (aka
> >> methods in some other languages) come to mind.
> >But C is not OOP. The ISO C bindings in Fortran are not ISO C++
> >bindings. This is for a reason: C++ does not have a standard ABI like
> >ISO C.
> Nor does C.  Almost everything that most people believe about C is
> wrong, because C is not well-defined at any level, so there are
> many twisty little C languages, all different.
> Richard is perfectly correct that my point was OOP.  C interoperability
> does not apply to any derived type with type-bound procedures, which
> include finalizers.  Note that this ALSO forbids them from being
> passed as data, even if the other language never uses the OOP features
> of the type.

You also made this claim regarding Fortran's C interop with strings:

"No, I mean things like 'Kilroy was here'.  Currently, Fortran's C
interoperability supports only strings of length 1, and you have
to kludge them up as arrays.  That doesn't work very well, especially
for things like function results."

This obviosuly proves you wrong:

subroutine foobar(fstr)
    ! this is the Fortran function we want to call from C
    ! it takes a variable length string as argument and
    ! print its length
    character(*) :: fstr
    write (*,*) len(fstr)
end subroutine

subroutine wrap_foobar(cstr) bind(c, name='foobar')
    ! a wrapper for foobar which we expose to C
    use, intrinsic :: iso_c_binding

        function strlen(cstr) bind(c, name='strlen')
           use, intrinsic :: iso_c_binding
           integer(c_int) :: strlen
           type(c_ptr), value :: cstr
        end function strlen
    end interface

    type(c_ptr), value :: cstr
    character(2147483647), pointer :: p_fstr
    integer :: n

    n = strlen(cstr)
    call c_f_pointer(cstr, p_fstr)
    call foobar(p_fstr(1:n))

end subroutine

I am not saying you are wrong regarding OOP derives types, but I'm not
taking your word for it. However, I am not wasting my time on Fortran
2003 OOP now, as it lacks compiler support. So I'll leave your claim

By the way, I am more than happy using Python for OOP and Fortran 95
for speed. As for numerical computing, I'd rather see better support
for array and matrix types in Cython than OOP in Fortran.

Sturla Molden

More information about the Python-list mailing list