f90wrap icon indicating copy to clipboard operation
f90wrap copied to clipboard

ImportError on modules with callbacks from fortran to python functions

Open jmp75 opened this issue 6 years ago • 1 comments

First, thank you for f90wrap.

I'd like to get a callback mechanism to capture Fortran messages and display them in a jupyter cell output (stdout is captured by a python console but SFAIK no easy way with jupyter).

I note f90wrap has a 'callback' CLI option but I am not sure how to use it. Say I have in cbfunc.f90 :

module cbfunc
    implicit none
    public
contains

subroutine f1()
    print *, "in f1, calling f2 twice.."
    call f2()
    call f2()
    return
 end
 
subroutine f2()
    !f2py    intent(callback, hide) fpy
    external fpy
    print *, "in f2, calling f2py.."
    call fpy()
    return
end
end module

and using

F90_FILES="cbfunc.f90"
OBJ_FILES="cbfunc.o"
MODULE_NAME=CBF
rm f90wrap_*.f90 *.o
rm _${MODULE_NAME}*
rm ${MODULE_NAME}.py
rm -rf __pycache__
f90wrap -m ${MODULE_NAME} $F90_FILES --callback fpy
gfortran -c $F90_FILES -fPIC
f2py-f90wrap -c -m _${MODULE_NAME} f90wrap_*.f90 $OBJ_FILES

Then in python import CBF fails with ImportError: /xxx/yyy/ff/_CBF.cpython-37m-x86_64-linux-gnu.so: undefined symbol: fpy_

The callback mechanism works if using only f2py i.e. f2py -c -m ${MODULE_NAME} cbfunc.f90 and in python:

import CBF
dir(CBF.cbfunc)
CBF.cbfunc.f2()

def f(): print("python f")

# CBF.cbfunc.fpy = f #nope ?
CBF.fpy = f
CBF.cbfunc.f2()
CBF.cbfunc.f1()

I'll see if I can find a fix or workaround.

jmp75 avatar Nov 01 '19 09:11 jmp75

For the interested reader note this method to capture the standard output if this is what you need/want to do and may be easier to set up than a callback.

jmp75 avatar Nov 05 '19 21:11 jmp75