F2py cannot compile module with private functions

I am creating a Python module from Fortran module using f2py . The Fortran module contains private procedures that should not be available in a Python module. Here's some sample code that reproduces the problem:

module testmodule

  implicit none

  public :: &
       test_sub

  private :: &
       useful_func

contains

  subroutine test_sub()

    !uses the function
    print*, useful_func(3)

  end subroutine test_sub

  function useful_func(in) result(res)
    integer, intent(in) :: in
    integer :: res

    !Does some calculation
    res=in+1

  end function useful_func

end module testmodule

      

When I compile it with

f2py -c test.f90 -m test

      

Compilation error with the following error message:

gfortran:f90: /tmp/tmpXzt_hf/src.linux-x86_64-2.7/test-f2pywrappers2.f90
/tmp/tmpXzt_hf/src.linux-x86_64-2.7/test-f2pywrappers2.f90:7:28:

       use testmodule, only : useful_func
                            1
Error: Symbol ยซ useful_func ยป referenced at (1) not found in module ยซ testmodule ยป

      

It looks like gfortran is trying to use a private function outside of the module, which of course fails.

Removing the public / private statement solves the problem (making all functions public), but I feel this is not a clean way to do it. These functions do not need to be used in Python and do not need to be available in the Python environment. What if you cannot modify a Fortran script that contains such a declaration?

In short:

What is the cleanest way to manage private procedures in Fortran using f2py?

+3


source to share


1 answer


f2py has heuristics to determine what to include in a compiled module. You can make it specific using the "only" option, as in

f2py -c -m ff ff.f90 only: test_sub

      



Typing f2py without an option gives you a list of useful functions. Depending on your needs, you can use the iso_c_binding function for Fortran (2003 and up).

+2


source







All Articles