9903

Errors when using a Fortran call back function in f2py

Question:

I am writing a code in python that exploits a Fortran 90 optimization algorithm. The idea is to have the MAIN routine in python that calls the algorithm in Fortran that calls another python function passing some parameters in order to calculate the value of an objective function (that is, it returns a scalar), like this:

[Python) MAIN ⇒ [Fortran] BB_algo ⇒ [Python] FUNCT

Such problem should be solved by using the f2py libraries in python. In order to try to make such method work I have written some simpler code that makes similar operations.

The Fortran subroutine is called writepy:

subroutine writepy(funct,n) implicit none real, EXTERNAL :: funct INTEGER :: I,n REAL*8 :: r !f2py intent(in), n !f2py intent(out), r !f2py real y !f2py y=funct(y) r = 1D0 DO I=1,5 write(*,*) 'funct:', funct(n) r = r + funct(n) ENDDO return end

while the code in Python is the following:

import os os.system("f2py -m wp -c writepy.f90") import wp def funct(n): print(n); return n if __name__ == '__main__': n=3; wp.writepy(funct,n)

Fortran compiles well with gfortran, but when I try to compile it with f2py it seems that there are some errors with the wrapper in C of Fortran, but I do not really know what to do at this point.

I get the following:

running build running config_cc unifing config_cc, config, build_clib, build_ext, build commands --compiler options running config_fc unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options running build_src build_src building extension "wp" sources f2py options: [] f2py:> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 Reading fortran codes... Reading file 'writepy.f90' (format:free) {'after': '(in), n', 'before': '', 'this': 'intent'} Line #12 in writepy.f90:" intent(in), n" analyzeline: no name pattern found in intent statement for ''. Skipping. {'after': '(out), r', 'before': '', 'this': 'intent'} Line #13 in writepy.f90:" intent(out), r" analyzeline: no name pattern found in intent statement for ''. Skipping. Post-processing... Block: wp Block: writepy Post-processing (stage 2)... Building modules... Building module "wp"... Constructing wrapper function "writepy"... sign2map: Confused: external funct is not in lcb_map[]. append_needs: unknown need 'funct' append_needs: unknown need 'funct' writepy(funct,n,[funct_extra_args]) Wrote C/API module "wp" to file "/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c" adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c' to sources. adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5' to include_dirs. copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 build_src: building npy-pkg config files running build_ext customize UnixCCompiler customize UnixCCompiler using build_ext customize Gnu95FCompiler Found executable /usr/bin/gfortran customize Gnu95FCompiler customize Gnu95FCompiler using build_ext building 'wp' extension compiling C sources C compiler: /usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 compile options: '-I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c' clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:2: In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:139:18: warning: comparison of integers of different signs: 'Py_ssize_t' (aka 'long') and 'unsigned long' [-Wsign-compare] if (size < sizeof(notalloc)) { ~~~~ ^ ~~~~~~~~~~~~~~~~ 2 warnings generated. clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18: In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef' funct_typedef funct_cptr; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = funct; ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct' funct_cptr = funct; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs' funct_nofargs_capi = funct_nofargs; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) { ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? (*f2py_func)(funct_cptr,&n); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types] (*f2py_func)(funct_cptr,&n); ^~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign] funct_capi = funct_capi; ~~~~~~~~~~ ^ ~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign] funct_args_capi = funct_args_capi; ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs' funct_nofargs = funct_nofargs_capi; ^ 4 warnings and 8 errors generated. In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18: In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef' funct_typedef funct_cptr; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = funct; ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct' funct_cptr = funct; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs' funct_nofargs_capi = funct_nofargs; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) { ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? (*f2py_func)(funct_cptr,&n); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types] (*f2py_func)(funct_cptr,&n); ^~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign] funct_capi = funct_capi; ~~~~~~~~~~ ^ ~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign] funct_args_capi = funct_args_capi; ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs' funct_nofargs = funct_nofargs_capi; ^ 4 warnings and 8 errors generated. error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1

<strong>Edit</strong>

Thanks to @DavidW's answer the code now works, but if I try to call the fortran function "writepy" from another function, the error of "use of undeclared identifier" is once again there.

the new Fortran code is

subroutine writepy(funct,n,r) implicit none EXTERNAL :: funct REAL :: funct INTEGER :: I,n intent(in):: n REAL*8 :: r intent(out) ::r r = 1D0 DO I=1,5 write(*,*) 'funct:', funct(I) r = r + funct(n) ENDDO return end subroutine call_writepy(funct,n,r) implicit none EXTERNAL :: funct REAL :: funct INTEGER :: n intent(inout):: n REAL*8 :: r intent(inout) ::r call writepy(funct,n,r) return end

it compiles with GFortran, but when I use it with Python I get:

