Fortran to Python interface generator with derived type support for automated wrapper generation
npx @tessl/cli install tessl/pypi-f90wrap@0.2.0A specialized Python tool that automatically generates Python extension modules to interface with Fortran code, particularly code that uses derived types. f90wrap extends f2py capabilities by creating a simplified Fortran 90 interface layer that is then wrapped with f2py, combined with a higher-level Pythonic wrapper that makes the additional layer transparent to users.
pip install f90wrap or conda install -c conda-forge f90wrapimport f90wrapFor programmatic API usage:
from f90wrap import parser
from f90wrap import fortran
from f90wrap import transform
from f90wrap import f90wrapgen
from f90wrap import pywrapgen
from f90wrap import codegen
from f90wrap.runtime import FortranDerivedType, FortranModule, register_classFor command-line tools:
from f90wrap.scripts.main import main as f90wrap_main
from f90wrap.scripts.f90doc import main as f90doc_main
from f90wrap.scripts.f2py_f90wrap import main as f2py_f90wrap_mainGenerate Python wrappers for Fortran source files:
# Basic wrapper generation
f90wrap -m mymodule fortran_source.f90
# Compile with f2py
f2py -c -m _mymodule f90wrap_*.f90 *.o
# Enhanced f2py with f90wrap features
f2py-f90wrap -c -m _mymodule f90wrap_*.f90 *.o
# Generate documentation
f90doc fortran_source.f90import f90wrap.parser as parser
import f90wrap.fortran as fortran
import f90wrap.transform as transform
# Parse Fortran source files
args = ['source.f90']
tree = parser.read_files(args)
# Transform AST for wrapping
transform.transform_to_f90_wrapper(tree, {}, [], [], [])
# Use runtime classes in generated wrappers
from f90wrap.runtime import FortranDerivedType
class MyFortranType(FortranDerivedType):
# Generated wrapper code would use this base class
passf90wrap follows a multi-stage compilation process:
The design supports advanced Fortran features like derived types, optional arguments, and provides enhanced error handling and interrupt capabilities.
Three main command-line utilities for wrapper generation, documentation, and enhanced f2py compilation.
f90wrap -m MODULE F90_FILES [OPTIONS]
f90doc F90_FILES [OPTIONS]
f2py-f90wrap [F2PY_OPTIONS]Comprehensive parsing of Fortran 90/95/2003/2008 source code into a structured Abstract Syntax Tree with support for modules, derived types, procedures, and interfaces.
def read_files(args, doc_plugin_filename=None): ...
class F90File(object): ...
class Fortran(object): ... # Base AST node
class Module(Fortran): ... # Module node
class Type(Fortran): ... # Derived type nodePowerful transformation system for adapting Fortran AST to be suitable for Python wrapping, including type conversions, intent handling, and code restructuring.
def transform_to_f90_wrapper(tree, types, callbacks, constructors, destructors, ...): ...
def transform_to_py_wrapper(tree, types): ...
class FortranTransformer(FortranVisitor): ...Flexible code generation framework for producing both Fortran wrapper code and Python wrapper code with customizable formatting and structure.
class CodeGenerator(object): ...
class F90WrapperGenerator(FortranVisitor, CodeGenerator): ...
class PythonWrapperGenerator(FortranVisitor, CodeGenerator): ...Runtime classes and utilities that provide the foundation for generated wrapper code, handling derived type management and Fortran-Python interoperability.
class FortranDerivedType(object): ...
class FortranDerivedTypeArray(object): ...
class FortranModule(object): ...
def register_class(object): ...Comprehensive type system analysis and utility functions for handling Fortran type conversions, kind mapping, and array processing.
def is_derived_type(typename): ...
def f2py_type(type, attributes=None): ...
def normalise_type(typename, kind_map): ...f90wrap provides enhanced error handling capabilities:
RuntimeError exceptions