Version 10 (modified by pearu, 6 years ago)

--

G3 F2PY - the Third Generation of F2PY

The G3 F2PY is a complete rewrite of f2py2e and its main aim is to support wrapping Fortran 90 derived types to Python.

Status

  • Fortran 77/90/2003 parser - completed
  • Support for scalar arguments - completed
  • Support for array arguments - work in progress

Fortran 77/90/2003 parser example

>>> from numpy.f2py.lib.parser.api import parse
>>> code = '''c comment
...       subroutine foo(a)
...       integer a
...       print*, "a=", a
...       end
... '''
>>> tree = parse(code, isfree=False)
>>> print tree
      !BEGINSOURCE <cStringIO.StringI object at 0xe8b2d0> mode=fix77
        SUBROUTINE foo(a)
          INTEGER a
          PRINT *, "a=", a
        END SUBROUTINE foo
>>> tree

      BeginSource
        blocktype='beginsource'
        name='<cStringIO.StringI object at 0xe8b2d0> mode=fix77'
        a=AttributeHolder:
      external_subprogram=<dict with keys ['foo']>
        content:
          Subroutine
            args=['a']
            item=Line('subroutine foo(a)',(2, 2),'')
            a=AttributeHolder:
        variables=<dict with keys ['a']>
            content:
              Integer
                selector=('', '')
                entity_decls=['a']
                item=Line('integer a',(3, 3),'')
              Print
                item=Line('print*, "a=", a',(4, 4),'')
          EndSubroutine
            blocktype='subroutine'
            name='foo'
            item=Line('end',(5, 5),'')
>>>

Scalar support and compilation example

>>> from numpy.f2py.lib.main import compile
>>> code = '''c comment
...       subroutine foo(a)
...       integer a
...       print*, "a=", a
...       end
... '''
>>> m, = compile('c -*- f77 -*-\n' + code, 'mymodule') # tell f2py that code is Fortran 77
>>> m.foo(3)
 a= 3
>>> m, =compile(code,'mymodule2',extra_args=['--fcompiler=gnu95']) # use gnu95 compiler, f2py will automatically detect that code is in Fortran 90 fixed format
>>> m.foo(3)
 a=           3
>>>

Related documentation