Signature file — NumPy v1.13 Manual (original) (raw)

The syntax specification for signature files (.pyf files) is borrowed from the Fortran 90/95 language specification. Almost all Fortran 90/95 standard constructs are understood, both in free and fixed format (recall that Fortran 77 is a subset of Fortran 90/95). F2PY introduces also some extensions to Fortran 90/95 language specification that help designing Fortran to Python interface, make it more “Pythonic”.

Signature files may contain arbitrary Fortran code (so that Fortran codes can be considered as signature files). F2PY silently ignores Fortran constructs that are irrelevant for creating the interface. However, this includes also syntax errors. So, be careful not making ones;-).

In general, the contents of signature files is case-sensitive. When scanning Fortran codes and writing a signature file, F2PY lowers all cases automatically except in multiline blocks or when --no-loweroption is used.

The syntax of signature files is presented below.

Python module block

A signature file may contain one (recommended) or more python module blocks. python module block describes the contents of a Python/C extension module <modulename>module.c that F2PY generates.

Exception: if <modulename> contains a substring __user__, then the corresponding python module block describes the signatures of so-called call-back functions (see Call-back arguments).

A python module block has the following structure:

python module []... [ interface <Fortran/C routine signatures> end [interface] ]... [ interface module [] [] end [module []] end [interface] ]... end [python module []]

Here brackets [] indicate an optional part, dots ... indicate one or more of a previous part. So, []... reads zero or more of a previous part.

Fortran/C routine signatures

The signature of a Fortran routine has the following structure:

[] function | subroutine
[ ( [] ) ] [ result ( ) ] [<argument/variable type declarations>] [<argument/variable attribute statements>] [] [] [] end [ function | subroutine [] ]

From a Fortran routine signature F2PY generates a Python/C extension function that has the following signature:

def ([,]): ... return

The signature of a Fortran block data has the following structure:

block data [ ] [] [] [] [] [] end [ block data [] ]

Type declarations

The definition of the <argument/variable type declaration> part is

[ [] :: ]

where

:= byte | character [] | complex [] | real [] | double complex | double precision | integer [] | logical []

:= * | ( [len=] [ , [kind=] ] ) | ( kind= [ , len= ] ) := * | ( [kind=] )

:= [ [ * ] [ ( ) ] | [ ( ) ] * ] | [ / / | = ]
[ , ]

and

If an argument has no <argument type declaration>, its type is determined by applying implicit rules to its name.

Statements

Attribute statements:

The <argument/variable attribute statement> is<argument/variable type declaration> without <typespec>. In addition, in an attribute statement one cannot use other attributes, also <entitydecl> can be only a list of names.

Use statements:

The definition of the <use statement> part is

use [ , | , ONLY : ]

where

:= => [ , ]

Currently F2PY uses use statement only for linking call-back modules and external arguments (call-back functions), seeCall-back arguments.

Common block statements:

The definition of the <common block statement> part is

common / /

where

:= [ ( ) ] [ , ]

If a python module block contains two or more common blocks with the same name, the variables from the additional declarations are appended. The types of variables in <shortentitydecl> are defined using <argument type declarations>. Note that the corresponding <argument type declarations> may contain array specifications; then you don’t need to specify these in<shortentitydecl>.

Other statements:

The <other statement> part refers to any other Fortran language constructs that are not described above. F2PY ignores most of them except

In addition, F2PY introduces the following statements:

Attributes

The following attributes are used by F2PY:

optional

The corresponding argument is moved to the end of <optional arguments> list. A default value for an optional argument can be specified <init_expr>, see entitydecl definition. Note that the default value must be given as a valid C expression.

Note that whenever <init_expr> is used, optional attribute is set automatically by F2PY.

For an optional array argument, all its dimensions must be bounded.

required

The corresponding argument is considered as a required one. This is default. You need to specify required only if there is a need to disable automatic optional setting when <init_expr> is used.

