<ttclass="descname">%BIGetBufferCode</tt><aclass="headerlink"href="#directive-%BIGetBufferCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIGetBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIReleaseBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIReleaseBufferCode</span></tt></a>) is used to
specify code that implements the buffer interface of Python v3. If Python v2
is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt>Py_buffer *sipBuffer</dt>
<dd>This is a pointer to the Python buffer structure that the handwritten code
must populate.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>int sipFlags</dt>
<dd>These are the flags that specify what elements of the <ttclass="docutils literal"><spanclass="pre">sipBuffer</span></tt>
structure must be populated.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to 0 if there was no error or -1 if
there was an error.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<ttclass="descname">%BIGetCharBufferCode</tt><aclass="headerlink"href="#directive-%BIGetCharBufferCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIGetCharBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIGetReadBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetReadBufferCode</span></tt></a>,
<aclass="reference internal"href="#directive-%BIGetSegCountCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetSegCountCode</span></tt></a> and <aclass="reference internal"href="#directive-%BIGetWriteBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetWriteBufferCode</span></tt></a>) is used
to specify code that implements the buffer interface of Python v2. If Python
v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the character buffer.</dd>
<ttclass="descname">%BIGetReadBufferCode</tt><aclass="headerlink"href="#directive-%BIGetReadBufferCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIGetReadBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIGetCharBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetCharBufferCode</span></tt></a>,
<aclass="reference internal"href="#directive-%BIGetSegCountCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetSegCountCode</span></tt></a> and <aclass="reference internal"href="#directive-%BIGetWriteBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetWriteBufferCode</span></tt></a>) is used
to specify code that implements the buffer interface of Python v2. If
Python v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the read buffer.</dd>
<ttclass="descname">%BIGetSegCountCode</tt><aclass="headerlink"href="#directive-%BIGetSegCountCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIGetSegCountCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIGetCharBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetCharBufferCode</span></tt></a>,
<aclass="reference internal"href="#directive-%BIGetReadBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetReadBufferCode</span></tt></a> and <aclass="reference internal"href="#directive-%BIGetWriteBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetWriteBufferCode</span></tt></a>) is
used to specify code that implements the buffer interface of Python v2. If
Python v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
<ttclass="descname">%BIGetWriteBufferCode</tt><aclass="headerlink"href="#directive-%BIGetWriteBufferCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIGetWriteBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIGetCharBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetCharBufferCode</span></tt></a>,
<aclass="reference internal"href="#directive-%BIGetReadBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetReadBufferCode</span></tt></a> and <aclass="reference internal"href="#directive-%BIGetSegCountCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetSegCountCode</span></tt></a> is used
to specify code that implements the buffer interface of Python v2. If Python
v3 is being used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>void **sipPtrPtr</dt>
<dd>This is the pointer used to return the address of the write buffer.</dd>
<ttclass="descname">%BIReleaseBufferCode</tt><aclass="headerlink"href="#directive-%BIReleaseBufferCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%BIReleaseBufferCode
<em>code</em>
%End
</pre>
<p>This directive (along with <aclass="reference internal"href="#directive-%BIGetBufferCode"><ttclass="xref docutils literal"><spanclass="pre">%BIGetBufferCode</span></tt></a>) is used to specify
code that implements the buffer interface of Python v3. If Python v2 is being
used then this is ignored.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt>Py_buffer *sipBuffer</dt>
<dd>This is a pointer to the Python buffer structure.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>PyObject *sipSelf</dt>
<dd>This is the Python object that wraps the structure or class instance, i.e.
<ttclass="descname">%CModule</tt><aclass="headerlink"href="#directive-%CModule"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%CModule <em>name</em> [<em>version</em>]
</pre>
<p>This directive is used to identify that the library being wrapped is a C
library and to define the name of the module and it’s optional version number.</p>
<p>See the <aclass="reference internal"href="#directive-%Module"><ttclass="xref docutils literal"><spanclass="pre">%Module</span></tt></a> directive for an explanation of the version
<ttclass="descname">%CompositeModule</tt><aclass="headerlink"href="#directive-%CompositeModule"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%CompositeModule <em>name</em>
</pre>
<p>A composite module is one that merges a number of related SIP generated
modules. For example, a module that merges the modules <ttclass="docutils literal"><spanclass="pre">a_mod</span></tt>, <ttclass="docutils literal"><spanclass="pre">b_mod</span></tt>
and <ttclass="docutils literal"><spanclass="pre">c_mod</span></tt> is equivalent to the following pure Python module:</p>
<p>A consolidated module is not intended to be explicitly imported by an
application. Instead it is imported by its component modules when they
themselves are imported.</p>
<p>Normally the wrapper code is contained in the component module and is linked
against the corresponding C or C++ library. The advantage of a consolidated
module is that it allows all of the wrapped C or C++ libraries to be linked
against a single module. If the linking is done statically then deployment of
generated modules can be greatly simplified.</p>
<p>It follows that a component module can be built in one of two ways, as a
normal standalone module, or as a component of a consolidated module. When
building as a component the <ttclass="docutils literal"><spanclass="pre">-p</span></tt> command line option should be used to
specify the name of the consolidated module.</p>
<dlclass="directive">
<dtid="directive-%ConvertFromTypeCode">
<ttclass="descname">%ConvertFromTypeCode</tt><aclass="headerlink"href="#directive-%ConvertFromTypeCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%ConvertFromTypeCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the <aclass="reference internal"href="#directive-%MappedType"><ttclass="xref docutils literal"><spanclass="pre">%MappedType</span></tt></a> directive to
specify the handwritten code that converts an instance of a mapped type to a
Python object.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the instance of the mapped type to be converted. It
will never be zero as the conversion from zero to <ttclass="docutils literal"><spanclass="pre">Py_None</span></tt> is handled
before the handwritten code is called.</dd>
<dt>PyObject *sipTransferObj</dt>
<dd>This specifies any desired ownership changes to the returned object. If it
is <ttclass="docutils literal"><spanclass="pre">NULL</span></tt> then the ownership should be left unchanged. If it is
<ttclass="docutils literal"><spanclass="pre">Py_None</span></tt> then ownership should be transferred to Python. Otherwise
ownership should be transferred to C/C++ and the returned object associated
with <em>sipTransferObj</em>. The code can choose to interpret these changes in
any way. For example, if the code is converting a C++ container of wrapped
classes to a Python list it is likely that the ownership changes should be
made to each element of the list.</dd>
</dl>
<p>The handwritten code must explicitly return a <ttclass="docutils literal"><spanclass="pre">PyObject</span><spanclass="pre">*</span></tt>. If there was an
error then a Python exception must be raised and <ttclass="docutils literal"><spanclass="pre">NULL</span></tt> returned.</p>
<p>The following example converts a <ttclass="docutils literal"><spanclass="pre">TQList<TQWidget</span><spanclass="pre">*></span></tt> instance to a Python
list of <ttclass="docutils literal"><spanclass="pre">TQWidget</span></tt> instances:</p>
// There was an error so garbage collect the Python list.
Py_DECREF(l);
return NULL;
}
// Add the wrapper to the list.
PyList_SET_ITEM(l, i, wobj);
}
// Return the Python list.
return l;
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%ConvertToSubClassCode">
<ttclass="descname">%ConvertToSubClassCode</tt><aclass="headerlink"href="#directive-%ConvertToSubClassCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%ConvertToSubClassCode
<em>code</em>
%End
</pre>
<p>When SIP needs to wrap a C++ class instance it first checks to make sure it
hasn’t already done so. If it has then it just returns a new reference to the
corresponding Python object. Otherwise it creates a new Python object of the
appropriate type. In C++ a function may be defined to return an instance of a
certain class, but can often return a sub-class instead.</p>
<p>This directive is used to specify handwritten code that exploits any available
real-time type information (RTTI) to see if there is a more specific Python
type that can be used when wrapping the C++ instance. The RTTI may be
provided by the compiler or by the C++ instance itself.</p>
<p>The directive is included in the specification of one of the classes that the
handwritten code handles the type conversion for. It doesn’t matter which
one, but a sensible choice would be the one at the root of that class
hierarchy in the module.</p>
<p>Note that if a class hierarchy extends over a number of modules then this
directive should be used in each of those modules to handle the part of the
hierarchy defined in that module. SIP will ensure that the different pieces
of code are called in the right order to determine the most specific Python
type to use.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the C++ class instance.</dd>
<dt>void **sipCppRet</dt>
<dd>When the sub-class is derived from more than one super-class then it is
possible that the C++ address of the instance as the sub-class is
different to that of the super-class. If so, then this must be set to the
C++ address of the instance when cast (usually using <ttclass="docutils literal"><spanclass="pre">static_cast</span></tt>)
from the super-class to the sub-class.</dd>
<dt>const sipTypeDef *sipType</dt>
<dd>The handwritten code must set this to the SIP generated type structure
that corresponds to the class instance. (The type structure for class
<ttclass="docutils literal"><spanclass="pre">Klass</span></tt> is <ttclass="docutils literal"><spanclass="pre">sipType_Klass</span></tt>.) If the RTTI of the class instance isn’t
recognised then <ttclass="docutils literal"><spanclass="pre">sipType</span></tt> must be set to <ttclass="docutils literal"><spanclass="pre">NULL</span></tt>. The code doesn’t
have to recognise the exact class, only the most specific sub-class that
it can.</dd>
<dt>sipWrapperType *sipClass</dt>
<dd><pclass="first">The handwritten code must set this to the SIP generated Python type object
that corresponds to the class instance. (The type object for class
<ttclass="docutils literal"><spanclass="pre">Klass</span></tt> is <ttclass="docutils literal"><spanclass="pre">sipClass_Klass</span></tt>.) If the RTTI of the class instance isn’t
recognised then <ttclass="docutils literal"><spanclass="pre">sipClass</span></tt> must be set to <ttclass="docutils literal"><spanclass="pre">NULL</span></tt>. The code doesn’t
have to recognise the exact class, only the most specific sub-class that
it can.</p>
<pclass="last">This is deprecated from SIP v4.8. Instead you should use <ttclass="docutils literal"><spanclass="pre">sipType</span></tt>.</p>
</dd>
</dl>
<p>The handwritten code must not explicitly return.</p>
// Skip the remaining event types to keep the example short.
default:
// We don't recognise the type.
sipType = NULL;
}
%End
// The rest of the class specification.
};</pre>
</div>
<dlclass="directive">
<dtid="directive-%ConvertToTypeCode">
<ttclass="descname">%ConvertToTypeCode</tt><aclass="headerlink"href="#directive-%ConvertToTypeCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%ConvertToTypeCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify the handwritten code that converts a Python
object to a mapped type instance and to handle any ownership transfers. It is
used as part of the <aclass="reference internal"href="#directive-%MappedType"><ttclass="xref docutils literal"><spanclass="pre">%MappedType</span></tt></a> directive and as part of a class
specification. The code is also called to determine if the Python object is of
the correct type prior to conversion.</p>
<p>When used as part of a class specification it can automatically convert
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt>int *sipIsErr</dt>
<dd>If this is <ttclass="docutils literal"><spanclass="pre">NULL</span></tt> then the code is being asked to check the type of the
Python object. The check must not have any side effects. Otherwise the
code is being asked to convert the Python object and a non-zero value
should be returned through this pointer if an error occurred during the
conversion.</dd>
<dt>PyObject *sipPy</dt>
<dd>This is the Python object to be converted.</dd>
<dt><em>type</em> **sipCppPtr</dt>
<dd>This is a pointer through which the address of the mapped type instance (or
zero if appropriate) is returned. Its value is undefined if <ttclass="docutils literal"><spanclass="pre">sipIsErr</span></tt>
is <ttclass="docutils literal"><spanclass="pre">NULL</span></tt>.</dd>
<dt>PyObject *sipTransferObj</dt>
<dd>This specifies any desired ownership changes to <em>sipPy</em>. If it is <ttclass="docutils literal"><spanclass="pre">NULL</span></tt>
then the ownership should be left unchanged. If it is <ttclass="docutils literal"><spanclass="pre">Py_None</span></tt> then
ownership should be transferred to Python. Otherwise ownership should be
transferred to C/C++ and <em>sipPy</em> associated with <em>sipTransferObj</em>. The
code can choose to interpret these changes in any way.</dd>
</dl>
<p>The handwritten code must explicitly return an <ttclass="docutils literal"><spanclass="pre">int</span></tt> the meaning of which
depends on the value of <ttclass="docutils literal"><spanclass="pre">sipIsErr</span></tt>.</p>
<p>If <ttclass="docutils literal"><spanclass="pre">sipIsErr</span></tt> is <ttclass="docutils literal"><spanclass="pre">NULL</span></tt> then a non-zero value is returned if the Python
object has a type that can be converted to the mapped type. Otherwise zero is
returned.</p>
<p>If <ttclass="docutils literal"><spanclass="pre">sipIsErr</span></tt> is not <ttclass="docutils literal"><spanclass="pre">NULL</span></tt> then a combination of the following flags is
returned.</p>
<blockquote>
<ulclass="simple">
<li><ttclass="xref docutils literal"><spanclass="pre">SIP_TEMPORARY</span></tt> is set to indicate that the returned instance
is a temporary and should be released to avoid a memory leak.</li>
<li><ttclass="xref docutils literal"><spanclass="pre">SIP_DERIVED_CLASS</span></tt> is set to indicate that the type of the
// The instance should be regarded as temporary (and be destroyed as
// soon as it has been used) unless it has been transferred from
// Python. sipGetState() is a convenience function that implements
// this common transfer behaviour.
return sipGetState(sipTransferObj);
%End</pre>
</div>
<p>When used in a class specification the handwritten code replaces the code that
would normally be automatically generated. This means that the handwritten
code must also handle instances of the class itself and not just the additional
types that are being supported. This should be done by making calls to
<atitle="sipCanConvertToType"class="reference external"href="c_api.html#sipCanConvertToType"><ttclass="xref docutils literal"><spanclass="pre">sipCanConvertToType()</span></tt></a> to check the object type and
<atitle="sipConvertToType"class="reference external"href="c_api.html#sipConvertToType"><ttclass="xref docutils literal"><spanclass="pre">sipConvertToType()</span></tt></a> to convert the object. The
<atitle="SIP_NO_CONVERTORS"class="reference external"href="c_api.html#SIP_NO_CONVERTORS"><ttclass="xref docutils literal"><spanclass="pre">SIP_NO_CONVERTORS</span></tt></a> flag <em>must</em> be passed to both these functions to
prevent recursive calls to the handwritten code.</p>
<dlclass="directive">
<dtid="directive-%Copying">
<ttclass="descname">%Copying</tt><aclass="headerlink"href="#directive-%Copying"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Copying
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be included at
the start of all source files generated by SIP. It is normally used to
include copyright and licensing terms.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>%Copying
Copyright (c) 2009 Riverbank Computing Limited
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%DefaultEncoding">
<ttclass="descname">%DefaultEncoding</tt><aclass="headerlink"href="#directive-%DefaultEncoding"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%DefaultEncoding <em>string</em>
</pre>
<p>This directive is used to specify the default encoding used for <ttclass="docutils literal"><spanclass="pre">char</span></tt>,
<ttclass="docutils literal"><spanclass="pre">const</span><spanclass="pre">char</span></tt>, <ttclass="docutils literal"><spanclass="pre">char</span><spanclass="pre">*</span></tt> or <ttclass="docutils literal"><spanclass="pre">const</span><spanclass="pre">char</span><spanclass="pre">*</span></tt> values. The encoding can be
either <ttclass="docutils literal"><spanclass="pre">"ASCII"</span></tt>, <ttclass="docutils literal"><spanclass="pre">"Latin-1"</span></tt>, <ttclass="docutils literal"><spanclass="pre">"UTF-8"</span></tt> or <ttclass="docutils literal"><spanclass="pre">"None"</span></tt>. An encoding of
<ttclass="docutils literal"><spanclass="pre">"None"</span></tt> means that the value is unencoded. The default can be overridden
for a particular value using the <aclass="reference external"href="annotations.html#aanno-Encoding"><ttclass="xref docutils literal"><spanclass="pre">Encoding</span></tt></a> annotation. If the
directive is not specified then <ttclass="docutils literal"><spanclass="pre">"None"</span></tt> is used.</p>
<ttclass="descname">%DefaultMetatype</tt><aclass="headerlink"href="#directive-%DefaultMetatype"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%DefaultMetatype <em>dotted-name</em>
</pre>
<p>This directive is used to specify the Python type that should be used as the
meta-type for any C/C++ data type defined in the same module, and by importing
modules, that doesn’t have an explicit meta-type.</p>
<p>If this is not specified then <ttclass="docutils literal"><spanclass="pre">sip.wrappertype</span></tt> is used.</p>
<p>You can also use the <aclass="reference external"href="annotations.html#canno-Metatype"><ttclass="xref docutils literal"><spanclass="pre">Metatype</span></tt></a> class annotation to specify the
meta-type used by a particular C/C++ type.</p>
<p>See the section <aclass="reference external"href="using.html#ref-types-metatypes"><em>Types and Meta-types</em></a> for more details.</p>
<ttclass="descname">%DefaultSupertype</tt><aclass="headerlink"href="#directive-%DefaultSupertype"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%DefaultSupertype <em>dotted-name</em>
</pre>
<p>This directive is used to specify the Python type that should be used as the
super-type for any C/C++ data type defined in the same module that doesn’t have
an explicit super-type.</p>
<p>If this is not specified then <ttclass="docutils literal"><spanclass="pre">sip.wrapper</span></tt> is used.</p>
<p>You can also use the <aclass="reference external"href="annotations.html#canno-Supertype"><ttclass="xref docutils literal"><spanclass="pre">Supertype</span></tt></a> class annotation to specify the
super-type used by a particular C/C++ type.</p>
<p>See the section <aclass="reference external"href="using.html#ref-types-metatypes"><em>Types and Meta-types</em></a> for more details.</p>
<ttclass="descname">%Doc</tt><aclass="headerlink"href="#directive-%Doc"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Doc
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be extracted
by SIP when the <ttclass="docutils literal"><spanclass="pre">-d</span></tt> command line option is used. The directive can be
specified any number of times and SIP will concatenate all the separate pieces
of text in the order that it sees them.</p>
<p>Documentation that is specified using this directive is local to the module in
which it appears. It is ignored by modules that <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> it. Use
the <aclass="reference internal"href="#directive-%ExportedDoc"><ttclass="xref docutils literal"><spanclass="pre">%ExportedDoc</span></tt></a> directive for documentation that should be
included by all modules that <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> this one.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>%Doc
<h1>An Example</h1>
<p>
This fragment of documentation is HTML and is local to the module in
which it is defined.
</p>
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%Docstring">
<ttclass="descname">%Docstring</tt><aclass="headerlink"href="#directive-%Docstring"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Docstring
<em>text</em>
%End
</pre>
<p>
<spanclass="versionmodified">New in version 4.10.</span></p>
<p>This directive is used to specify explicit docstrings for classes, functions
and methods.</p>
<p>The docstring of a class is made up of the docstring specified for the class
itself, with the docstrings specified for each contructor appended.</p>
<p>The docstring of a function or method is made up of the concatenated docstrings
specified for each of the overloads.</p>
<p>Specifying an explicit docstring will prevent SIP from generating an automatic
docstring that describes the Python signature of a function or method overload.
This means that SIP will generate less informative exceptions (i.e. without a
full signature) when it fails to match a set of arguments to any function or
method overload.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>class Klass
{
%Docstring
This will be at the start of the class's docstring.
%End
public:
Klass();
%Docstring
This will be appended to the class's docstring.
%End
};</pre>
</div>
<dlclass="directive">
<dtid="directive-%End">
<ttclass="descname">%End</tt><aclass="headerlink"href="#directive-%End"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<p>This isn’t a directive in itself, but is used to terminate a number of
directives that allow a block of handwritten code or text to be specified.</p>
<dlclass="directive">
<dtid="directive-%Exception">
<ttclass="descname">%Exception</tt><aclass="headerlink"href="#directive-%Exception"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Exception <em>name</em> [(<em>base-exception)]
{
[*header-code</em>]
<em>raise-code</em>
};
</pre>
<p>This directive is used to define new Python exceptions, or to provide a stub
for existing Python exceptions. It allows handwritten code to be provided
that implements the translation between C++ exceptions and Python exceptions.
The arguments to <ttclass="docutils literal"><spanclass="pre">throw</span><spanclass="pre">()</span></tt> specifiers must either be names of classes or the
names of Python exceptions defined by this directive.</p>
<p><em>name</em> is the name of the exception.</p>
<p><em>base-exception</em> is the optional base exception. This may be either one of
the standard Python exceptions or one defined with a previous
<p><em>header-code</em> is the optional <aclass="reference internal"href="#directive-%TypeHeaderCode"><ttclass="xref docutils literal"><spanclass="pre">%TypeHeaderCode</span></tt></a> used to specify any
external interface to the exception being defined.</p>
<p><em>raise-code</em> is the <aclass="reference internal"href="#directive-%RaiseCode"><ttclass="xref docutils literal"><spanclass="pre">%RaiseCode</span></tt></a> used to specify the handwritten
code that converts a reference to the C++ exception to the Python exception.</p>
<p>An exception may be annotated with <aclass="reference external"href="annotations.html#xanno-Default"><ttclass="xref docutils literal"><spanclass="pre">Default</span></tt></a> to specify that it should
be caught by default if there is no <ttclass="docutils literal"><spanclass="pre">throw</span></tt> clause.</p>
<dlclass="directive">
<dtid="directive-%ExportedDoc">
<ttclass="descname">%ExportedDoc</tt><aclass="headerlink"href="#directive-%ExportedDoc"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%ExportedDoc
<em>text</em>
%End
</pre>
<p>This directive is used to specify some arbitrary text that will be extracted
by SIP when the <ttclass="docutils literal"><spanclass="pre">-d</span></tt> command line option is used. The directive can be
specified any number of times and SIP will concatenate all the separate pieces
of text in the order that it sees them.</p>
<p>Documentation that is specified using this directive will also be included by
modules that <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> it.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>%ExportedDoc
==========
An Example
==========
This fragment of documentation is reStructuredText and will appear in the
module in which it is defined and all modules that %Import it.
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%ExportedHeaderCode">
<ttclass="descname">%ExportedHeaderCode</tt><aclass="headerlink"href="#directive-%ExportedHeaderCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%ExportedHeaderCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code, typically the declarations
of types, that is placed in a header file that is included by all generated
code for all modules. It should not include function declarations because
Python modules should not explicitly call functions in another Python module.</p>
<p>See also <aclass="reference internal"href="#directive-%ModuleCode"><ttclass="xref docutils literal"><spanclass="pre">%ModuleCode</span></tt></a> and <aclass="reference internal"href="#directive-%ModuleHeaderCode"><ttclass="xref docutils literal"><spanclass="pre">%ModuleHeaderCode</span></tt></a>.</p>
<dlclass="directive">
<dtid="directive-%Feature">
<ttclass="descname">%Feature</tt><aclass="headerlink"href="#directive-%Feature"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Feature <em>name</em>
</pre>
<p>This directive is used to declare a feature. Features (along with
<aclass="reference internal"href="#directive-%Platforms"><ttclass="xref docutils literal"><spanclass="pre">%Platforms</span></tt></a> and <aclass="reference internal"href="#directive-%Timeline"><ttclass="xref docutils literal"><spanclass="pre">%Timeline</span></tt></a>) are used by the
<aclass="reference internal"href="#directive-%If"><ttclass="xref docutils literal"><spanclass="pre">%If</span></tt></a> directive to control whether or not parts of a specification
are processed or ignored.</p>
<p>Features are mutually independent of each other - any combination of features
may be enabled or disable. By default all features are enabled. The SIP
<ttclass="docutils literal"><spanclass="pre">-x</span></tt> command line option is used to disable a feature.</p>
<p>If a feature is enabled then SIP will automatically generate a corresponding C
preprocessor symbol for use by handwritten code. The symbol is the name of
the feature prefixed by <ttclass="docutils literal"><spanclass="pre">SIP_FEATURE_</span></tt>.</p>
<ttclass="descname">%GCClearCode</tt><aclass="headerlink"href="#directive-%GCClearCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%GCClearCode
<em>code</em>
%End
</pre>
<p>Python has a cyclic garbage collector which can identify and release unneeded
objects even when their reference counts are not zero. If a wrapped C
structure or C++ class keeps its own reference to a Python object then, if the
garbage collector is to do its job, it needs to provide some handwritten code
to traverse and potentially clear those embedded references.</p>
<p>See the section <em>Supporting cyclic garbage collection</em> in <aclass="reference external"href="http://www.python.org/dev/doc/devel/ext/">Embedding and
Extending the Python Interpreter</a>
for the details.</p>
<p>This directive is used to specify the code that clears any embedded references.
(See <aclass="reference internal"href="#directive-%GCTraverseCode"><ttclass="xref docutils literal"><spanclass="pre">%GCTraverseCode</span></tt></a> for specifying the code that traverses any
embedded references.)</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to the result to be returned.</dd>
<ttclass="descname">%GCTraverseCode</tt><aclass="headerlink"href="#directive-%GCTraverseCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%GCTraverseCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify the code that traverses any embedded
references for Python’s cyclic garbage collector. (See
<aclass="reference internal"href="#directive-%GCClearCode"><ttclass="xref docutils literal"><spanclass="pre">%GCClearCode</span></tt></a> for a full explanation.)</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class.</dd>
<dt>visitproc sipVisit</dt>
<dd>This is the visit function provided by the garbage collector.</dd>
<dt>void *sipArg</dt>
<dd>This is the argument to the visit function provided by the garbage
collector.</dd>
<dt>int sipRes</dt>
<dd>The handwritten code should set this to the result to be returned.</dd>
<p>This directive is used in conjunction with features (see
<aclass="reference internal"href="#directive-%Feature"><ttclass="xref docutils literal"><spanclass="pre">%Feature</span></tt></a>), platforms (see <aclass="reference internal"href="#directive-%Platforms"><ttclass="xref docutils literal"><spanclass="pre">%Platforms</span></tt></a>) and versions
(see <aclass="reference internal"href="#directive-%Timeline"><ttclass="xref docutils literal"><spanclass="pre">%Timeline</span></tt></a>) to control whether or not parts of a specification
are processed or not.</p>
<p>A <em>range</em> of versions means all versions starting with the lower bound up to
but excluding the upper bound. If the lower bound is omitted then it is
interpreted as being before the earliest version. If the upper bound is
omitted then it is interpreted as being after the latest version.</p>
<ttclass="descname">%Import</tt><aclass="headerlink"href="#directive-%Import"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Import <em>filename</em>
</pre>
<p>This directive is used to import the specification of another module. This is
needed if the current module makes use of any types defined in the imported
module, e.g. as an argument to a function, or to sub-class.</p>
<p>If <em>filename</em> cannot be opened then SIP prepends <em>filename</em> with the name of
the directory containing the current specification file (i.e. the one
containing the <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> directive) and tries again. If this also
fails then SIP prepends <em>filename</em> with each of the directories, in turn,
specified by the <ttclass="docutils literal"><spanclass="pre">-I</span></tt> command line option.</p>
<ttclass="descname">%Include</tt><aclass="headerlink"href="#directive-%Include"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Include <em>filename</em>
</pre>
<p>This directive is used to include contents of another file as part of the
specification of the current module. It is the equivalent of the C
preprocessor’s <ttclass="docutils literal"><spanclass="pre">#include</span></tt> directive and is used to structure a large module
specification into manageable pieces.</p>
<p><aclass="reference internal"href="#directive-%Include"><ttclass="xref docutils literal"><spanclass="pre">%Include</span></tt></a> follows the same search process as <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a>
<ttclass="descname">%InitialisationCode</tt><aclass="headerlink"href="#directive-%InitialisationCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%InitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
in the generated module initialisation code after the SIP module has been
imported but before the module itself has been initialised.</p>
<p>It is typically used to call <atitle="sipRegisterPyType"class="reference external"href="c_api.html#sipRegisterPyType"><ttclass="xref docutils literal"><spanclass="pre">sipRegisterPyType()</span></tt></a>.</p>
// The code will be executed when the module is first imported, after
// the SIP module has been imported, but before other module-specific
// initialisation has been completed.
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%License">
<ttclass="descname">%License</tt><aclass="headerlink"href="#directive-%License"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%License /<em>license-annotations</em>/
</pre>
<p>This directive is used to specify the contents of an optional license
dictionary. The license dictionary is called <ttclass="xref docutils literal"><spanclass="pre">__license__</span></tt> and is stored
in the module dictionary. The elements of the dictionary are specified using
the <aclass="reference external"href="annotations.html#lanno-Licensee"><ttclass="xref docutils literal"><spanclass="pre">Licensee</span></tt></a>, <aclass="reference external"href="annotations.html#lanno-Signature"><ttclass="xref docutils literal"><spanclass="pre">Signature</span></tt></a>, <aclass="reference external"href="annotations.html#lanno-Timestamp"><ttclass="xref docutils literal"><spanclass="pre">Timestamp</span></tt></a> and <aclass="reference external"href="annotations.html#lanno-Type"><ttclass="xref docutils literal"><spanclass="pre">Type</span></tt></a>
annotations. Only the <aclass="reference external"href="annotations.html#lanno-Type"><ttclass="xref docutils literal"><spanclass="pre">Type</span></tt></a> annotation is compulsory.</p>
<p>Note that this directive isn’t an attempt to impose any licensing restrictions
on a module. It is simply a method for easily embedding licensing information
in a module so that it is accessible to Python scripts.</p>
<ttclass="descname">%MappedType</tt><aclass="headerlink"href="#directive-%MappedType"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
template<<em>type-list</em>>
%MappedType <em>type</em>
{
[<em>header-code</em>]
[<em>convert-to-code</em>]
[<em>convert-from-code</em>]
};
%MappedType <em>type</em>
{
[<em>header-code</em>]
[<em>convert-to-code</em>]
[<em>convert-from-code</em>]
};
</pre>
<p>This directive is used to define an automatic mapping between a C or C++ type
and a Python type. It can be used as part of a template, or to map a specific
type.</p>
<p>When used as part of a template <em>type</em> cannot itself refer to a template. Any
occurrences of any of the type names (but not any <ttclass="docutils literal"><spanclass="pre">*</span></tt> or <ttclass="docutils literal"><spanclass="pre">&</span></tt>) in
<em>type-list</em> will be replaced by the actual type names used when the template is
instantiated. Template mapped types are instantiated automatically as required
(unlike template classes which are only instantiated using <ttclass="docutils literal"><spanclass="pre">typedef</span></tt>).</p>
<p>Any explicit mapped type will be used in preference to any template that maps
the same type, ie. a template will not be automatically instantiated if there
is an explicit mapped type.</p>
<p><em>header-code</em> is the <aclass="reference internal"href="#directive-%TypeHeaderCode"><ttclass="xref docutils literal"><spanclass="pre">%TypeHeaderCode</span></tt></a> used to specify the library
interface to the type being mapped.</p>
<p><em>convert-to-code</em> is the <aclass="reference internal"href="#directive-%ConvertToTypeCode"><ttclass="xref docutils literal"><spanclass="pre">%ConvertToTypeCode</span></tt></a> used to specify the
handwritten code that converts a Python object to an instance of the mapped
type.</p>
<p><em>convert-from-code</em> is the <aclass="reference internal"href="#directive-%ConvertFromTypeCode"><ttclass="xref docutils literal"><spanclass="pre">%ConvertFromTypeCode</span></tt></a> used to specify
the handwritten code that converts an instance of the mapped type to a Python
<p>Using this we can use, for example, <ttclass="docutils literal"><spanclass="pre">TQList<TQObject</span><spanclass="pre">*></span></tt> throughout the
<ttclass="descname">%MethodCode</tt><aclass="headerlink"href="#directive-%MethodCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%MethodCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the specification of a global function, class
method, operator, constructor or destructor to specify handwritten code that
replaces the normally generated call to the function being wrapped. It is
usually used to handle argument types and results that SIP cannot deal with
automatically.</p>
<p>Normally the specified code is embedded in-line after the function’s arguments
have been successfully converted from Python objects to their C or C++
equivalents. In this case the specified code must not include any <ttclass="docutils literal"><spanclass="pre">return</span></tt>
statements.</p>
<p>However if the <aclass="reference external"href="annotations.html#fanno-NoArgParser"><ttclass="xref docutils literal"><spanclass="pre">NoArgParser</span></tt></a> annotation has been used then the specified
code is also responsible for parsing the arguments. No other code is generated
by SIP and the specified code must include a <ttclass="docutils literal"><spanclass="pre">return</span></tt> statement.</p>
<p>In the context of a destructor the specified code is embedded in-line in the
Python type’s deallocation function. Unlike other contexts it supplements
rather than replaces the normally generated code, so it must not include code
to return the C structure or C++ class instance to the heap. The code is only
called if ownership of the structure or class is with Python.</p>
<p>The specified code must also handle the Python Global Interpreter Lock (GIL).
If compatibility with SIP v3.x is required then the GIL must be released
immediately before the C++ call and reacquired immediately afterwards as shown
<p>If compatibility with SIP v3.x is not required then this is optional but
should be done if the C++ function might block the current thread or take a
significant amount of time to execute. (See <aclass="reference external"href="using.html#ref-gil"><em>The Python Global Interpreter Lock</em></a> and the
<aclass="reference external"href="annotations.html#fanno-ReleaseGIL"><ttclass="xref docutils literal"><spanclass="pre">ReleaseGIL</span></tt></a> and <aclass="reference external"href="annotations.html#fanno-HoldGIL"><ttclass="xref docutils literal"><spanclass="pre">HoldGIL</span></tt></a> annotations.)</p>
<p>If the <aclass="reference external"href="annotations.html#fanno-NoArgParser"><ttclass="xref docutils literal"><spanclass="pre">NoArgParser</span></tt></a> annotation has not been used then the following
variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> a0</dt>
<dd><pclass="first">There is a variable for each argument of the Python signature (excluding
any <ttclass="docutils literal"><spanclass="pre">self</span></tt> argument) named <ttclass="docutils literal"><spanclass="pre">a0</span></tt>, <ttclass="docutils literal"><spanclass="pre">a1</span></tt>, etc. The <em>type</em> of the
variable is the same as the type defined in the specification with the
following exceptions:</p>
<ulclass="simple">
<li>if the argument is only used to return a value (e.g. it is an <ttclass="docutils literal"><spanclass="pre">int</span><spanclass="pre">*</span></tt>
without an <aclass="reference external"href="annotations.html#aanno-In"><ttclass="xref docutils literal"><spanclass="pre">In</span></tt></a> annotation) then the type has one less level of
indirection (e.g. it will be an <ttclass="docutils literal"><spanclass="pre">int</span></tt>)</li>
<li>if the argument is a structure or class (or a reference or a pointer to a
structure or class) then <em>type</em> will always be a pointer to the structure
or class.</li>
</ul>
<pclass="last">Note that handwritten code for destructors never has any arguments.</p>
</dd>
<dt>PyObject *a0Wrapper</dt>
<dd>This variable is made available only if the <aclass="reference external"href="annotations.html#aanno-GetWrapper"><ttclass="xref docutils literal"><spanclass="pre">GetWrapper</span></tt></a> annotation
is specified for the corresponding argument. The variable is a pointer to
the Python object that wraps the argument.</dd>
<dt><em>type</em> *sipCpp</dt>
<dd><pclass="first">If the directive is used in the context of a class constructor then this
must be set by the handwritten code to the constructed instance. If it is
set to <ttclass="docutils literal"><spanclass="pre">0</span></tt> and no Python exception is raised then SIP will continue to
try other Python signatures.</p>
<p>If the directive is used in the context of a method (but not the standard
binary operator methods, e.g. <ttclass="xref docutils literal"><spanclass="pre">__add__()</span></tt>) or a destructor then this is
a pointer to the C structure or C++ class instance.</p>
<p>Its <em>type</em> is a pointer to the structure or class.</p>
<pclass="last">Standard binary operator methods follow the same convention as global
functions and instead define two arguments called <ttclass="docutils literal"><spanclass="pre">a0</span></tt> and <ttclass="docutils literal"><spanclass="pre">a1</span></tt>.</p>
</dd>
<dt>sipErrorState sipError</dt>
<dd><pclass="first">The handwritten code should set this to either <ttclass="docutils literal"><spanclass="pre">sipErrorContinue</span></tt> or
<ttclass="docutils literal"><spanclass="pre">sipErrorFail</span></tt>, and raise an appropriate Python exception, if an error
is detected. Its initial value will be <ttclass="docutils literal"><spanclass="pre">sipErrorNone</span></tt>.</p>
<p>When <ttclass="docutils literal"><spanclass="pre">sipErrorContinue</span></tt> is used, SIP will remember the exception as the
reason why the particular overloaded callable could not be invoked. It
will then continue to try the next overloaded callable. It is typically
used by code that needs to do additional type checking of the callable’s
arguments.</p>
<p>When <ttclass="docutils literal"><spanclass="pre">sipErrorFail1</span></tt> is used, SIP will report the exception immediately
and will not attempt to invoke other overloaded callables.</p>
<pclass="last"><ttclass="docutils literal"><spanclass="pre">sipError</span></tt> is not provided for destructors.</p>
</dd>
<dt>int sipIsErr</dt>
<dd><pclass="first">The handwritten code should set this to a non-zero value, and raise an
appropriate Python exception, if an error is detected. This is the
equivalent of setting <ttclass="docutils literal"><spanclass="pre">sipError</span></tt> to <ttclass="docutils literal"><spanclass="pre">sipErrorFail</span></tt>. Its initial value
will be <ttclass="docutils literal"><spanclass="pre">0</span></tt>.</p>
<pclass="last"><ttclass="docutils literal"><spanclass="pre">sipIsErr</span></tt> is not provided for destructors.</p>
</dd>
<dt><em>type</em> sipRes</dt>
<dd><pclass="first">The handwritten code should set this to the result to be returned. The
<em>type</em> of the variable is the same as the type defined in the Python
signature in the specification with the following exception:</p>
<ulclass="simple">
<li>if the argument is a structure or class (or a reference or a pointer to a
structure or class) then <em>type</em> will always be a pointer to the structure
or class.</li>
</ul>
<pclass="last"><ttclass="docutils literal"><spanclass="pre">sipRes</span></tt> is not provided for inplace operators (e.g. <ttclass="docutils literal"><spanclass="pre">+=</span></tt> or
<ttclass="xref docutils literal"><spanclass="pre">__imul__()</span></tt>) as their results are handled automatically, nor for class
constructors or destructors.</p>
</dd>
<dt>PyObject *sipSelf</dt>
<dd>If the directive is used in the context of a class constructor, destructor
or method then this is the Python object that wraps the structure or class
instance, i.e. <ttclass="docutils literal"><spanclass="pre">self</span></tt>.</dd>
<dt>bool sipSelfWasArg</dt>
<dd><pclass="first">This is only made available for non-abstract, virtual methods. It is set
if <ttclass="docutils literal"><spanclass="pre">self</span></tt> was explicitly passed as the first argument of the method
rather than being bound to the method. In other words, the call was:</p>
<p>If the <aclass="reference external"href="annotations.html#fanno-NoArgParser"><ttclass="xref docutils literal"><spanclass="pre">NoArgParser</span></tt></a> annotation has been used then only the following
variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt>PyObject *sipArgs</dt>
<dd>This is the tuple of arguments.</dd>
<dt>PyObject *sipKwds</dt>
<dd>This is the dictionary of keyword arguments.</dd>
</dl>
<p>The following is a complete example:</p>
<divclass="highlight-python"><pre>class Klass
{
public:
virtual int foo(SIP_PYTUPLE);
%MethodCode
// The C++ API takes a 2 element array of integers but passing a
// two element tuple is more Pythonic.
int iarr[2];
if (PyArg_ParseTuple(a0, "ii", &iarr[0], &iarr[1]))
// PyArg_ParseTuple() will have raised the exception.
sipIsErr = 1;
}
%End
};</pre>
</div>
<p>As the example is a virtual method <aclass="footnote-reference"href="#id2"id="id1">[1]</a>, note the use of <ttclass="docutils literal"><spanclass="pre">sipSelfWasArg</span></tt> to
determine exactly which implementation of <ttclass="docutils literal"><spanclass="pre">foo()</span></tt> to call.</p>
<p>If a method is in the <ttclass="docutils literal"><spanclass="pre">protected</span></tt> section of a C++ class then SIP generates
helpers that provide access to method. However, these are not available if
the Python module is being built with <ttclass="docutils literal"><spanclass="pre">protected</span></tt> redefined as <ttclass="docutils literal"><spanclass="pre">public</span></tt>.</p>
<p>The following pattern should be used to cover all possibilities:</p>
<tr><tdclass="label"><aclass="fn-backref"href="#id1">[1]</a></td><td>See <aclass="reference internal"href="#directive-%VirtualCatcherCode"><ttclass="xref docutils literal"><spanclass="pre">%VirtualCatcherCode</span></tt></a> for a description of how SIP
generated code handles the reimplementation of C++ virtual methods in
Python.</td></tr>
</tbody>
</table>
<dlclass="directive">
<dtid="directive-%Module">
<ttclass="descname">%Module</tt><aclass="headerlink"href="#directive-%Module"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Module <em>name</em> [<em>version</em>]
</pre>
<p>This directive is used to identify that the library being wrapped is a C++
library and to define the name of the module and it’s optional version number.</p>
<p>The name may contain periods to specify that the module is part of a Python
package.</p>
<p>The optional version number is useful if you (or others) might create other
modules that build on this module, i.e. if another module might
<aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> this module. Under the covers, a module exports an API
that is used by modules that <aclass="reference internal"href="#directive-%Import"><ttclass="xref docutils literal"><spanclass="pre">%Import</span></tt></a> it and the API is given a
version number. A module built on that module knows the version number of the
API that it is expecting. If, when the modules are imported at run-time, the
version numbers do not match then a Python exception is raised. The dependent
module must then be re-built using the correct specification files for the base
module.</p>
<p>The version number should be incremented whenever a module is changed. Some
changes don’t affect the exported API, but it is good practice to change the
<p>See also <aclass="reference internal"href="#directive-%ExportedHeaderCode"><ttclass="xref docutils literal"><spanclass="pre">%ExportedHeaderCode</span></tt></a> and <aclass="reference internal"href="#directive-%ModuleCode"><ttclass="xref docutils literal"><spanclass="pre">%ModuleCode</span></tt></a>.</p>
<dlclass="directive">
<dtid="directive-%OptionalInclude">
<ttclass="descname">%OptionalInclude</tt><aclass="headerlink"href="#directive-%OptionalInclude"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%OptionalInclude <em>filename</em>
</pre>
<p>This directive is identical to the <aclass="reference internal"href="#directive-%Include"><ttclass="xref docutils literal"><spanclass="pre">%Include</span></tt></a> directive except that
SIP silently continues processing if <em>filename</em> could not be opened.</p>
<p>Note that SIP works around the Python limitation that prevents nested types
being pickled.</p>
<p>Both named and unnamed enums can be pickled automatically without providing any
handwritten code.</p>
<dlclass="directive">
<dtid="directive-%Platforms">
<ttclass="descname">%Platforms</tt><aclass="headerlink"href="#directive-%Platforms"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Platforms {<em>name</em><em>name</em> ...}
</pre>
<p>This directive is used to declare a set of platforms. Platforms (along with
<aclass="reference internal"href="#directive-%Feature"><ttclass="xref docutils literal"><spanclass="pre">%Feature</span></tt></a> and <aclass="reference internal"href="#directive-%Timeline"><ttclass="xref docutils literal"><spanclass="pre">%Timeline</span></tt></a>) are used by the
<aclass="reference internal"href="#directive-%If"><ttclass="xref docutils literal"><spanclass="pre">%If</span></tt></a> directive to control whether or not parts of a specification
are processed or ignored.</p>
<p>Platforms are mutually exclusive - only one platform can be enabled at a time.
By default all platforms are disabled. The SIP <ttclass="docutils literal"><spanclass="pre">-t</span></tt> command line option is
<ttclass="descname">%PostInitialisationCode</tt><aclass="headerlink"href="#directive-%PostInitialisationCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%PostInitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
at the very end of the generated module initialisation code.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt>PyObject *sipModule</dt>
<dd>This is the module object returned by <ttclass="docutils literal"><spanclass="pre">Py_InitModule()</span></tt>.</dd>
<dt>PyObject *sipModuleDict</dt>
<dd>This is the module’s dictionary object returned by <ttclass="docutils literal"><spanclass="pre">Py_ModuleGetDict()</span></tt>.</dd>
// The code will be executed when the module is first imported and
// after all other initialisation has been completed.
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%PreInitialisationCode">
<ttclass="descname">%PreInitialisationCode</tt><aclass="headerlink"href="#directive-%PreInitialisationCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%PreInitialisationCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that is embedded in-line
at the very start of the generated module initialisation code.</p>
// The code will be executed when the module is first imported and
// before other initialisation has been completed.
%End</pre>
</div>
<dlclass="directive">
<dtid="directive-%RaiseCode">
<ttclass="descname">%RaiseCode</tt><aclass="headerlink"href="#directive-%RaiseCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%RaiseCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the definition of an exception using the
<aclass="reference internal"href="#directive-%Exception"><ttclass="xref docutils literal"><spanclass="pre">%Exception</span></tt></a> directive to specify handwritten code that raises a
Python exception when a C++ exception has been caught. The code is embedded
in-line as the body of a C++ <ttclass="docutils literal"><spanclass="pre">catch</span><spanclass="pre">()</span></tt> clause.</p>
<p>The specified code must handle the Python Global Interpreter Lock (GIL) if
necessary. The GIL must be acquired before any calls to the Python API and
released after the last call as shown in this example fragment:</p>
<p>Finally, the specified code must not include any <ttclass="docutils literal"><spanclass="pre">return</span></tt> statements.</p>
<p>The following variable is made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em>&sipExceptionRef</dt>
<dd>This is a reference to the caught C++ exception. The <em>type</em> of the
reference is the same as the type defined in the <ttclass="docutils literal"><spanclass="pre">throw</span><spanclass="pre">()</span></tt> specifier.</dd>
</dl>
<p>See the <aclass="reference internal"href="#directive-%Exception"><ttclass="xref docutils literal"><spanclass="pre">%Exception</span></tt></a> directive for an example.</p>
<dlclass="directive">
<dtid="directive-%SetCode">
<ttclass="descname">%SetCode</tt><aclass="headerlink"href="#directive-%SetCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%SetCode
<em>code</em>
%End
</pre>
<p>This directive is used after the declaration of a C++ class variable or C
structure member to specify handwritten code to convert it from a Python
object. It is usually used to handle types that SIP cannot deal with
automatically.</p>
<p>The following variables are made available to the handwritten code:</p>
<dlclass="docutils">
<dt><em>type</em> *sipCpp</dt>
<dd>This is a pointer to the structure or class instance. Its <em>type</em> is a
pointer to the structure or class. It is not made available if the
variable being wrapped is a static class variable.</dd>
<dt>int sipErr</dt>
<dd>If the conversion failed then the handwritten code should raise a Python
exception and set this to a non-zero value. Its initial value will be
automatically set to zero.</dd>
<dt>PyObject *sipPy</dt>
<dd>This is the Python object that the handwritten code should convert.</dd>
<dt>PyObject *sipPyType</dt>
<dd>If the variable being wrapped is a static class variable then this is the
Python type object of the class from which the variable was referenced
(<em>not</em> the class in which it is defined). It may be safely cast to a
PyTypeObject * or a sipWrapperType *.</dd>
</dl>
<p>See the <aclass="reference internal"href="#directive-%GetCode"><ttclass="xref docutils literal"><spanclass="pre">%GetCode</span></tt></a> directive for an example.</p>
<dlclass="directive">
<dtid="directive-%Timeline">
<ttclass="descname">%Timeline</tt><aclass="headerlink"href="#directive-%Timeline"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%Timeline {<em>name</em><em>name</em> ...}
</pre>
<p>This directive is used to declare a set of versions released over a period of
time. Versions (along with <aclass="reference internal"href="#directive-%Feature"><ttclass="xref docutils literal"><spanclass="pre">%Feature</span></tt></a> and <aclass="reference internal"href="#directive-%Platforms"><ttclass="xref docutils literal"><spanclass="pre">%Platforms</span></tt></a>)
are used by the <aclass="reference internal"href="#directive-%If"><ttclass="xref docutils literal"><spanclass="pre">%If</span></tt></a> directive to control whether or not parts of a
specification are processed or ignored.</p>
<p>Versions are mutually exclusive - only one version can be enabled at a time.
By default all versions are disabled. The SIP <ttclass="docutils literal"><spanclass="pre">-t</span></tt> command line option is
<p><aclass="reference internal"href="#directive-%Timeline"><ttclass="xref docutils literal"><spanclass="pre">%Timeline</span></tt></a> can be used any number of times in a module to allow
multiple libraries to be wrapped in the same module.</p>
<dlclass="directive">
<dtid="directive-%TypeCode">
<ttclass="descname">%TypeCode</tt><aclass="headerlink"href="#directive-%TypeCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%TypeCode
<em>code</em>
%End
</pre>
<p>This directive is used as part of the specification of a C structure or a C++
class to specify handwritten code, typically the implementations of utility
functions, that can be called by other handwritten code in the structure or
class.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>class Klass
{
%TypeCode
// Print an instance on stderr for debugging purposes.
static void dump_klass(const Klass *k)
{
fprintf(stderr,"Klass %s at %p\n", k->name(), k);
}
%End
// The rest of the class specification.
};</pre>
</div>
<p>Because the scope of the code is normally within the generated file that
implements the type, any utility functions would normally be declared
<ttclass="docutils literal"><spanclass="pre">static</span></tt>. However a naming convention should still be adopted to prevent
clashes of function names within a module in case the SIP <ttclass="docutils literal"><spanclass="pre">-j</span></tt> command line
option is used.</p>
<dlclass="directive">
<dtid="directive-%TypeHeaderCode">
<ttclass="descname">%TypeHeaderCode</tt><aclass="headerlink"href="#directive-%TypeHeaderCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%TypeHeaderCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that defines the interface
to a C or C++ type being wrapped, either a structure, a class, or a template.
It is used within a class definition or a <aclass="reference internal"href="#directive-%MappedType"><ttclass="xref docutils literal"><spanclass="pre">%MappedType</span></tt></a> directive.</p>
<p>Normally <em>code</em> will be a pre-processor <ttclass="docutils literal"><spanclass="pre">#include</span></tt> statement.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>// Wrap the Klass class.
class Klass
{
%TypeHeaderCode
#include <klass.h>
%End
// The rest of the class specification.
};</pre>
</div>
<dlclass="directive">
<dtid="directive-%UnitCode">
<ttclass="descname">%UnitCode</tt><aclass="headerlink"href="#directive-%UnitCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%UnitCode
<em>code</em>
%End
</pre>
<p>This directive is used to specify handwritten code that it included at the very
start of a generated compilation unit (ie. C or C++ source file). It is
typically used to <ttclass="docutils literal"><spanclass="pre">#include</span></tt> a C++ precompiled header file.</p>
<dlclass="directive">
<dtid="directive-%VirtualCatcherCode">
<ttclass="descname">%VirtualCatcherCode</tt><aclass="headerlink"href="#directive-%VirtualCatcherCode"title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<preclass="literal-block">
%VirtualCatcherCode
<em>code</em>
%End
</pre>
<p>For most classes there are corresponding <aclass="reference external"href="c_api.html#ref-derived-classes"><em>generated derived classes</em></a> that contain reimplementations of the class’s virtual
methods. These methods (which SIP calls catchers) determine if there is a
corresponding Python reimplementation and call it if so. If there is no Python
reimplementation then the method in the original class is called instead.</p>
<p>This directive is used to specify handwritten code that replaces the normally
generated call to the Python reimplementation and the handling of any returned
results. It is usually used to handle argument types and results that SIP
cannot deal with automatically.</p>
<p>This directive can also be used in the context of a class destructor to
specify handwritten code that is embedded in-line in the internal derived
class’s destructor.</p>
<p>In the context of a method the Python Global Interpreter Lock (GIL) is
automatically acquired before the specified code is executed and automatically
released afterwards.</p>
<p>In the context of a destructor the specified code must handle the GIL. The
GIL must be acquired before any calls to the Python API and released after the
<p>The following variables are made available to the handwritten code in the
context of a method:</p>
<dlclass="docutils">
<dt><em>type</em> a0</dt>
<dd>There is a variable for each argument of the C++ signature named <ttclass="docutils literal"><spanclass="pre">a0</span></tt>,
<ttclass="docutils literal"><spanclass="pre">a1</span></tt>, etc. The <em>type</em> of the variable is the same as the type defined in
the specification.</dd>
<dt>int a0Key</dt>
<dd>There is a variable for each argument of the C++ signature that has a type
where it is important to ensure that the corresponding Python object is not
garbage collected too soon. This only applies to output arguments that
return <ttclass="docutils literal"><spanclass="pre">'\0'</span></tt> terminated strings. The variable would normally be passed
to <atitle="sipParseResult"class="reference external"href="c_api.html#sipParseResult"><ttclass="xref docutils literal"><spanclass="pre">sipParseResult()</span></tt></a> using either the <ttclass="docutils literal"><spanclass="pre">A</span></tt> or <ttclass="docutils literal"><spanclass="pre">B</span></tt> format
characters.</dd>
<dt>int sipIsErr</dt>
<dd>The handwritten code should set this to a non-zero value, and raise an
appropriate Python exception, if an error is detected.</dd>
<dt>PyObject *sipMethod</dt>
<dd>This object is the Python reimplementation of the virtual C++ method. It
is normally passed to <atitle="sipCallMethod"class="reference external"href="c_api.html#sipCallMethod"><ttclass="xref docutils literal"><spanclass="pre">sipCallMethod()</span></tt></a>.</dd>
<dt><em>type</em> sipRes</dt>
<dd>The handwritten code should set this to the result to be returned. The
<em>type</em> of the variable is the same as the type defined in the C++ signature
in the specification.</dd>
<dt>int sipResKey</dt>
<dd>This variable is only made available if the result has a type where it is
important to ensure that the corresponding Python object is not garbage
collected too soon. This only applies to <ttclass="docutils literal"><spanclass="pre">'\0'</span></tt> terminated strings. The
variable would normally be passed to <atitle="sipParseResult"class="reference external"href="c_api.html#sipParseResult"><ttclass="xref docutils literal"><spanclass="pre">sipParseResult()</span></tt></a> using either
the <ttclass="docutils literal"><spanclass="pre">A</span></tt> or <ttclass="docutils literal"><spanclass="pre">B</span></tt> format characters.</dd>
<dt>sipSimpleWrapper *sipPySelf</dt>
<dd>This variable is only made available if either the <ttclass="docutils literal"><spanclass="pre">a0Key</span></tt> or
<ttclass="docutils literal"><spanclass="pre">sipResKey</span></tt> are made available. It defines the context within which keys
are unique. The variable would normally be passed to
<atitle="sipParseResult"class="reference external"href="c_api.html#sipParseResult"><ttclass="xref docutils literal"><spanclass="pre">sipParseResult()</span></tt></a> using the <ttclass="docutils literal"><spanclass="pre">S</span></tt> format character.</dd>
</dl>
<p>No variables are made available in the context of a destructor.</p>
<p>For example:</p>
<divclass="highlight-python"><pre>class Klass
{
public:
virtual int foo(SIP_PYTUPLE) [int (int *)];
%MethodCode
// The C++ API takes a 2 element array of integers but passing a
// two element tuple is more Pythonic.
int iarr[2];
if (PyArg_ParseTuple(a0, "ii", &iarr[0], &iarr[1]))
{
Py_BEGIN_ALLOW_THREADS
sipRes = sipCpp->Klass::foo(iarr);
Py_END_ALLOW_THREADS
}
else
{
// PyArg_ParseTuple() will have raised the exception.
sipIsErr = 1;
}
%End
%VirtualCatcherCode
// Convert the 2 element array of integers to the two element
// tuple.
PyObject *result;
result = sipCallMethod(&sipIsErr, sipMethod, "ii", a0[0], a0[1]);