Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError on modules with callbacks from fortran to python functions #93

Closed
jmp75 opened this issue Nov 1, 2019 · 1 comment
Closed

Comments

@jmp75
Copy link
Contributor

jmp75 commented Nov 1, 2019

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 jmp75 changed the title Cannot get callbacks to work ImportError on modules with callbacks from fortran to python functions Nov 1, 2019
@jmp75
Copy link
Contributor Author

jmp75 commented Nov 5, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant