PythonMethodTable Derived Type

type, public :: PythonMethodTable

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

Python 3: initialise and configure in PyInit_module name function with bind(c, name="PyInit_module_name") attribute and type(c_ptr) return value. Python 2: initialise in initmodule name subroutine with bind(c) attribute

Pass the configured PythonMethodTable to PythonModule%init



Type-Bound Procedures

procedure, public :: init => PythonMethodTable_init

Initialises the method table. Call in PyInit_module name (Py3) / initmodule name (Py2)

  • private subroutine PythonMethodTable_init(self, num_methods)

    Arguments

    Type IntentOptional AttributesName
    class(PythonMethodTable), intent(inout) :: self
    integer, intent(in) :: num_methods

    The number of methods your Python module shall have.

procedure, public :: add_method => PythonMethodTable_add_method

Adds a method to your Python module

  • private subroutine PythonMethodTable_add_method(self, method_name, doc_string, flags, method_funptr)

    Arguments

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

    Name of the Python method.

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

    Doc string for the Python method

    integer(kind=C_INT), intent(in) :: flags

    Controls which kind of arguments the Python method shall take.

    use flags=METH_VARARGS if method shall take only arguments. use flags=METH_KWARGS if method shall take only keyword args. use flags=METH_VARARGS+METH_KWARGS if method shall take both arguments and keyword args. use flags=METH_NOARGS if method shall take no arguments.

    type(c_funptr), intent(in) :: method_funptr

    Function pointer to the Fortran implementation of the method.

    Use C_FUNLOC() to get method_funptr The Fortran function must take "TYPE(c_ptr), VALUE" arguments (number depends on what arguments it takes) and have a type(c_ptr) return value. E. g. if flags=METH_VARARGS+METH_KWARGS, one needs 3 arguments (1st arg: module c_ptr, 2nd: arguments c_ptr, 3rd: kwargs c_ptr) use unsafe_cast_from_c_ptr to cast 2nd argument to a tuple and 3rd argument to a dict. The function must return a Python object type(c_ptr). Use object%get_c_ptr() to retrieve the c_ptr from a forpy object.

procedure, public :: get_method_table => PythonMethodTable_get

Used only internally. Gets type(c_ptr) to method table.

  • private function PythonMethodTable_get(self) result(m)

    Arguments

    Type IntentOptional AttributesName
    class(PythonMethodTable), intent(in) :: self

    Return Value type(c_ptr)