fgen_runtime.base#

Base class for wrapped derived types

INVALID_INSTANCE_INDEX#

INVALID_INSTANCE_INDEX: int = -1#

Value used to denote an invalid instance_index.

This can occur value when a wrapper class has not yet been initialised (connected to a Fortran instance).

FinalizableWrapperBase#

class FinalizableWrapperBase(instance_index=-1)[source]#

Bases: ABC

Base class for model wrappers

abstract property exposed_attributes: tuple[str, ...]#

Attributes exposed by this wrapper

abstract finalize()[source]#

Finalise the Fortran instance and set self back to being uninitialised

This method resets self.instance_index back to _UNINITIALISED_instance_index

Should be decorated with check_initialised()

Return type:

None

abstract classmethod from_new_connection()[source]#

Initialise by establishing a new connection with the Fortran module

This requests a new model index from the Fortran module and then initialises a class instance

Returns:

fgen_runtime.base.FinalizableWrapperBaseNew class instance

property initialized: bool#

Is the instance initialised, i.e. connected to a Fortran instance?

instance_index: int#

Model index of wrapper Fortran instance

FinalizableWrapperBaseContext#

class FinalizableWrapperBaseContext(model)[source]#

Bases: AbstractContextManager

Context manager for a wrapper

model: fgen_runtime.base.FinalizableWrapperBase#

model instance to be managed

check_initialised#

check_initialised(method)[source]#

Check that initialisation is called before execution

Parameters:

method (typing.TypeVar(FuncT, bound= typing.Callable[..., typing.Any])) – Method to wrap

Returns:

typing.TypeVar(FuncT, bound= typing.Callable[..., typing.Any]) – * Wrapped method. Before the method is called, initialisation will first be * checked. If the model wrapper hasn’t been initialised before the method is * called, an InitialisationError will be raised.

execute_finalize_on_fail#

execute_finalize_on_fail(inst, func_to_try, *args, **kwargs)[source]#

Execute a function, finalising the instance before raising if any error occurs

This function is most useful in factory functions where it provides a clean way of ensuring that any Fortran is freed up in the event of an initialisation failure for any reason

Parameters:
Returns:

typing.TypeVar(T) – Result of calling func_to_try(inst.instance_index, \*args, \*\*kwargs)

Raises:

Exception – Any exception which occurs when calling func_to_try. Before the     exception is raised, ``inst.finalize() is called.