If Python None object is used as a required argument, the argument is treated as optional. That is, in the case of array argument, the memory is allocated. And if <init_expr> is given, the corresponding initialization is carried out.

dimension(<arrayspec>)

The corresponding variable is considered as an array with given dimensions in <arrayspec>.

intent(<intentspec>)

This specifies the “intention” of the corresponding argument. <intentspec> is a comma separated list of the following keys:

The following rules apply:

check([<C-booleanexpr>])

Perform consistency check of arguments by evaluating<C-booleanexpr>; if <C-booleanexpr> returns 0, an exception is raised.

If check(..) is not used then F2PY generates few standard checks (e.g. in a case of an array argument, check for the proper shape and size) automatically. Use check() to disable checks generated by F2PY.

depend([<names>])

This declares that the corresponding argument depends on the values of variables in the list <names>. For example, <init_expr>may use the values of other arguments. Using information given bydepend(..) attributes, F2PY ensures that arguments are initialized in a proper order. If depend(..) attribute is not used then F2PY determines dependence relations automatically. Usedepend() to disable dependence relations generated by F2PY.

When you edit dependence relations that were initially generated by F2PY, be careful not to break the dependence relations of other relevant variables. Another thing to watch out is cyclic dependencies. F2PY is able to detect cyclic dependencies when constructing wrappers and it complains if any are found.

allocatable

The corresponding variable is Fortran 90 allocatable array defined as Fortran 90 module data.

external

The corresponding argument is a function provided by user. The signature of this so-called call-back function can be defined

For example, F2PY generates from

external cb_sub, cb_fun integer n real a(n),r call cb_sub(a,n) r = cb_fun(4)

the following call-back signatures:

subroutine cb_sub(a,n) real dimension(n) :: a integer optional,check(len(a)>=n),depend(a) :: n=len(a) end subroutine cb_sub function cb_fun(e_4_e) result (r) integer :: e_4_e real :: r end function cb_fun

The corresponding user-provided Python function are then:

def cb_sub(a,[n]): ... return def cb_fun(e_4_e): ... return r

See also intent(callback) attribute.

parameter

The corresponding variable is a parameter and it must have a fixed value. F2PY replaces all parameter occurrences by their corresponding values.

Extensions

F2PY directives

The so-called F2PY directives allow using F2PY signature file constructs also in Fortran 77/90 source codes. With this feature you can skip (almost) completely intermediate signature file generations and apply F2PY directly to Fortran source codes.

F2PY directive has the following form:

where allowed comment characters for fixed and free format Fortran codes are cC*!# and !, respectively. Everything that follows<comment char>f2py is ignored by a compiler but read by F2PY as a normal Fortran, non-comment line:

When F2PY finds a line with F2PY directive, the directive is first replaced by 5 spaces and then the line is reread.

For fixed format Fortran codes, <comment char> must be at the first column of a file, of course. For free format Fortran codes, F2PY directives can appear anywhere in a file.

C expressions

C expressions are used in the following parts of signature files:

A C expression may contain:

For initializing an array <array name>, F2PY generates a loop over all indices and dimensions that executes the following pseudo-statement:

(_i[0],_i[1],...) = ;

where _i[<i>] refers to the <i>-th index value and that runs from 0 to shape(<array name>,<i>)-1.

For example, a function myrange(n) generated from the following signature

subroutine myrange(a,n) fortranname ! myrange is a dummy wrapper integer intent(in) :: n real*8 intent(c,out),dimension(n),depend(n) :: a = _i[0] end subroutine myrange

is equivalent to numpy.arange(n,dtype=float).

Warning

F2PY may lower cases also in C expressions when scanning Fortran codes (see --[no]-lower option).

Multiline blocks

A multiline block starts with ''' (triple single-quotes) and ends with ''' in some strictly subsequent line. Multiline blocks can be used only within .pyf files. The contents of a multiline block can be arbitrary (except that it cannot contain ''') and no transformations (e.g. lowering cases) are applied to it.

Currently, multiline blocks can be used in the following constructs: