0

f2py gives an error when I try compile FORTRAN subroutine, from which I call another FORTRAN function passing array to it. I simplified code to leave the problem only.

SUBROUTINE MAS (matrix, a)
    IMPLICIT NONE
    INTERFACE
        LOGICAL FUNCTION LTRY(input_array)
            IMPLICIT NONE
            INTEGER*4 :: input_array(:,:)
        END FUNCTION LTRY
    END INTERFACE

    INTEGER*4 ::  matrix (:,:)
!f2py  INTENT(INOUT) :: matrix(:,:)
    INTEGER*4 a
!f2py INTENT(INOUT) :: a

    a = 1
    IF ( LTRY (matrix)) a = 2
END SUBROUTINE

LOGICAL FUNCTION LTRY(input_array)
    IMPLICIT NONE
    INTEGER*4 :: input_array (:,:)

    IF ( ANY(input_array == 0)) LTRY = .FALSE.
END FUNCTION LTRY

After I try to compile this using f2py -c -m ptest ptest.f90 I get a tremendous number of errors. What's wrong? Using Ubuntu and gfortran compiler.

Here is compiler output (part with error reports):

gfortran:f77: /tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:16.35:
              function ltry(input) ! in :ptest:ptest.f90:mas:unkno
                                   1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:18.72:
                  integer, dimension(:,:) :: input
                                                                        1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:19.72:
                  logical :: ltry
                                                                        1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:20.17:
              end function ltry
                 1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:40.32:
      ltryf2pywrap = .not.(.not.ltry(input_array))
                                1
Error: Operand of .not. operator at (1) is INTEGER(4)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:16.35:
              function ltry(input) ! in :ptest:ptest.f90:mas:unkno
                                   1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:18.72:
                  integer, dimension(:,:) :: input
                                                                        1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:19.72:
                  logical :: ltry
                                                                        1
Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:20.17:
              end function ltry
                 1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f:40.32:
      ltryf2pywrap = .not.(.not.ltry(input_array))
                                1
Error: Operand of .not. operator at (1) is INTEGER(4)
error: Command "/usr/bin/gfortran -Wall -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmpSZHMCR/src.linux-i686-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c -c /tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.f -o /tmp/tmpSZHMCR/tmp/tmpSZHMCR/src.linux-i686-2.7/ptest-f2pywrappers.o" failed with exit status 1
Compilation failed.

Some errors look quite strange. For example compiler considers logical function as an integer.

Alexey_AM
  • 31
  • 5

1 Answers1

0

Unfortunately, you have not indicated which type of error you get. I have tried to compile it on my system, and I do get error messages:

/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:16:35:

               function ltry(input) ! in :ptest:ptest.f90:mas:unkno
                                   1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:18:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:19:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:20:17:

               end function ltry
                 1
Error: Expecting END INTERFACE statement at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:16:35:

               function ltry(input) ! in :ptest:ptest.f90:mas:unkno
                                   1
Error: Unexpected junk after function declaration at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:18:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:19:72: Error: Unexpected data declaration statement in INTERFACE block at (1)
/tmp/tmpLKy0jV/src.linux-x86_64-2.7/ptest-f2pywrappers.f:20:17:

               end function ltry
                 1
Error: Expecting END INTERFACE statement at (1)

Looking into the wrapped file, we see that the wrapper has made a very dubious decision: It has added comments at the end of the line, but then line-wrapped the comment without declaring it a comment in the second line:

C     -*- fortran -*-
C     This file is autogenerated with f2py (version:2)
C     It contains Fortran 77 wrappers to fortran functions.

      subroutine f2pywrapmas (matrix, a, f2py_matrix_d0, f2py_matr
     &ix_d1)
      integer*4 a
      integer f2py_matrix_d0
      integer f2py_matrix_d1
      integer*4 matrix(f2py_matrix_d0,f2py_matrix_d1)
      interface
      subroutine mas(matrix,a) 
          integer*4, dimension(:,:) :: matrix
          integer*4 :: a
          interface  ! in :ptest:ptest.f90:mas
              function ltry(input) ! in :ptest:ptest.f90:mas:unkno
     &wn_interface
                  integer, dimension(:,:) :: input
                  logical :: ltry
              end function ltry
          end interface 
      end subroutine mas
      end interface
      call mas(matrix, a)
      end

I don't know how to help you, though. But I do suggest you post your actual error messages in the question.

chw21
  • 7,970
  • 1
  • 16
  • 31