【发布时间】:2017-09-14 21:07:15
【问题描述】:
我正在使用 f2py 从 Fortran 模块构建 Python 模块。 Fortran 模块包含不需要在 Python 模块中可用的私有过程。以下是重现该问题的代码示例:
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
当我编译它时:
f2py -c test.f90 -m test
编译失败并显示以下错误消息:
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 »
gfortran 似乎试图在模块之外使用 private 函数,这当然失败了。
删除 public/private 语句解决了问题(通过公开所有功能),但我觉得这不是一个干净的方法。这些函数不一定要在 Python 中使用,也不应该在 Python 环境中可用。 如果不能修改包含此类声明的 Fortran 脚本怎么办?
简而言之:
在 Fortran 中使用 f2py 管理私有过程的简洁方法是什么?
【问题讨论】:
标签: python fortran private python-module f2py