f2py_wrapper_gen — Fortran 90 source code wrapping

This module is used to write intermediary Fortran 90 wrapper files to allow a code which makes use of derived types to be wrapped with f2py.

All routines which accept derived types arguments are wrapped by equivalent routines which instead accept integer arrays as opaque handles. The Fortran transfer() intrinsic is used to convert these handles into pointers to derived types, as described in [Pletzer2008]. Using integer arrays of size 12 makes this approach portable across most currently available Fortran compilers. In this way we can access the underlying Fortran structures from Python.

initialise() and finalise() routines are handled specially: on initialisation, a derived type pointer is allocated before the wrapped routine is invoked, and an opaque reference to this new derived type is returned. On finalisation the underlying derived type pointer is deallocated after the wrapped routine returns.

Extra routines are generated to access the values of attributes within derived types. For scalars a pair of get and set routines is created, whilst for arrays a single routine which returns the shape, memory location and type of the array is output. These routines are used by quippy.oo_fortran when constructing the object-oriented wrapper layer.

There are various other special cases which are handled individually: for details see the source code.

This module defines a single function:

f2py_wrapper_gen.wrap_mod(mod, type_map[, out, kindlines])
Parameters:
  • modf90doc.C_module instance
  • type_map – dictionary
  • out – file-like object or None
  • kindlines – list of strings
Return type:

dictionary

Write a Fortran 90 wrapper file for the module mod to the file out. Returns a specification dictionary describing the interface which has been created.

type_map should be a dictionary mapping Fortran derived-type names to the names of the Fortran modules in which they are defined.

kind_lines is a list of strings to be included near the top of the generated module to import any Fortran kind definitions that are needed from other modules.

The spec dictionary returned by this function is combined with those of other modules and saved to disk by quippy’s setup.py script.