I am creating a Python module in Fortran using f2py . I want to get an error (including an error message) in a Python program if an error is detected in the Fortran module. Consider the following example:
Fortran Code (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
This simple program adds 1 to each element of a . But an error should occur if a(i) is zero. The accompanying Python code:
import test print test.foo(np.array([1,2],dtype='uint32')) print test.foo(np.array([0,2],dtype='uint32'))
Now the conclusion:
[2 3] ERROR HERE..? [1 3]
But I want the Python program to keep the error . Please, help.
Answer
The stop command in Fortran does just that. Consider the updated Fortran code:
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
Now the conclusion:
[2 3] Error from Fortran
those. Python code will not continue after an error.
python fortran f2py
Tom de geus
source share