In this section we describe each of the annotations that can be used in specification files.
Annotations can either be argument annotations, class annotations, mapped type annotations, enum annotations, exception annotations, function annotations, license annotations, typedef annotations or variable annotations depending on the context in which they can be used.
Annotations are placed between forward slashes (/). Multiple annotations are comma separated within the slashes.
Annotations have a type and, possibly, a value. The type determines the format of the value. The name of an annotation and its value are separated by =.
Annotations can have one of the following types:
The value is the name of an API (defined using the %API directive) separated by a range of version numbers with a colon.
The range of version numbers is a pair of numbers separated by a hyphen specifying the lower and upper bounds of the range. A version number is within the range if it is greater or equal to the lower bound and less than the upper bound. Each bound can be omitted meaning that the range is unbounded in that direction.
For example:
# This is part of the PyTQt4 API up to but excluding v2.
void hex() /API=PyTQt4:-2/
# This is part of the PyTQt4 API starting from v2.
void hex() /PyName=hex_, API=PyTQt4:2-/
The following example shows argument and function annotations:
void exec(TQWidget * /Transfer/) /ReleaseGIL, PyName=call_exec/;
Note that the current version of SIP does not complain about unknown annotations, or annotations used out of their correct context.
This boolean annotation specifies that the corresponding argument refers to an array.
The argument should be either a pointer to a wrapped type, a char * or a unsigned char *. If the argument is a character array then the annotation also implies the Encoding annotation with an encoding of "None".
There must be a corresponding argument with the ArraySize annotation specified. The annotation may only be specified once in a list of arguments.
Python will automatically convert between certain compatible types. For example, if a floating pointer number is expected and an integer supplied, then the integer will be converted appropriately. This can cause problems when wrapping C or C++ functions with similar signatures. For example:
// The wrapper for this function will also accept an integer argument
// which Python will automatically convert to a floating point number.
void foo(double);
// The wrapper for this function will never get used.
void foo(int);
This boolean annotation specifies that the corresponding argument (which should be either bool, int, float, double, enum or a wrapped class) must match the type without any automatic conversions. In the context of a wrapped class the invocation of any %ConvertToTypeCode is suppressed.
The following example gets around the above problem:
// The wrapper for this function will only accept floating point
// numbers.
void foo(double /Constrained/);
// The wrapper for this function will be used for anything that Python
// can convert to an integer, except for floating point numbers.
void foo(int);
New in version 4.10.
This string annotation specifies the type of the argument as it will appear in any generated docstrings. It is usually used with arguments of type SIP_PYOBJECT to provide a more specific type.
New in version 4.10.
This string annotation specifies the default value of the argument as it will appear in any generated docstrings.
This string annotation specifies that the corresponding argument (which should be either char, const char, char * or const char *) refers to an encoded character or '\0' terminated encoded string with the specified encoding. The encoding can be either "ASCII", "Latin-1", "UTF-8" or "None". An encoding of "None" means that the corresponding argument refers to an unencoded character or string.
The default encoding is specified by the %DefaultEncoding directive. If the directive is not specified then None is used.
Python v3 will use the bytes type to represent the argument if the encoding is "None" and the str type otherwise.
Python v2 will use the str type to represent the argument if the encoding is "None" and the tqunicode type otherwise.
This boolean annotation is only ever used in conjunction with handwritten code specified with the %MethodCode directive. It causes an extra variable to be generated for the corresponding argument which is a pointer to the Python object that wraps the argument.
See the %MethodCode directive for more detail.
This boolean annotation is used to specify that the corresponding argument (which should be a pointer type) is used to pass a value to the function.
For pointers to wrapped C structures or C++ class instances, char * and unsigned char * then this annotation is assumed unless the Out annotation is specified.
For pointers to other types then this annotation must be explicitly specified if required. The argument will be dereferenced to obtain the actual value.
New in version 4.10.1.
This boolean annotation is used with arguments of virtual methods that are a const reference to a class. Normally, if the class defines a copy constructor then a copy of the returned reference is automatically created and wrapped before being passed to a Python reimplementation of the method. The copy will be owned by Python. This means that the reimplementation may take a reference to the argument without having to make an explicit copy.
If the annotation is specified then the copy is not made and the original reference is wrapped instead and will be owned by C++.
This boolean annotation is used to specify that the corresponding argument (which should be a pointer type) is used by the function to return a value as an element of a tuple.
For pointers to wrapped C structures or C++ class instances, char * and unsigned char * then this annotation must be explicitly specified if required.
For pointers to other types then this annotation is assumed unless the In annotation is specified.
This boolean annotation is used to specify that ownership of the corresponding argument (which should be a wrapped C structure or C++ class instance) is transferred from Python to C++. In addition, if the argument is of a class method, then it is associated with the class instance with regard to the cyclic garbage collector.
See Ownership of Objects for more detail.
This boolean annotation is used to specify that ownership of the corresponding argument (which should be a wrapped C structure or C++ class instance) is transferred back to Python from C++. In addition, any association of the argument with regard to the cyclic garbage collector with another instance is removed.
See Ownership of Objects for more detail.
This boolean annotation is only used in C++ constructors or methods. In the context of a constructor or factory method it specifies that ownership of the instance being created is transferred from Python to C++ if the corresponding argument (which should be a wrapped C structure or C++ class instance) is not None. In addition, the newly created instance is associated with the argument with regard to the cyclic garbage collector.
In the context of a non-factory method it specifies that ownership of this is transferred from Python to C++ if the corresponding argument is not None. If it is None then ownership is transferred to Python.
The annotation may be used more that once, in which case ownership is transferred to last instance that is not None.
See Ownership of Objects for more detail.
New in version 4.8.2.
Normally when a Python object is converted to a C/C++ instance None is handled automatically before the class’s %ConvertToTypeCode is called. This boolean annotation specifies that the handling of None will be left to the %ConvertToTypeCode. The annotation is ignored if the class does not have any %ConvertToTypeCode.
New in version 4.9.
This API range annotation is used to specify an API and corresponding range of version numbers that the class is enabled for.
If a class or mapped type has different implementations enabled for different ranges of version numbers then those ranges must not overlap.
See Managing Incompatible APIs for more detail.
This boolean annotation is used to specify that the class’s destructor should not be called until the Python interpreter exits. It would normally only be applied to singleton classes.
When the Python interpreter exits the order in which any wrapped instances are garbage collected is unpredictable. However, the underlying C or C++ instances may need to be destroyed in a certain order. If this annotation is specified then when the wrapped instance is garbage collected the C or C++ instance is not destroyed but instead added to a list of delayed instances. When the interpreter exits then the function sipDelayedDtors() is called with the list of delayed instances. sipDelayedDtors() can then choose to call (or ignore) the destructors in any desired order.
The sipDelayedDtors() function must be specified using the %ModuleCode directive.
Parameter: | dd_list – the linked list of delayed instances. |
---|
This structure describes a particular delayed destructor.
Note that the above applies only to C and C++ instances that are owned by Python.
This dotted name annotation specifies the name of the Python type object (i.e. the value of the tp_name field) used as the meta-type used when creating the type object for this C structure or C++ type.
See the section Types and Meta-types for more details.
This dotted name annotation specifies the name of the Python type object (i.e. the value of the tp_name field) used as the super-type used when creating the type object for this C structure or C++ type.
See the section Types and Meta-types for more details.
New in version 4.9.
This API range annotation is used to specify an API and corresponding range of version numbers that the mapped type is enabled for.
If a class or mapped type has different implementations enabled for different ranges of version numbers then those ranges must not overlap.
See Managing Incompatible APIs for more detail.
New in version 4.10.
This string annotation specifies the name of the type as it will appear in any generated docstrings.
New in version 4.9.
This API range annotation is used to specify an API and corresponding range of version numbers that the function is enabled for.
See Managing Incompatible APIs for more detail.
New in version 4.10.
This string annotation specifies the name of the type of the returned value as it will appear in any generated docstrings. It is usually used with values of type SIP_PYOBJECT to provide a more specific type.
This boolean annotation specifies that the value returned by the function (which should be a wrapped C structure or C++ class instance) is a newly created instance and is owned by Python.
See Ownership of Objects for more detail.
New in version 4.10.
This boolean annotation specifies that the argument parser generated for this function will support passing the parameters using Python’s keyword argument syntax. Keyword arguments cannot be used for functions that have unnamed arguments or use an ellipsis to designate that the function has a variable number of arguments.
New in version 4.10.3.
This boolean annotation specifies that a __len__() method should be automatically generated that will use the method being annotated to compute the value that the __len__() method will return.
New in version 4.10.1.
This boolean annotation is used with methods and global functions that return a const reference to a class. Normally, if the class defines a copy constructor then a copy of the returned reference is automatically created and wrapped. The copy will be owned by Python.
If the annotation is specified then the copy is not made and the original reference is wrapped instead and will be owned by C++.
New in version 4.10.
This boolean annotation specifies that the argument parser generated for this function will not support passing the parameters using Python’s keyword argument syntax. In other words, the argument parser will only support only normal positional arguments. This annotation is useful when the default setting of allowing keyword arguments has been changed via the command line, but you would still like certain functions to only support positional arguments.
This boolean annotation specifies that ownership of the value returned by the function (which should be a wrapped C structure or C++ class instance) is transferred to C++. It is only used in the context of a class constructor or a method.
In the case of methods returned values (unless they are new references to already wrapped values) are normally owned by C++ anyway. However, in addition, an association between the returned value and the instance containing the method is created with regard to the cyclic garbage collector.
See Ownership of Objects for more detail.
This boolean annotation specifies that ownership of the value returned by the function (which should be a wrapped C structure or C++ class instance) is transferred back to Python from C++. Normally returned values (unless they are new references to already wrapped values) are owned by C++. In addition, any association of the returned value with regard to the cyclic garbage collector with another instance is removed.
See Ownership of Objects for more detail.
This boolean annotation specifies that ownership of this is transferred from Python to C++.
See Ownership of Objects for more detail.
This optional string annotation specifies the license’s licensee. No restrictions are placed on the contents of the string.
See the %License directive.
This optional string annotation specifies the license’s signature. No restrictions are placed on the contents of the string.
See the %License directive.
This boolean annotation specifies that the definition of the type rather than the name of the type being defined should be used in the generated code.
Normally a typedef would be defined as follows:
typedef bool MyBool;
This would result in MyBool being used in the generated code.
Specifying the annotation means that bool will be used in the generated code instead.
New in version 4.10.
This string annotation specifies the name of the type of the variable as it will appear in any generated docstrings. It is usually used with variables of type SIP_PYOBJECT to provide a more specific type.