jameskermode / f90wrap

F90 to Python interface generator with derived type support
GNU Lesser General Public License v3.0
244 stars 83 forks source link

Method overloading via interfaces and type mistmatch #32

Open csullivan opened 9 years ago

csullivan commented 9 years ago

First off, allow me to compliment you on this nice python library and f2py extension. After some work, I got it working for my library, but I did run into a few issues:

Interfaced functions of a f90 module are translated to static methods in the auto generate python class, however in the function list for the overloaded call loop, the function names are not prefixed by Class_name, i.e. [Class_name.method1(a,b), Class_name.method1(a,b,c), ... ]

Another issue I found was that asterisks inserted after a type are not handled correctly, and instead the parameter containing the asterisk is ignored completely. For example,

subroutine foo(a,b) 
    real*8, intent(in) :: a
    integer :: b
    print *, a*b
end subroutine foo

is parsed by f90wrap as

subroutine f90wrap_foo(b)
    integer :: b
    ....
end subroutine

If I instead use real(kind=8), f90wrap will include the parameter, but will translate it to the wrong type. In the f90wrap_ file it will be listed as real(4).

libAtoms commented 9 years ago

Thanks for reporting, I will have a look at the asterisk parsing.

For the real(kind=8) case, note that you need to supply a file containing the mapping from kinds to C types as no attempt is made to interpret the kind strings (I admit that this is silly when they are simply numbers like kind=8, I'll try to fix this soon too...). Here's an example kind_map for your case:

$ cat kind_map
{
 'real':     {'': 'float',
              '8': 'double'}
}
$ f90wrap -k kind_map test.f90

which results in the correct wrapper:

subroutine f90wrap_foo(a, b)
    implicit none
    external foo

    real(8), intent(in) :: a
    integer :: b
    call foo(a, b)
end subroutine f90wrap_foo
jameskermode commented 9 years ago

(Oops, signed in as wrong GitHub user, previous comment was by me as well)

jameskermode commented 9 years ago

Interfaced functions of a f90 module are translated to static methods in the auto generate python class, however in the function list for the overloaded call loop, the function names are not prefixed by Class_name, i.e. [Class_name.method1(a,b), Class_name.method1(a,b,c), ... ]

Understood. Do you have a simple test case I could use to speed up debugging this issue?

csullivan commented 8 years ago

Hi, sure no problem.

example.F90

!-*-f90-*-                                                                                                                                                                             
module class_example

  implicit none
  private
  public :: Example, return_example

  interface return_example
     module procedure return_example_first, return_example_second, return_example_third
  end interface return_example

  type Example
     integer :: first
     integer :: second
     integer :: third
  end type Example

  ! singleton                                                                                                                                                                          
  type(Example) :: this 

contains

!------------------------------------------------------------------------------------!                                                                                                 

  function return_example_first(first) result(instance)

    implicit none
    integer :: first
    type(Example) :: instance

    this%first = first
    instance = this

    return

  end function return_example_first

!------------------------------------------------------------------------------------!        

  function return_example_second(first,second) result(instance)                                                                                                                        

    implicit none
    integer :: first
    integer :: second
    type(Example) :: instance

    this%first = first
    this%second = second

    instance = this
    return

  end function return_example_second

!------------------------------------------------------------------------------------!                                                                                                 

  function return_example_third(first,second,third) result(instance)

    implicit none
    integer :: first
    integer :: second
    integer :: third
    type(Example) :: instance

    this%first = first
    this%second = second
    this%third = third

    instance = this
    return

  end function return_example_third

!------------------------------------------------------------------------------------!                                                                                                 
end module class_example

Then f90wrap -m example ./example.F90 produces example.py, which contains the following loop over interfaced member functions (which should be prefixed with the class name since they are static, or not be static):

example.py (abridged)

...
    @staticmethod
    def _return_example_third(first, second, third):
        ...

    @staticmethod
    def return_example(*args, **kwargs):
        """                                                                                                                                                                            
        return_example(*args, **kwargs)                                                                                                                                                

        Defined at ./example.F90 lines 8-10                                                                                                                                            

        Overloaded interface containing the following procedures:                                                                                                                      
          _return_example_first                                                                                                                                                        
          _return_example_second                                                                                                                                                       
          _return_example_third                                                                                                                                                        

        """
        for proc in [_return_example_first, _return_example_second, \
            _return_example_third]:
            try:
                return proc(*args, **kwargs)
            except TypeError:
                continue                                                                                                                                                               

Manually changing the above list to,

for proc in [Class_Example._return_example_first, \
            Class_Example._return_example_second, \
            Class_Example._return_example_third]:

Fixes the python runtime error (NameError: global function _returnexample* is not defined).

jameskermode commented 8 years ago

Interface generation should now be fixed. I didn't look at the asterisk parsing yet.