[Numpy-discussion] Recovering from a STOP ?

Pierre GM pgmdevlist@gmail....
Wed Mar 14 08:28:38 CDT 2012


THYC


Dear all,

I'm working with some large inherited F90 code that needs to be wrapped in
Python. if the code base itself cannot be modified (it's a static archive), some
additional F90 files were written to help the interaction with the code. Writing
a python extension combining the archive and these additional files is rather
straightforward using F2PY, the extension loads, all is well but...
...when a problem occurs in a subroutine deep in the base code, it calls another
routine (say, ENDRUN) which itself uses a STOP statement. As you know, this
statement not only exits the fortran code, but makes the extension and then the
interpreter crash...

I patched ENDRUN use STOP, I tried to call a C external function instead of
using STOP:
"""
*- ENDRUN.f90 -*
subroutine ENDRUN(C_NOM)
  use endrun_wrap
  Implicit none
  character(LEN=*), intent(in) :: C_NOM
  ! write(*, *) C_NOM
  ! stop
  call pyraise_runtime(C_NOM)
end subroutine ENDRUN

*- ENDRUNWRAP.f90 -*
module endrun_wrap
  interface
    subroutine pyraise_runtime(message)
        character(LEN=*), intent(in) :: message
    end subroutine pyraise_runtime
  end interface
end module endrun_wrap

*- pyraise_runtime.c -*
#include <Python.h>

void pyraise_runtime_(char *message);

void pyraise_runtime_(char *message)
{
   printf("calling PyErr_SetString(PyExc_RuntimeError, message)\n");
   PyErr_SetString(PyExc_RuntimeError, message);
}
"""

Alas, the RuntimeError doesn't look like it's passed back to the interpreter,
which still crashes. (Adding a Py_Exit(-1) at the end of pyraise_runtime at
least let the interpreter do some extra cleaning after the fortran code stopped,
but still...)

Note that ENDRUN is never supposed to be called directly by the user (so no
point to define a callback function via f2py, right ?).

Any idea would be quite welcome.
Thanks an awful lot in advance.

P.
PS: Oh, BTW, I'm limited to numpy 1.4.1 and the f2py that comes with it... 










More information about the NumPy-Discussion mailing list