Comment #5 on issue 39 by
samg...@googlemail.com: STOP command kills entire
python instance
http://code.google.com/p/f2py/issues/detail?id=39
OK, it works, thanks! It's a bit convoluted but seems to work. For
reference/posterity, my solution was the following (I've changed around
some of the names and simplified my code a bit, but it should work as
written):
1) Replace every "STOP" call with "call long_jump" using a Python script (I
can post this up if you like; I jumped through a few hoops to get rid of
stuff like comments/strings including the characters "stop" or functions
called "stop_stuff", etc.
2) Write these files:
---
clean_stop.c:
#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
void run_fortran_();
void long_jump_()
{
longjmp(buf,1);
}
int set_jump_()
{
if(!setjmp(buf))
{
run_fortran_();
}
}
---
myfortran.f90:
program myfortran
! This is optional; allows you to run it as a command-line program too
call set_jump
end program myfortran
subroutine run
! This is what you call in Python
call set_jump
end subroutine run
subroutine run_fortran
! FORTAN CODE GOES HERE
end subroutine run_fortran
---
3) Compile both as object files in gfortran and gcc
4) Run "f2py -m myfortran -h myfortran.pyf myfortran.f90" (you can delete
the interface call to run_fortran if you like, might make things cleaner
for the users)
5) Run "f2py -lgfortran" -c myfortran.pyf *.o
6) In Python, call "import myfortran; myfortran.run()"
(Note: the output of this program is as files, I assume returning variables
should be a simple thing to hack into the solution above)
I suspect it might have been simpler to embed the C code in Python as the
entry point, but never mind.