pythonfortranfortran77f2py

f2py can't determine expression type - how do I fix this?


I'm trying to wrap some Fortran code with f2py and I get the following error:

Reading fortran codes...
    Reading file 'first_fn_prog.f90' (format:fix)
Line #8 in first_fn_prog.f90:"      parameter (a = -1.d+0/2.72d+0)         "
    determineexprtype: could not determine expressions ('-1.d+0/2.72d+0') type.
analyzeline: Failed to evaluate '-1.d+0/2.72d+0'. Ignoring: invalid syntax (<string>, line 1)

The code I'm talking about is here:

  subroutine show(t)    
  real*8 T                            
  write (*,*) 'T = ', t 
  end                           

  subroutine shout() 
  real*8 a                      
  parameter (a = -1.d+0/2.72d+0)        
  write (*,*) 'SHOUT SHOUT!'    
  call show(a)                 

  end

  program circle
  real*8 a, c
  integer b

  common /data/ arr1(19)   

  write (*,*) 'Enter a:'
  read (*,*) a
  write (*,*) 'Enter b'
  read (*,*) b
  c = a + dble(b)

  call shout()

  end

If I change the lines

  real*8 a                      
  parameter (a = -1.d+0/2.72d+0)

to

  real*8 a                      
  a = -1.d+0/2.72d+0

there is no error about the expression mentioned in 'a' var. Besides, I have tested both modules constructed with the error and without it and it seems that in both cases values calculated in 'a' var are the same. So I assume this error doesn't influence anything, but still I need to fix this.

Also if I replace 1.d+0/2.72d+0 with a simple value, say:

  real*8 a                      
  parameter (a = 3)

f2py also shows no errors about that.

What can I do with this?

The full f2py wrapping log is presented below:

f2py -c first_fn_prog.f90 -m ft
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 "ft" sources
f2py options: []
f2py:> /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c
creating /tmp/tmp3axmu237/src.linux-x86_64-3.5
Reading fortran codes...
    Reading file 'first_fn_prog.f90' (format:fix)
Line #8 in first_fn_prog.f90:"      parameter (a = -1.d+0/2.72d+0)         "
    determineexprtype: could not determine expressions ('-1.d+0/2.72d+0') type.
analyzeline: Failed to evaluate '-1.d+0/2.72d+0'. Ignoring: invalid syntax (<string>, line 1)
Post-processing...
    Block: ft
            Block: show
            Block: shout
            Block: circle
Post-processing (stage 2)...
Building modules...
    Building module "ft"...
        Constructing wrapper function "show"...
          show(t)
        Constructing wrapper function "shout"...
          shout()
        Constructing COMMON block support for "data"...
          arr1
    Wrote C/API module "ft" to file "/tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c"
    Fortran 77 wrappers are saved to "/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f"
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c' to sources.
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5' to include_dirs.
copying /home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmp3axmu237/src.linux-x86_64-3.5
copying /home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmp3axmu237/src.linux-x86_64-3.5
  adding '/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f' to sources.
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
get_default_fcompiler: matching types: '['gnu95', 'intel', 'lahey', 'pg', 'absoft', 'nag', 'vast', 'compaq', 'intele', 'intelem', 'gnu', 'g95', 'pathf95', 'nagfor']'
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'ft' extension
compiling C sources
C compiler: x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC

creating /tmp/tmp3axmu237/tmp
creating /tmp/tmp3axmu237/tmp/tmp3axmu237
creating /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5
compile options: '-I/tmp/tmp3axmu237/src.linux-x86_64-3.5 -I/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include -I/usr/include/python3.5m -c'
x86_64-linux-gnu-gcc: /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c
x86_64-linux-gnu-gcc: /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c
In file included from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:0,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.h:13,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.c:15:
/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
In file included from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:0,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.h:13,
                 from /tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.c:2:
/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
compiling Fortran sources
Fortran f77 compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmp3axmu237/src.linux-x86_64-3.5 -I/home/veniamin/.local/lib/python3.5/site-packages/numpy/core/include -I/usr/include/python3.5m -c'
gfortran:fix: first_fn_prog.f90
gfortran:f77: /tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.f
/usr/bin/gfortran -Wall -g -Wall -g -shared /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/ftmodule.o /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/fortranobject.o /tmp/tmp3axmu237/first_fn_prog.o /tmp/tmp3axmu237/tmp/tmp3axmu237/src.linux-x86_64-3.5/ft-f2pywrappers.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5 -lgfortran -o ./ft.cpython-35m-x86_64-linux-gnu.so
Removing build directory /tmp/tmp3axmu237

SOLVED

The easiest way to avoid the error above is just to declare a variable and assign a value to it without the "parameter" statement.


Solution

  • The best thing I could decide with my poor knowledge of Fortran is just to avoid a 'parameter' statement and do as follow:

      real*8 a                      
      a = -1.d+0/2.72d+0
    

    Not sure whether it is crucial for Fortran to declare constants in that way, but this will do for my purpose I guess.

    UPDATE

    The following construction

    REAL*8, PARAMETER :: A=-1.d+0/2.72d+0
    

    is also working even while compiling files with Fortran77 compiler. Thanks to francescalus.