gpt4 book ai didi

python - 使用 f2py 在 (Fortran) 模块错误中停止 python 代码?

转载 作者:太空狗 更新时间:2023-10-29 21:53:14 27 4
gpt4 key购买 nike

我正在使用 f2py 在 Fortran 中创建一个 Python 模块。如果在 Fortran 模块中遇到错误,我想在 Python 程序中产生错误(包括错误消息)。考虑以下示例:

Fortran 代码(test.f):

subroutine foo(a,m)

integer :: m,i
integer, dimension(m) :: a
!f2py intent(in) :: m
!f2py intent(in,out) :: a
!f2py intent(hide), depend(a) :: m=shape(a)

do i = 1,m
if ( a(i) .eq. 0 ) then
print*, 'ERROR HERE..?'
end if
a(i) = a(i)+1
end do

end subroutine

这个非常简单的程序将 1 添加到 a 的每个元素。但如果 a(i) 等于零,则应该产生错误。附带的 Python 代码:

import test

print test.foo(np.array([1,2],dtype='uint32'))
print test.foo(np.array([0,2],dtype='uint32'))

现在的输出是:

[2 3]
ERROR HERE..?
[1 3]

但是我希望 Python 程序保留错误。请帮忙。

回答

Fortran 中的stop 命令就是这样做的。考虑更新后的 Fortran 代码:

subroutine foo(a,m)

integer :: m,i
integer, dimension(m) :: a
!f2py intent(in) :: m
!f2py intent(in,out) :: a
!f2py intent(hide), depend(a) :: m=shape(a)

do i = 1,m
if ( a(i) .eq. 0 ) then
print*, 'Error from Fortran'
stop
end if
a(i) = a(i)+1
end do

end subroutine

现在的输出是:

[2 3]
Error from Fortran

即错误后 Python 代码不会继续。

最佳答案

我已经向 numpy 社区建议添加一个额外的 f2py“增强”(raise_python_exception),这使得在 Fortran 中定义一个字符串变量成为可能,如果一旦函数返回,非空将导致 Python 引发异常。

所以在 Fortran 中你会这样写:

subroutine calc_dq(q, temp, dq, error_mesg)
!f2py raise_python_exception error_mesg

real, intent(in) :: q, temp
real, intent(out) :: dq

character(len=100), intent(out) :: error_mesg

if (.not. init_called()) then
error_mesg = "`init` hasn't been called."
else
call q_flux_function(q, temp, dq)
endif
end subroutine calc_dq

并从 Python 中调用 error_mesg 变量的内容作为异常的内容:

In [2]: calc_dq(1.0, 300.)
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-8-c0ce0cb9cda1> in <module>()
----> 1 calc_dq(1.0, 300.)

Exception: `init` hasn't been called.

我认为这是从 Fortran 引发异常的一种非常方便的方法,因为它允许轻松定义异常消息。我把我的suggestion on github .

关于python - 使用 f2py 在 (Fortran) 模块错误中停止 python 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18021081/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com