(1) 处的外部函数“f”在 f2py 的子例程中没有隐式类型

Joh*_*sty 2 python fortran external callback f2py

我已经使用 Fortran 90 一段时间了,最​​近决定使用 f2py 封装一些模块,以使用 Python 作为前端来简化原型设计。

\n\n

但是,在尝试编译传递外部函数(来自 Python)的子例程时,我遇到了错误。这是复制错误的代码:

\n\n
! file: callback.f90\nsubroutine callback(f,x,y)\n  implicit none\n  ! - args -\n  ! in\n  external :: f\n  real(kind=kind(0.0d0)), intent(in) :: x\n  ! out\n  real(kind=kind(0.0d0)), intent(inout) :: y\n\n  y = f(x)\nend subroutine\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,如果我使用 f2py 生成签名文件 (.pyf),我将获得以下结果:

\n\n
!    -*- f90 -*- \n! Note: the context of this file is case sensitive.\npython module callback__user__routines \ninterface callback_user_interface \n    function f(x) result (y) ! in :callback:callback.f90\n        intent(inout) f\n        real(kind=kind(0.0d0)) intent(in) :: x\n        real(kind=kind(0.0d0)) intent(inout) :: y\n    end function f\nend interface callback_user_interface\nend python module callback__user__routines\npython module callback ! in  \n    interface  ! in :callback\n        subroutine callback(f,x,y) ! in :callback:callback.f90\n            use callback__user__routines\n            external f\n            real(kind=kind(0.0d0)) intent(in) :: x\n            real(kind=kind(0.0d0)) intent(inout) :: y\n        end subroutine callback\n    end interface \nend python module callback\n\n! This file was auto-generated with f2py (version:2).\n! See http://cens.ioc.ee/projects/f2py2e/\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是不正确的,所以我按以下方式修改它:

\n\n
!    -*- f90 -*- \n! Note: the context of this file is case sensitive.\npython module __user__routines\n    interface\n        function f(x) result (y) \n            real(kind=kind(0.0d0)) intent(in) :: x\n            real(kind=kind(0.0d0)) :: y\n        end function f\n    end interface\nend python module __user__routines\npython module callback ! in  \n    interface  ! in :callback\n        subroutine callback(f,x,y)\n            use __user__routines\n            external f\n            real(kind=kind(0.0d0)) intent(in) :: x\n            real(kind=kind(0.0d0)) intent(inout) :: y\n        end subroutine callback\n    end interface \nend python module callback\n\n! This file was auto-generated with f2py (version:2).\n! See http://cens.ioc.ee/projects/f2py2e/\n
Run Code Online (Sandbox Code Playgroud)\n\n

尽管如此,两者都会抛出一个错误,告诉我我还没有定义 f:

\n\n
callback.f90:1:21:\nsubroutine callback(f,x,y)\n                    1\nError: Symbol \xe2\x80\x98f\xe2\x80\x99 at (1) has no IMPLICIT type\ncallback.f90:10:6:\n\n  y = f(x)\n      1\nError: Can't convert UNKNOWN to REAL(8) at (1)\ncallback.f90:1:21:\n\nsubroutine callback(f,x,y)\n                    1\nError: Symbol \xe2\x80\x98f\xe2\x80\x99 at (1) has no IMPLICIT type\ncallback.f90:10:6:\n\n   y = f(x)\n       1\nError: Can't convert UNKNOWN to REAL(8) at (1)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经做了尽可能多的研究来手动正确编辑 .pyf 签名文件,但在这种情况下无济于事。有没有人在尝试将 f2py 与外部函数一起使用时遇到类似的错误?

\n

Vla*_*r F 5

错误是正确的,是一个Fortran错误,即使没有任何f2py或Python也会发生

subroutine callback(f,x,y)
  implicit none
  external :: f
  real(kind=kind(0.0d0)), intent(in) :: x
  real(kind=kind(0.0d0)), intent(inout) :: y

  y = f(x)
end subroutine
Run Code Online (Sandbox Code Playgroud)

f没有声明类型并且implicit none已启用,因此这是一个 Fortran 错误。

使用

  real(kind=kind(0.0d0)), external :: f
Run Code Online (Sandbox Code Playgroud)

或者

  external :: f
  real(kind=kind(0.0d0)) :: f
Run Code Online (Sandbox Code Playgroud)