/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized] if (capi_j>capi_i) ^~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here return return_value; ^~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true if (capi_j>capi_i) ^~~~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning float return_value; ^ = 0.0 /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef' funct_typedef funct_cptr; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = funct; ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct' funct_cptr = funct; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs' funct_nofargs_capi = funct_nofargs; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) { ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? (*f2py_func)(funct_cptr,&n,&r); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types] (*f2py_func)(funct_cptr,&n,&r); ^~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign] funct_capi = funct_capi; ~~~~~~~~~~ ^ ~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign] funct_args_capi = funct_args_capi; ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs' funct_nofargs = funct_nofargs_capi; ^ 5 warnings and 8 errors generated. routsign2map: Confused: function call_writepy has externals ['funct'] but no "use" statement. sign2map: Confused: external funct is not in lcb_map[]. append_needs: unknown need 'funct' append_needs: unknown need 'funct' In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:18: In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/fortranobject.h:13: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized] if (capi_j>capi_i) ^~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here return return_value; ^~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true if (capi_j>capi_i) ^~~~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning float return_value; ^ = 0.0 /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef' funct_typedef funct_cptr; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? funct_cptr = funct; ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct' funct_cptr = funct; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs' funct_nofargs_capi = funct_nofargs; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) { ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'? (*f2py_func)(funct_cptr,&n,&r); ^~~~~~~~~~ funct_capi /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here PyObject *funct_capi = Py_None; ^ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types] (*f2py_func)(funct_cptr,&n,&r); ^~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign] funct_capi = funct_capi; ~~~~~~~~~~ ^ ~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign] funct_args_capi = funct_args_capi; ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~ /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs' funct_nofargs = funct_nofargs_capi; ^ 5 warnings and 8 errors generated. error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1 256

Answer1:

There's a number of little issues that make it not work:

subroutine writepy(funct,n,r)

I've added r to the list of parameters. It compiles without this, but otherwise there's no way of getting r back to Python which I think is what you want based on the intent(out)

implicit none EXTERNAL :: funct REAL :: funct

I had to put EXTERNAL and REAL on separate lines. I'm not sure why. This was your main issue.

INTEGER :: I,n REAL*8 :: r intent(in) :: n intent(out) :: r

I've removed the !f2py from the intent lines. You should let Fortran know the intent as well as f2py. (The code still compiles without this change but it makes more sense this way)

I've then deleted your !f2py real y and !f2py y=funct(y) lines. I don't know what they were trying to achieve but I don't think they were valid.

r = 1D0 DO I=1,5 write(*,*) 'funct:', funct(n) r = r + funct(n) ENDDO return end

The rest is unchanged.

Answer2:

The problem is due to the fact that the compiler did not recognize the function "funct" from Python. In order to make the compiler aware of such function and its role, one needs to modify the .pyf file.

The automatically generated .pyf file (with the command f2py writepy.f90 -m wp -h wp.pyf) is the following:

! -*- f90 -*- ! Note: the context of this file is case sensitive. python module __user__routines interface writepy_user_interface function funct(i) ! in :wp:writepy.f90:writepy:unknown_interface integer :: i real :: funct end function funct end interface writepy_user_interface end python module __user__routines python module wp ! in interface ! in :wp subroutine writepy(funct,n,r) ! in :wp:writepy.f90 use writepy__user__routines external funct integer intent(in) :: n real*8 intent(out) :: r end subroutine writepy subroutine call_writepy(funct,n,r,q) ! in :wp:writepy.f90 external funct integer intent(inout) :: n real*8 intent(out) :: r real*8 intent(out) :: q end subroutine call_writepy end interface end python module wp ! This file was auto-generated with f2py (version:2). ! See http://cens.ioc.ee/projects/f2py2e/

It is easy to notice that f2py recognizes “funct” in the “__user__routines” module. However it does not understand that such function is used in both the subroutines “writepy” and “call_writepy”. This is the source of the error. Therefore the user must modify the .pyf file, adding the command “use __user__routines”, in all the subroutines where “funct” is present as an external object:

! -*- f90 -*- ! Note: the context of this file is case sensitive. python module __user__routines interface function funct(i) integer :: i real*8 :: r end function funct end interface end python module __user__routines python module wp ! in interface ! in :wp subroutine writepy(funct,n,r) ! in :wp:writepy.f90 use __user__routines external funct integer intent(in) :: n real*8 intent(out) :: r end subroutine writepy subroutine call_writepy(funct,n,r)! in :wp:writepy.f90 use __user__routines external funct integer intent(inout) :: n real*8 intent(out) :: r real*8 intent(out) :: q end subroutine call_writepy end interface end python module wp ! This file was auto-generated with f2py (version:2). ! See http://cens.ioc.ee/projects/f2py2e/

And use the following command when compiling in f2py the Fortran code: f2py -c wp.pyf writepy.f90 telling to the compiler to explicitly use the edited file “wp.pyf” as a reference

Recommend

  • Time Complexity of finding the length of an array
  • How to convert JSON data to Objects in iPhone [duplicate]
  • ora-00972 identifier is too long oracle 10g
  • Initializing a pthread mutex in shared memory
  • how to overcome building error when I'm using bcp to export boost thread?
  • Wait for angular to finish updating the DOM
  • Redirect to trailng slash (htaccess)
  • How to name a 'group' check box in Adobe Reader when wanting to fill form by FDF / XFDF
  • what is the purpose of “export as namespace foo”?
  • Prevent page break in text block with iText, XMLWorker
  • iText RadioGroup/RadioButtons across multiple PdfPCells
  • python mysqldb delete row
  • Django Haystack Rebuild Index
  • Why are YouTube videos using 'youtube.com/v' not loading
  • C++ friend class std::vector
  • Does Apportable support to build library binary (.a/.so)?
  • How to retrieve information from antrun back to maven?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • SyntaxError: (irb):26: both block arg and actual block given
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Optimizing the print function in Matlab
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • Android Studio 1.3 RC3. Google Play services out of date. Requires 7571000 but found 6774470
  • Security issues with PHP's Readfile method
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • How does this usort cmp function actually work?
  • Unable to install Git-core+svn by MacPorts
  • Stop Bash Script if Hive Fails
  • Django simple Captcha “No module named fields” error
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Meteor helpers not available in Angular template
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Convert array of 8 bytes to signed long in C++
  • using HTMLImports.whenReady not working in chrome
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Net Present Value in Excel for Grouped Recurring CF