考虑以下在 test.f 中定义的 Fortran 子例程:
subroutine test(py_func)
use iso_fortran_env, only stdout => output_unit
external py_func
integer :: a
integer :: b
a = 12
write(stdout, *) a
b = py_func(a)
write(stdout, *) b
end subroutine
还有以下 Python 代码,在 call_test.py 中定义:
import test
def func(x):
return x * 2
test.test(func)
使用以下(英特尔编译器)编译:
python f2py.py -c test.f --fcompiler=intelvem -m test
当我运行测试时,我希望这是输出:
12
24
但我实际上得到了这个:
12
0
似乎b
正在使用默认值而不是test
. 我尝试在 Fortran 中使用以下内容:
!f2py intent(callback) py_func
external py_func
!f2py integer y,x
!f2py y = py_func(x)
12
但是我的程序在打印到控制台后崩溃了。
有什么想法可以在这里发生吗?崩溃的原因将是一个奖励,但我真的只是想在这一点上让一个简单的回调工作。