[f2py] "natural" Fortran calling conventions?

Thomas Breuel tmbdev at gmail.com
Wed Oct 14 22:37:38 EEST 2009


> You don't have to write a whole wrapper to do this, you can use the
> commenting method:
>
> subroutine f2py_trsps_f(n,m,l,h)
> !f2py intent(c) h
>  implicit none
>  integer :: n,m,l
>  real*8  :: h(n,m,l)
>  print*,h(1:8,1,1)
>  return
> end subroutine f2py_trsps_f

OK, I tried this and it isn't working for me.  intent(c) seems to
instruct Python to pass the data in C order, but Fortran doesn't seem
to know that, so the elements end up in the wrong order.

Furthermore, f2py seems to generate some kind of array bounds checking
code so that I can't interchange the array bounds in the Fortran code.
 intent(cache) also isn't doing the trick.

Even if they did, the meaning of intent(c) and intent(cache) when used
in this way could change any release (and likely will once f2py
supports new Fortran arrays).

Altogether, I still think f2py should get an "intent(natural)"; with
that, a C array declared as:

    float a[17][99][3];

would get passed to Fortran as:

    subroutine f(a)
    !f2py intent(natural) a
    real, intent(in) :: a(3,99,17)
    ...
    end subroutine f

This would let me write natural, efficient Fortran loops that
correspond to the natural, efficient C loops, and it would mean that
there is no cost in calling from Python to Fortran and back.

Tom



More information about the f2py-users mailing list