Professional Documents
Culture Documents
===========
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
``=``.
*boolean*
This type of annotation has no value and is implicitly true.
*integer*
This type of annotation is an integer. In some cases the value is
optional.
*name*
The value is a name that is compatible with a C/C++ identifier. In some
cases the value is optional.
*dotted name*
The value is a name that is compatible with an identifier preceded by a
Python scope.
*string*
The value is a double quoted string.
*API range*
The value is the name of an API (defined using the :directive:`%API`
directive) separated by a range of version numbers with a colon.
For example::
.. _ref-arg-annos:
Argument Annotations
--------------------
.. argument-annotation:: AllowNone
.. argument-annotation:: Array
.. argument-annotation:: ArraySize
.. argument-annotation:: Constrained
// 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 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);
.. argument-annotation:: DisallowNone
.. versionadded:: 4.16.4
.. argument-annotation:: DocType
.. versionadded:: 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
:stype:`SIP_PYOBJECT` to provide a more specific type.
.. argument-annotation:: DocValue
.. versionadded:: 4.10
.. argument-annotation:: Encoding
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 ``unicode`` type otherwise.
.. argument-annotation:: GetWrapper
.. argument-annotation:: In
For pointers to wrapped C structures or C++ class instances, ``char *`` and
``unsigned char *`` then this annotation is assumed unless the :aanno:`Out`
annotation is specified.
Both :aanno:`In` and :aanno:`Out` may be specified for the same argument.
.. argument-annotation:: KeepReference
Otherwise the reference is not kept by any specific object and will never
be released.
.. argument-annotation:: NoCopy
.. versionadded:: 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++.
.. argument-annotation:: Out
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
:aanno:`In` annotation is specified.
Both :aanno:`In` and :aanno:`Out` may be specified for the same argument.
.. argument-annotation:: PyInt
.. versionadded:: 4.12
.. argument-annotation:: ResultSize
.. argument-annotation:: SingleShot
.. argument-annotation:: Transfer
.. argument-annotation:: TransferBack
.. argument-annotation:: TransferThis
The annotation may be used more that once, in which case ownership is
transferred to last instance that is not ``None``.
.. _ref-class-annos:
Class Annotations
-----------------
.. class-annotation:: Abstract
This boolean annotation is used to specify that the class has additional
pure virtual methods that have not been specified and so it cannot be
instantiated or sub-classed from Python.
.. class-annotation:: AllowNone
.. versionadded:: 4.8.2
.. versionadded:: 4.9
.. class-annotation:: DelayDtor
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
:c:func:`sipDelayedDtors()` is called with the list of delayed instances.
:c:func:`sipDelayedDtors()` can then choose to call (or ignore) the
destructors in any desired order.
:param dd_list:
the linked list of delayed instances.
.. c:type:: sipDelayedDtor
This is the name of the class excluding any package or module name.
This is the address of the next entry in the list or zero if this is
the last one.
Note that the above applies only to C and C++ instances that are owned by
Python.
.. class-annotation:: Deprecated
.. class-annotation:: FileExtension
.. versionadded:: 4.16.6
.. class-annotation:: ExportDerived
.. versionadded:: 4.15
In many cases SIP generates a derived class for each class being wrapped
(see :ref:`ref-derived-classes`). Normally this is used internally. This
boolean annotation specifies that the declaration of the class is exported
and able to be used by handwritten code.
.. class-annotation:: External
.. class-annotation:: Metatype
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.
.. class-annotation:: Mixin
.. versionadded:: 4.15
This boolean annotation specifies that the class can be used as a mixin
with other wrapped classes.
Normally a Python application cannot define a new class that is derived
from more than one wrapped class. In C++ this would create a new C++
class. This cannot be done from Python. At best a C++ instance of each of
the wrapped classes can be created and wrapped as separate Python objects.
However some C++ classes may function perfectly well with this restriction.
Such classes are often intended to be used as mixins.
.. class-annotation:: NoDefaultCtors
.. class-annotation:: PyName
This name annotation specifies an alternative name for the class being
wrapped which is used when it is referred to from Python. It is required
when a class name is the same as a Python keyword. It may also be used to
avoid name clashes with other objects (e.g. enums, exceptions, functions)
that have the same name in the same C++ scope.
.. seealso:: :directive:`%AutoPyName`
.. class-annotation:: Supertype
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.
.. class-annotation:: VirtualErrorHandler
.. versionadded:: 4.14
.. mapped-type-annotation:: AllowNone
.. mapped-type-annotation:: API
.. versionadded:: 4.9
.. mapped-type-annotation:: DocType
.. versionadded:: 4.10
.. mapped-type-annotation:: NoRelease
This boolean annotation is used to specify that the mapped type does not
support the :c:func:`sipReleaseType()` function. Any
:directive:`%ConvertToTypeCode` should not create temporary instances of
the mapped type, i.e. it should not return :c:macro:`SIP_TEMPORARY`.
.. mapped-type-annotation:: PyName
This name annotation specifies an alternative name for the mapped type
being wrapped which is used when it is referred to from Python. The only
time a Python type is created for a mapped type is when it is used as a
scope for static methods or enums.
.. seealso:: :directive:`%AutoPyName`
.. _ref-enum-annos:
Enum Annotations
----------------
.. enum-annotation:: NoScope
.. versionadded:: 4.15
.. enum-annotation:: PyName
This name annotation specifies an alternative name for the enum or enum
member being wrapped which is used when it is referred to from Python. It
is required when an enum or enum member name is the same as a Python
keyword. It may also be used to avoid name clashes with other objects
(e.g. classes, exceptions, functions) that have the same name in the same
C++ scope.
.. seealso:: :directive:`%AutoPyName`
.. _ref-exception-annos:
Exception Annotations
---------------------
.. exception-annotation:: Default
This boolean annotation specifies that the exception being defined will be
used as the default exception to be caught if a function or constructor
does not have a ``throw`` clause.
.. exception-annotation:: PyName
This name annotation specifies an alternative name for the exception being
defined which is used when it is referred to from Python. It is required
when an exception name is the same as a Python keyword. It may also be
used to avoid name clashes with other objects (e.g. classes, enums,
functions) that have the same name.
.. seealso:: :directive:`%AutoPyName`
.. _ref-function-annos:
Function Annotations
--------------------
.. function-annotation:: AbortOnException
.. versionadded:: 4.16.4
.. function-annotation:: AllowNone
.. versionadded:: 4.16.4
This boolean annotation is used to specify that the value returned by the
function (which should be either :stype:`SIP_PYBUFFER`,
:stype:`SIP_PYCALLABLE`, :stype:`SIP_PYDICT`, :stype:`SIP_PYLIST`,
:stype:`SIP_PYSLICE`, :stype:`SIP_PYTUPLE` or :stype:`SIP_PYTYPE`) may be
``None``.
.. function-annotation:: API
.. versionadded:: 4.9
.. function-annotation:: AutoGen
This optional name annotation is used with class methods to specify that
the method be automatically included in all sub-classes. The value is the
name of a feature (specified using the :directive:`%Feature` directive)
which must be enabled for the method to be generated.
.. function-annotation:: Default
This boolean annotation is only used with C++ constructors. Sometimes SIP
needs to create a class instance. By default it uses a constructor with no
compulsory arguments if one is specified. (SIP will automatically generate
a constructor with no arguments if no constructors are specified.) This
annotation is used to explicitly specify which constructor to use. Zero is
passed as the value of any arguments to the constructor. This annotation
is ignored if the class defines :directive:`%InstanceCode`.
.. function-annotation:: Deprecated
.. function-annotation:: DisallowNone
.. versionadded:: 4.16.4
This boolean annotation is used to specify that the value returned by the
function (which should be a pointer to either a C++ class or a mapped type)
must not be ``None``.
.. function-annotation:: DocType
.. versionadded:: 4.10
.. function-annotation:: Encoding
.. function-annotation:: Factory
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.
.. function-annotation:: HoldGIL
This boolean annotation specifies that the Python Global Interpreter Lock
(GIL) is not released before the call to the underlying C or C++ function.
See :ref:`ref-gil` and the :fanno:`ReleaseGIL` annotation.
.. function-annotation:: __imatmul__
.. versionadded:: 4.17
.. function-annotation:: KeepReference
.. versionadded:: 4.12.2
.. function-annotation:: KeywordArgs
.. versionadded:: 4.10
This string annotation specifies the level of support the argument parser
generated for this function will provide for passing the parameters using
Python's keyword argument syntax. The value of the annotation can be
either ``"None"`` meaning that keyword arguments are not supported,
``"All"`` meaning that all named arguments can be passed as keyword
arguments, or ``"Optional"`` meaning that all named optional arguments
(i.e. those with a default value) can be passed as keyword arguments.
.. deprecated:: 4.12
It can also be used as a boolean annotation which is the equivalent of
specifiying a value of ``"All"``.
.. function-annotation:: __len__
.. versionadded:: 4.10.3
.. function-annotation:: __matmul__
.. versionadded:: 4.17
.. function-annotation:: NewThread
This boolean annotation specifies that the function will create a new
thread.
.. function-annotation:: NoArgParser
.. function-annotation:: NoCopy
.. versionadded:: 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++.
.. function-annotation:: NoDerived
This boolean annotation is only used with C++ constructors. In many cases
SIP generates a derived class for each class being wrapped (see
:ref:`ref-derived-classes`). This derived class contains constructors with
the same C++ signatures as the class being wrapped. Sometimes you may want
to define a Python constructor that has no corresponding C++ constructor.
This annotation is used to suppress the generation of the constructor in
the derived class.
.. function-annotation:: NoKeywordArgs
.. versionadded:: 4.10
.. deprecated:: 4.12
Use the :fanno:`KeywordArgs` annotation with a value of ``"None"``.
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 normal positional arguments. This annotation is useful when the
default setting of allowing keyword arguments has been changed via the
command line or the :directive:`%Module` directive, but you would still
like certain functions to only support positional arguments.
.. function-annotation:: NoRaisesPyException
.. versionadded:: 4.13.1
This boolean annotation specifies that the function or constructor does not
raise a Python exception to indicate that an error occurred.
.. seealso:: :fanno:`RaisesPyException`
.. function-annotation:: NoVirtualErrorHandler
.. versionadded:: 4.14
.. function-annotation:: Numeric
This boolean annotation specifies that the operator should be interpreted
as a numeric operator rather than a sequence operator.
Python uses the ``+`` operator for adding numbers and concatanating
sequences, and the ``*`` operator for multiplying numbers and repeating
sequences. Unless this or the :fanno:`Sequence` annotation is specified,
SIP tries to work out which is meant by looking at other operators that
have been defined for the type. If it finds either ``-``, ``-=``, ``/``,
``/=``, ``%`` or ``%=`` defined then it assumes that ``+``, ``+=``, ``*``
and ``*=`` should be numeric operators. Otherwise, if it finds either
``[]``, :meth:`__getitem__`, :meth:`__setitem__` or :meth:`__delitem__`
defined then it assumes that they should be sequence operators.
.. function-annotation:: PostHook
This name annotation is used to specify the name of a Python builtin that
is called immediately after the call to the underlying C or C++ function or
any handwritten code. The builtin is not called if an error occurred. It
is primarily used to integrate with debuggers.
.. function-annotation:: PreHook
This name annotation is used to specify the name of a Python builtin that
is called immediately after the function's arguments have been successfully
parsed and before the call to the underlying C or C++ function or any
handwritten code. It is primarily used to integrate with debuggers.
.. function-annotation:: PyName
This name annotation specifies an alternative name for the function being
wrapped which is used when it is referred to from Python. It is required
when a function or method name is the same as a Python keyword. It may
also be used to avoid name clashes with other objects (e.g. classes, enums,
exceptions) that have the same name in the same C++ scope.
.. seealso:: :directive:`%AutoPyName`
.. function-annotation:: PyInt
.. versionadded:: 4.12
.. function-annotation:: RaisesPyException
.. versionadded:: 4.12.1
.. function-annotation:: ReleaseGIL
This boolean annotation specifies that the Python Global Interpreter Lock
(GIL) is released before the call to the underlying C or C++ function and
reacquired afterwards. It should be used for functions that might block or
take a significant amount of time to execute. See :ref:`ref-gil` and the
:fanno:`HoldGIL` annotation.
.. function-annotation:: Sequence
.. versionadded:: 4.14.7
Python uses the ``+`` operator for adding numbers and concatanating
sequences, and the ``*`` operator for multiplying numbers and repeating
sequences. Unless this or the :fanno:`Numeric` annotation is specified,
SIP tries to work out which is meant by looking at other operators that
have been defined for the type. If it finds either ``-``, ``-=``, ``/``,
``/=``, ``%`` or ``%=`` defined then it assumes that ``+``, ``+=``, ``*``
and ``*=`` should be numeric operators. Otherwise, if it finds either
``[]``, :meth:`__getitem__`, :meth:`__setitem__` or :meth:`__delitem__`
defined then it assumes that they should be sequence operators.
.. function-annotation:: Transfer
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.
.. function-annotation:: TransferBack
.. function-annotation:: TransferThis
This boolean annotation specifies that ownership of ``this`` is transferred
from Python to C++.
.. function-annotation:: VirtualErrorHandler
.. versionadded:: 4.14
.. _ref-typedef-annos:
Typedef Annotations
-------------------
.. typedef-annotation:: Capsule
.. versionadded:: 4.14.1
This boolean annotation may only be used when the base type is ``void *``
and specifies that a Python capsule object is used to wrap the value rather
than a :class:`sip.voidptr`. The advantage of using a capsule is that name
based type checking is performed using the name of the type being defined.
.. typedef-annotation:: DocType
.. versionadded:: 4.10
.. typedef-annotation:: Encoding
.. typedef-annotation:: NoTypeName
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::
Specifying the annotation means that ``bool`` will be used in the generated
code instead.
.. typedef-annotation:: PyInt
.. versionadded:: 4.12
.. typedef-annotation:: PyName
.. versionadded:: 4.13.1
This name annotation only applies when the typedef is being used to create
the wrapping for a class defined using a template and specifies an
alternative name for the class when it is referred to from Python. It is
required when a class name is the same as a Python keyword. It may also
be used to avoid name clashes with other objects (e.g. enums, exceptions,
functions) that have the same name in the same C++ scope.
.. seealso:: :directive:`%AutoPyName`
.. _ref-variable-annos:
Variable Annotations
--------------------
.. variable-annotation:: DocType
.. versionadded:: 4.10
.. variable-annotation:: Encoding
.. variable-annotation:: NoSetter
.. versionadded:: 4.16
This boolean annotation specifies that the variable will have no setter and
will be read-only. Because SIP does not fully understand C/C++ types
(particularly ``const`` arrays) it is sometimes necessary to explicitly
annotate a variable as being read-only.
.. variable-annotation:: PyInt
.. versionadded:: 4.12
.. variable-annotation:: PyName
This name annotation specifies an alternative name for the variable being
wrapped which is used when it is referred to from Python. It is required
when a variable name is the same as a Python keyword. It may also be used
to avoid name clashes with other objects (e.g. classes, functions) that
have the same name in the same C++ scope.
.. seealso:: :directive:`%AutoPyName`