PythonModule Derived Type

type, public :: PythonModule

Only used for writing Python extension modules. Datastructure to hold information about your Python extension module. Put exactly one instance at Fortran module level.

Python 3: initialise and configure in PyInit_ function with bind(c, name="PyInit_") attribute and type(c_ptr) return value.

Python 2: initialise in init subroutine with bind(c) attribute



Type-Bound Procedures

procedure, public :: init => PythonModule_init

Initialises the PythonModule with a PythonMethod table.

Python 3: the return value must be returned by PyInit_module name

Python 2: call in initmodule name, ignore return value

  • private function PythonModule_init(self, module_name, doc_string, method_table) result(module_ptr)

    Arguments

    Type IntentOptional AttributesName
    class(PythonModule), intent(inout) :: self
    character(kind=C_CHAR,len=*), intent(in) :: module_name

    Name of the Python extension module.

    character(kind=C_CHAR,len=*), intent(in) :: doc_string

    Doc string for the Python extension module.

    type(PythonMethodTable), intent(in) :: method_table

    Table of methods of the Python extension module.

    Return Value type(c_ptr)

procedure, public :: add_object => PythonModule_add_object

Adds a Python object to the module that can be accessed by my_module.the_name_of_object_added Useful to add constants to a Python module

  • private function PythonModule_add_object(self, object_name, obj) result(ierror)

    add an object as a member to a module

    Arguments

    Type IntentOptional AttributesName
    class(PythonModule), intent(inout) :: self
    character(kind=C_CHAR,len=*), intent(in) :: object_name

    Name of the module member. It can be accessed by module_name.object_name in Python.

    class(object), intent(in) :: obj

    The object to add as a member to the module.

    Return Value integer(kind=C_INT)

    Error code, 0 on success.