fortranfortran90fortran77f2py

f2py raises 'Assertion error' while interfacing Fortran77 file with exclamation mark comments


I was tasked to wrap a Fortran 77 code into python3. I tried to wrap it with the following command

f2py -c mum1_6.f -m mum

f2py raised the following error

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 "mum" sources
f2py options: []
f2py:> /tmp/tmp79t4vbg0/src.linux-x86_64-3.5/mummodule.c
creating /tmp/tmp79t4vbg0/src.linux-x86_64-3.5
Reading fortran codes...
    Reading file 'mum1_6.f' (format:fix,strict)
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
rmbadname1: Replacing "len" with "len_bn".
Post-processing...
    Block: mum
            Block: init_mu
            Block: prinfo
            Block: med_cons
            Block: spl1
            Block: dspl1
            Block: spl2
Traceback (most recent call last):
  File "/home/veniamin/.local/bin/f2py", line 11, in <module>
    sys.exit(main())
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/f2py2e.py", line 683, in main
    run_compile()
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/f2py2e.py", line 650, in run_compile
    setup(ext_modules=[ext])
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/core.py", line 171, in setup
    return old_setup(**new_attr)
  File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/command/build.py", line 47, in run
    old_build.run(self)
  File "/usr/lib/python3.5/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/command/build_src.py", line 148, in run
    self.build_sources()
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/command/build_src.py", line 165, in build_sources
    self.build_extension_sources(ext)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/command/build_src.py", line 325, in build_extension_sources
    sources = self.f2py_sources(sources, ext)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/distutils/command/build_src.py", line 562, in f2py_sources
    ['-m', ext_name]+f_sources)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/f2py2e.py", line 425, in run_main
    postlist = callcrackfortran(files, options)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/f2py2e.py", line 329, in callcrackfortran
    postlist = crackfortran.crackfortran(files)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 3254, in crackfortran
    postlist = postcrack(grouplist[0])
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 1891, in postcrack
    g = postcrack(g, tab=tab + '\t')
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 1910, in postcrack
    block['body'] = analyzebody(block, args, tab=tab)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 2072, in analyzebody
    b = postcrack(b, as_, tab=tab + '\t')
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 1910, in postcrack
    block['body'] = analyzebody(block, args, tab=tab)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 2072, in analyzebody
    b = postcrack(b, as_, tab=tab + '\t')
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 1906, in postcrack
    block['vars'] = analyzevars(block)
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 2653, in analyzevars
    for d in rmbadname([x.strip() for x in markoutercomma(dim).split('@,@')]):
  File "/home/veniamin/.local/lib/python3.5/site-packages/numpy/f2py/crackfortran.py", line 833, in markoutercomma
    assert not f, repr((f, line, l))
AssertionError: (-1, '9) !---> array which contains dimensions for output ', '9) !---> array which contains dimensions for output ')

The corresponding part of the code:

      DIMENSION NXG(9),NYG(9),X0G(9),SXG(9),Y0G(9),SYG(9) ! -> arrays which
c  contain values for above referenced initial values, number of values and
c  steps (see above commons /sok_2_1/, /sok_2_2/, /sok_2_3/ etc.).
      DIMENSION IJ(9) !---> array which contains dimensions for output 1-di-
c  mentional arrays with splains. Dimension of output array should be equal
c  to (I+2)*(J+2), where I and J are dimensions for corresponding input 2-di-
c  mensional array. 

The thing I don't understand is what's the problem with the '!' comments? If I remove them, the error is not raised. Also there are a lot of lines with the same '!' comments before this part of the code. Actually, the file I'm wrapping is enourmous, and such comments are almost on every line. I have no idea why f2py have problems only with a part of them.

I tried to think about all of these and jumped to a conclusion that these comments actually are not the part of Fortran 77 standard and that might be the case. So I decided to change an extension to '.f90' and run f2py again. The problem seemed to be solved. As I read on several sites, Fortran 77 code is a subset of Fortran 90, so I was sure that everything is ok. But some time later I tried to compile some Fortran 77 code (not with f2py, but using the compiler directly) as Fortran 90 code and I found that this isn't that simple. At least,old-fashioned 'c' comments are not accepted by F90 compiler in that way.

So I have two questions:

1) Is it possible to somehow solve these problems with '!' inline comments without compiling the file as f90?

2) Is it even correct to feed my '.f' file to f2py as '.f90'? Does f2py anyhow separates different 'code standards' in the same file and does the wrapping in the right way?

Unfortunately, the code I'm talking about is very big to attach it here in the code snippet.


Solution

  • So, I didn't have any luck in interfacing the program with f2py as F77. I tried to feed it to f2py as F90 files and it worked fine for me, because Fortran 90 compilers understand the fixed form notation.