How to pass an array of characters from Fortran to Python

106 Views Asked by At

I wanted to write a function in fortran and then call it in another python program. In order to do that, I tried to use f2py or fmodpy, but unfortunately it doesn't work. My (fortran) function takes a positive integer as an input and returns an array of characters. Does anyone know how I could do that?

This is the file containing my function (the file is called my_fortran_lib.f90):


recursive function GenerateCompleteBinaryTreeNew(height) RESULT(treeResult)
!  This function returns the words representing a complete binary tree, producing the labels of its leaves 
!  (which are words composed of the letters {0, 1}). The height of the tree must be at least 1.
  implicit none

  INTEGER, INTENT(IN) :: height
  INTEGER :: i
  INTEGER :: number_entries_temp, number_entries 
  number_entries_temp = (2 ** height - 1) 
  !f2py integer :: number_entries_temp = pow(2,height-1)
  number_entries = number_entries_temp * 2
  CHARACTER(len=20), INTENT(out), DIMENSION(number_entries) :: treeResult
  CHARACTER(len=20), INTENT(inout), DIMENSION(number_entries_temp) :: treeResult_temp
  

  ! Case : height = 0
  IF (height == 0) THEN
    treeResult(1) = ""
  ! Case : height = 1
  else if (height == 1) then
    i = 1
    treeResult(1) = '0'
    treeResult(2) = '1'
  else
  ! Case : height > 1
    treeResult_temp = GenerateCompleteBinaryTreeNew(height-1)

    do i = 1, number_entries_temp
      treeResult(i) = '0' // treeResult_temp(i)
    end do
    do i = 1, number_entries_temp
      treeResult(i+  number_entries_temp) = '1' // treeResult_temp(i)
    end do
  end if
end function GenerateCompleteBinaryTreeNew

I tried with f2py by typing this command in the shell:

f2py3 -c -m myflib my_fortran_lib.f90

Here is the error message that I get


/Users/valeriano/Documents/.venv/lib/python3.9/site-packages/numpy/f2py/f2py2e.py:719: VisibleDeprecationWarning: distutils has been deprecated since NumPy 1.26.xUse the Meson backend instead, or generate wrapperswithout -c and use a custom build script
  builder = build_backend(
running build
running config_cc
INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
INFO: build_src
INFO: building extension "myflib" sources
INFO: f2py options: []
INFO: f2py:> /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.c
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9
Reading fortran codes...
        Reading file 'my_fortran_lib.f90' (format:free)
Post-processing...
        Block: myflib
                        Block: foo
                        Block: generatecompletebinarytreenew
appenddecl: "intent" not implemented.
appenddecl: "dimension" not implemented.
In: :myflib:my_fortran_lib.f90:generatecompletebinarytreenew
analyzevars: prefix ('recursive') were not used
Applying post-processing hooks...
  character_backward_compatibility_hook
Post-processing (stage 2)...
Building modules...
    Building module "myflib"...
    Generating possibly empty wrappers"
    Maybe empty "myflib-f2pywrappers.f"
        Constructing wrapper function "foo"...
          foo()
    Generating possibly empty wrappers"
    Maybe empty "myflib-f2pywrappers.f"
                Creating wrapper for Fortran function "generatecompletebinarytreenew"("generatecompletebinarytreenew")...
        Constructing wrapper function "generatecompletebinarytreenew"...
getstrlength: expected a signature of a string but got: {'typespec': 'character', 'charselector': {'len': '20'}, 'attrspec': [], 'intent': ['out'], 'dimension': ['number_entries']}
          generatecompletebinarytreenew = generatecompletebinarytreenew(height)
    Wrote C/API module "myflib" to file "/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.c"
    Fortran 77 wrappers are saved to "/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflib-f2pywrappers.f"
INFO:   adding '/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/fortranobject.c' to sources.
INFO:   adding '/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9' to include_dirs.
copying /Users/valeriano/Documents/.venv/lib/python3.9/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9
copying /Users/valeriano/Documents/.venv/lib/python3.9/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9
INFO:   adding '/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflib-f2pywrappers.f' to sources.
INFO: build_src: building npy-pkg config files
running build_ext
INFO: customize UnixCCompiler
INFO: customize UnixCCompiler using build_ext
INFO: get_default_fcompiler: matching types: '['gnu95', 'nag', 'nagfor', 'absoft', 'ibm', 'intel', 'gnu', 'g95', 'pg']'
INFO: customize Gnu95FCompiler
INFO: Found executable /usr/local/bin/gfortran
INFO: customize Gnu95FCompiler
INFO: customize Gnu95FCompiler using build_ext
INFO: building 'myflib' extension
INFO: compiling C sources
INFO: C compiler: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -Wno-error=unreachable-code -ftrapping-math

creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a
creating /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9
INFO: compile options: '-DNPY_DISABLE_OPTIMIZATION=1 -I/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9 -I/Users/valeriano/Documents/.venv/lib/python3.9/site-packages/numpy/core/include -I/Users/valeriano/Documents/.venv/include -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/include/python3.9 -c'
INFO: clang: /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.c
INFO: clang: /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/fortranobject.c
/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.c:312:43: error: use of undeclared identifier 'number_entries'
    generatecompletebinarytreenew_Dims[0]=number_entries;
                                          ^
1 error generated.
error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -Wno-error=unreachable-code -ftrapping-math -DNPY_DISABLE_OPTIMIZATION=1 -I/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9 -I/Users/valeriano/Documents/.venv/lib/python3.9/site-packages/numpy/core/include -I/Users/valeriano/Documents/.venv/include -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/include/python3.9 -c /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.c -o /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.o -MMD -MF /var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/var/folders/l9/5sv8cvgs649f75m12cv86vd80000gn/T/tmp7_xnuf2a/src.macosx-10.9-universal2-3.9/myflibmodule.o.d" failed with exit status 1
0

There are 0 best solutions below