C API for Handwritten Code¶
In this section we describe the API that can be used by handwritten code in specification files.
-
SIP_API_MAJOR_NR¶
This is a C preprocessor symbol that defines the major number of the SIP API. Its value is a number. There is no direct relationship between this and the SIP version number.
-
SIP_API_MINOR_NR¶
This is a C preprocessor symbol that defines the minor number of the SIP API. Its value is a number. There is no direct relationship between this and the SIP version number.
-
SIP_BLOCK_THREADS¶
This is a C preprocessor macro that will make sure the Python Global Interpreter Lock (GIL) is acquired. Python API calls must only be made when the GIL has been acquired. There must be a corresponding
SIP_UNBLOCK_THREADS
at the same lexical scope.
-
SIP_NO_CONVERTORS¶
This is a flag used by various type convertors that suppresses the use of a type’s
%ConvertToTypeCode
.
-
SIP_NOT_NONE¶
This is a flag used by various type convertors that causes the conversion to fail if the Python object being converted is
Py_None
.
-
SIP_NULLPTR¶
New in version 4.19.14.
This is a C preprocessor macro that should be used instead of
NULL
ornullptr
. It ensures the correct value is used depending on whether C or C++ is being generated and which language standard the compiler supports.
-
SIP_OWNS_MEMORY¶
New in version 4.15.2.
This is a flag used by various array constructors that species that the array owns the memory that holds the array’s contents.
-
SIP_PROTECTED_IS_PUBLIC¶
New in version 4.10.
This is a C preprocessor symbol that is defined automatically by the build system to specify that the generated code is being compiled with
protected
redefined aspublic
. This allows handwritten code to determine if the generated helper functions for accessing protected C++ functions are available (see%MethodCode
).
-
SIP_READ_ONLY¶
New in version 4.15.2.
This is a flag used by various array constructors that species that the array is read-only.
- SIP_RELEASE_GIL(sip_gilstate_t sipGILState)
New in version 4.14.4.
This is called from the handwritten code specified with the
VirtualErrorHandler
in order to release the Python Global Interpreter Lock (GIL) prior to changing the execution path (e.g. by throwing a C++ exception). It should not be called under any other circumstances.- Parameters
sipGILState – an opaque value provided to the handwritten code by SIP.
-
SIP_SSIZE_T¶
This is a C preprocessor macro that is defined as
Py_ssize_t
for Python v2.5 and later, and asint
for earlier versions of Python. It makes it easier to write PEP 353 compliant handwritten code.
-
SIP_SSIZE_T_FORMAT¶
New in version 4.15.4.
This is a C preprocessor macro that is defined as
%zd
for Python v2.5 and later, and as%d
for earlier versions of Python. It makes it easier to write PEP 353 compliant handwritten code.
-
SIP_UNBLOCK_THREADS¶
This is a C preprocessor macro that will restore the Python Global Interpreter Lock (GIL) to the state it was prior to the corresponding
SIP_BLOCK_THREADS
.
-
SIP_USE_PYCAPSULE¶
New in version 4.11.
This is a C preprocessor symbol that is defined when
PyCapsule
objects are being used rather than the (now deprecated)PyCObject
objects.
-
SIP_VERSION¶
This is a C preprocessor symbol that defines the SIP version number represented as a 3 part hexadecimal number (e.g. v5.0.0 is represented as
0x050000
).
-
SIP_VERSION_STR¶
This is a C preprocessor symbol that defines the SIP version number represented as a string. For development versions it will contain either
.dev
or-snapshot-
.
-
sipErrorState sipBadCallableArg(int arg_nr, PyObject *arg)¶
New in version 4.10.
This is called from
%MethodCode
to raise a Python exception when an argument to a function, a C++ constructor or method is found to have an unexpected type. This should be used when the%MethodCode
does additional type checking of the supplied arguments.- Parameters
arg_nr – the number of the argument. Arguments are numbered from 0 but are numbered from 1 in the detail of the exception.
arg – the argument.
- Returns
the value that should be assigned to
sipError
.
-
void sipBadCatcherResult(PyObject *method)¶
This raises a Python exception when the result of a Python reimplementation of a C++ method doesn’t have the expected type. It is normally called by handwritten code specified with the
%VirtualCatcherCode
directive.- Parameters
method – the Python method and would normally be the supplied
sipMethod
.
-
void sipBadLengthForSlice(SIP_SSIZE_T seqlen, SIP_SSIZE_T slicelen)¶
This raises a Python exception when the length of a slice object is inappropriate for a sequence-like object. It is normally called by handwritten code specified for
__setitem__()
methods.- Parameters
seqlen – the length of the sequence.
slicelen – the length of the slice.
-
type sipBufferInfoDef¶
New in version 4.19.
This C structure is used with
sipGetBufferInfo()
andsipReleaseBufferInfo()
and encapsulates information provided by a Python object that implements the buffer protocol. The structure elements are as follows.- void \*bi_buf
The address of the buffer.
- PyObject \*bi_obj
A reference to the object that implements the buffer protocol.
-
SIP_SSIZE_T bi_len¶
The length of the buffer in bytes.
- char \*bi_format
The format of each element of the buffer.
-
PyObject *sipBuildResult(int *iserr, const char *format, ...)¶
This creates a Python object based on a format string and associated values in a similar way to the Python
Py_BuildValue()
function.- Parameters
iserr – if this is not
NULL
then the location it points to is set to a non-zero value.format – the string of format characters.
- Returns
If there was an error then
NULL
is returned and a Python exception is raised.
If the format string begins and ends with parentheses then a tuple of objects is created. If it contains more than one format character then parentheses must be specified.
In the following description the first letter is the format character, the entry in parentheses is the Python object type that the format character will create, and the entry in brackets are the types of the C/C++ values to be passed.
a
(string) [char]Convert a C/C++
char
to a Python v2 or v3 string object.b
(boolean) [int]Convert a C/C++
int
to a Python boolean.c
(string/bytes) [char]Convert a C/C++
char
to a Python v2 string object or a Python v3 bytes object.d
(float) [double]Convert a C/C++
double
to a Python floating point number.e
(integer) [enum]Convert an anonymous C/C++
enum
to a Python integer.f
(float) [float]Convert a C/C++
float
to a Python floating point number.g
(string/bytes) [char *,SIP_SSIZE_T
]Convert a C/C++ character array and its length to a Python v2 string object or a Python v3 bytes object. If the array is
NULL
then the length is ignored and the result isPy_None
.h
(integer) [short]Convert a C/C++
short
to a Python integer.i
(integer) [int]Convert a C/C++
int
to a Python integer.l
(long) [long]Convert a C/C++
long
to a Python integer.m
(long) [unsigned long]Convert a C/C++
unsigned long
to a Python long.n
(long) [long long]Convert a C/C++
long long
to a Python long.o
(long) [unsigned long long]Convert a C/C++
unsigned long long
to a Python long.r
(wrapped instance) [type *,SIP_SSIZE_T
, constsipTypeDef
*]Convert an array of C structures, C++ classes or mapped type instances to a Python tuple. Note that copies of the array elements are made.
s
(string/bytes) [char *]Convert a C/C++
'\0'
terminated string to a Python v2 string object or a Python v3 bytes object. If the string pointer isNULL
then the result isPy_None
.t
(long) [unsigned short]Convert a C/C++
unsigned short
to a Python long.u
(long) [unsigned int]Convert a C/C++
unsigned int
to a Python long.w
(unicode/string) [wchar_t]Convert a C/C++ wide character to a Python v2 unicode object or a Python v3 string object.
x
(unicode/string) [wchar_t *]Convert a C/C++
L'\0'
terminated wide character string to a Python v2 unicode object or a Python v3 string object. If the string pointer isNULL
then the result isPy_None
.A
(string) [char *]Convert a C/C++
'\0'
terminated string to a Python v2 or v3 string object. If the string pointer isNULL
then the result isPy_None
.B
(wrapped instance) [type *,sipWrapperType
*, PyObject *]Deprecated since version 4.8: Use
N
instead.Convert a new C structure or a new C++ class instance to a Python class instance object. Ownership of the structure or instance is determined by the
PyObject *
argument. If it isNULL
and the instance has already been wrapped then the ownership is unchanged. If it isNULL
orPy_None
then ownership will be with Python. Otherwise ownership will be with C/C++ and the instance associated with thePyObject *
argument. The Python class is influenced by any applicable%ConvertToSubClassCode
code.C
(wrapped instance) [type *,sipWrapperType
*, PyObject *]Deprecated since version 4.8: Use
D
instead.Convert a C structure or a C++ class instance to a Python class instance object. If the structure or class instance has already been wrapped then the result is a new reference to the existing class instance object. Ownership of the structure or instance is determined by the
PyObject *
argument. If it isNULL
and the instance has already been wrapped then the ownership is unchanged. If it isNULL
and the instance is newly wrapped then ownership will be with C/C++. If it isPy_None
then ownership is transferred to Python via a call tosipTransferBack()
. Otherwise ownership is transferred to C/C++ and the instance associated with thePyObject *
argument via a call tosipTransferTo()
. The Python class is influenced by any applicable%ConvertToSubClassCode
code.D
(wrapped instance) [type *, constsipTypeDef
*, PyObject *]Convert a C structure, C++ class or mapped type instance to a Python object. If the instance has already been wrapped then the result is a new reference to the existing object. Ownership of the instance is determined by the
PyObject *
argument. If it isNULL
and the instance has already been wrapped then the ownership is unchanged. If it isNULL
and the instance is newly wrapped then ownership will be with C/C++. If it isPy_None
then ownership is transferred to Python via a call tosipTransferBack()
. Otherwise ownership is transferred to C/C++ and the instance associated with thePyObject *
argument via a call tosipTransferTo()
. The Python class is influenced by any applicable%ConvertToSubClassCode
code.E
(wrapped enum) [enum, PyTypeObject *]Deprecated since version 4.8: Use
F
instead.Convert a named C/C++
enum
to an instance of the corresponding Python named enum type.F
(wrapped enum) [enum,sipTypeDef
*]Convert a named C/C++
enum
to an instance of the corresponding Python named enum type.G
(unicode) [wchar_t *,SIP_SSIZE_T
]Convert a C/C++ wide character array and its length to a Python unicode object. If the array is
NULL
then the length is ignored and the result isPy_None
.L
(integer) [char]New in version 4.12.
Convert a C/C++
char
to a Python integer.M
(long) [unsigned char]New in version 4.12.
Convert a C/C++
unsigned char
to a Python long.N
(wrapped instance) [type *,sipTypeDef
*, PyObject *]Convert a new C structure, C++ class or mapped type instance to a Python object. Ownership of the instance is determined by the
PyObject *
argument. If it isNULL
and the instance has already been wrapped then the ownership is unchanged. If it isNULL
orPy_None
then ownership will be with Python. Otherwise ownership will be with C/C++ and the instance associated with thePyObject *
argument. The Python class is influenced by any applicable%ConvertToSubClassCode
code.R
(object) [PyObject *]The result is value passed without any conversions. The reference count is unaffected, i.e. a reference is taken.
S
(object) [PyObject *]The result is value passed without any conversions. The reference count is incremented.
V
(sip.voidptr) [void *]Convert a C/C++
void *
to a Pythonsip.voidptr
object.z
(object) [const char *, void *]New in version 4.14.1.
Convert a C/C++
void *
to a Python named capsule object.=
(long) [size_t]New in version 4.19.14.
Convert a C/C++
size_t
to a Python long.
-
PyObject *sipCallMethod(int *iserr, PyObject *method, const char *format, ...)¶
This calls a Python method passing a tuple of arguments based on a format string and associated values in a similar way to the Python
PyObject_CallObject()
function.- Parameters
iserr – if this is not
NULL
then the location it points to is set to a non-zero value if there was an error.method – the Python bound method to call.
format – the string of format characters (see
sipBuildResult()
).
- Returns
If there was an error then
NULL
is returned and a Python exception is raised.
It is normally called by handwritten code specified with the
%VirtualCatcherCode
directive with method being the suppliedsipMethod
.
-
int sipCanConvertToEnum(PyObject *obj, const sipTypeDef *td)¶
Deprecated since version 4.19.4: Use
sipConvertToEnum()
instead.This checks if a Python object can be converted to a named enum.
- Parameters
obj – the Python object.
td – the enum’s generated type structure.
- Returns
a non-zero value if the object can be converted.
-
int sipCanConvertToInstance(PyObject *obj, sipWrapperType *type, int flags)¶
Deprecated since version 4.8: Use
sipCanConvertToType()
instead.This checks if a Python object can be converted to an instance of a C structure or C++ class.
- Parameters
obj – the Python object.
type – the C/C++ type’s generated type object.
flags – any combination of the
SIP_NOT_NONE
andSIP_NO_CONVERTORS
flags.
- Returns
a non-zero value if the object can be converted.
-
int sipCanConvertToMappedType(PyObject *obj, const sipMappedType *mt, int flags)¶
Deprecated since version 4.8: Use
sipCanConvertToType()
instead.This checks if a Python object can be converted to an instance of a C structure or C++ class which has been implemented as a mapped type.
- Parameters
obj – the Python object.
mt – the opaque structure returned by
sipFindMappedType()
.flags – this may be the
SIP_NOT_NONE
flag.
- Returns
a non-zero value if the object can be converted.
-
int sipCanConvertToType(PyObject *obj, const sipTypeDef *td, int flags)¶
This checks if a Python object can be converted to an instance of a C structure, C++ class or mapped type.
- Parameters
obj – the Python object.
td – the C/C++ type’s generated type structure.
flags – any combination of the
SIP_NOT_NONE
andSIP_NO_CONVERTORS
flags.
- Returns
a non-zero value if the object can be converted.
-
type sipCFunctionDef¶
New in version 4.19.
This C structure is used with
sipGetCFunction()
and encapsulates the components parts of a Python C function. The structure elements are as follows.- PyMethodDef \*cf_function
The C function.
- PyObject \*cf_self
The optional bound object.
-
PyObject *sipClassName(PyObject *obj)¶
Deprecated since version 4.8: Use the following instead:
PyString_FromString(obj->ob_type->tp_name)
This gets the class name of a wrapped instance as a Python string. It comes with a reference.
- Parameters
obj – the wrapped instance.
- Returns
the name of the instance’s class.
-
PyObject *sipConvertFromConstVoidPtr(const void *cpp)¶
This creates a
sip.voidptr
object for a memory address. The object will not be writeable and has no associated size.- Parameters
cpp – the memory address.
- Returns
the
sip.voidptr
object.
-
PyObject *sipConvertFromConstVoidPtrAndSize(const void *cpp, SIP_SSIZE_T size)¶
This creates a
sip.voidptr
object for a memory address. The object will not be writeable and can be used as an immutable buffer object.- Parameters
cpp – the memory address.
size – the size associated with the address.
- Returns
the
sip.voidptr
object.
-
PyObject *sipConvertFromEnum(int eval, const sipTypeDef *td)¶
This converts a named C/C++
enum
to a Python object. If the enum is a C++11 scoped enum then the Python object is created using theenum
module. Otherwise a SIP generated type is used that can itself be converted to anint
.- Parameters
eval – the enumerated value to convert.
td – the enum’s generated type structure.
- Returns
the Python object.
-
PyObject *sipConvertFromInstance(void *cpp, sipWrapperType *type, PyObject *transferObj)¶
Deprecated since version 4.8: Use
sipConvertFromType()
instead.This converts a C structure or a C++ class instance to an instance of the corresponding generated Python type.
- Parameters
cpp – the C/C++ instance.
type – the type’s generated type object.
transferObj – this controls the ownership of the returned value.
- Returns
the Python object.
If the C/C++ instance has already been wrapped then the result is a new reference to the existing class instance object.
If transferObj is
NULL
and the instance has already been wrapped then the ownership is unchanged.If transferObj is
NULL
and the instance is newly wrapped then ownership will be with C/C++.If transferObj is
Py_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and the instance associated with transferObj via a call to
sipTransferTo()
.The Python type is influenced by any applicable
%ConvertToSubClassCode
code.
-
PyObject *sipConvertFromMappedType(void *cpp, const sipMappedType *mt, PyObject *transferObj)¶
Deprecated since version 4.8: Use
sipConvertFromType()
instead.This converts a C structure or a C++ class instance wrapped as a mapped type to an instance of the corresponding generated Python type.
- Parameters
cpp – the C/C++ instance.
mt – the opaque structure returned by
sipFindMappedType()
.transferObj – this controls the ownership of the returned value.
- Returns
the Python object.
If transferObj is
NULL
then the ownership is unchanged.If transferObj is
Py_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and the instance associated with transferObj argument via a call to
sipTransferTo()
.
-
PyObject *sipConvertFromNamedEnum(int eval, PyTypeObject *type)¶
Deprecated since version 4.8: Use
sipConvertFromEnum()
instead.This converts a named C/C++
enum
to an instance of the corresponding generated Python type.- Parameters
eval – the enumerated value to convert.
type – the enum’s generated type object.
- Returns
the Python object.
-
PyObject *sipConvertFromNewInstance(void *cpp, sipWrapperType *type, PyObject *transferObj)¶
Deprecated since version 4.8: Use
sipConvertFromNewType()
instead.This converts a new C structure or a C++ class instance to an instance of the corresponding generated Python type.
- Parameters
cpp – the C/C++ instance.
type – the type’s generated type object.
transferObj – this controls the ownership of the returned value.
- Returns
the Python object.
If transferObj is
NULL
orPy_None
then ownership will be with Python.Otherwise ownership will be with C/C++ and the instance associated with transferObj.
The Python type is influenced by any applicable
%ConvertToSubClassCode
code.
-
PyObject *sipConvertFromNewPyType(void *cpp, PyTypeObject *py_type, sipWrapper *owner, sipSimpleWrapper **selfp, const char *format, ...)¶
New in version 4.15.
This converts a new C structure or a C++ class instance to an instance of a corresponding Python type (as opposed to the corresponding generated Python type). This is useful when the C/C++ library provides some sort of mechanism whereby handwritten code has some control over the exact type of structure or class being created. Typically it would be used to create an instance of the generated derived class which would then allow Python re-implementations of C++ virtual methods to function properly.
- Parameters
cpp – the C/C++ instance.
py_type – the Python type object. This is called to create the Python object and is passed the arguments defined by the string of format characters.
owner – is the optional owner of the Python object.
selfp – is an optional pointer to the
sipPySelf
instance variable of the C/C++ instance if that instance’s type is a generated derived class. Otherwise it should beNULL
.format – the string of format characters (see
sipBuildResult()
).
- Returns
the Python object. If there was an error then
NULL
is returned and a Python exception is raised.
-
PyObject *sipConvertFromNewType(void *cpp, const sipTypeDef *td, PyObject *transferObj)¶
This converts a new C structure or a C++ class instance to an instance of the corresponding generated Python type.
- Parameters
cpp – the C/C++ instance.
td – the type’s generated type structure.
transferObj – this controls the ownership of the returned value.
- Returns
the Python object.
If transferObj is
NULL
orPy_None
then ownership will be with Python.Otherwise ownership will be with C/C++ and the instance associated with transferObj.
The Python type is influenced by any applicable
%ConvertToSubClassCode
code.
-
SIP_SSIZE_T sipConvertFromSequenceIndex(SIP_SSIZE_T idx, SIP_SSIZE_T len)¶
This converts a Python sequence index (i.e. where a negative value refers to the offset from the end of the sequence) to a C/C++ array index. If the index was out of range then a negative value is returned and a Python exception raised.
- Parameters
idx – the sequence index.
len – the length of the sequence.
- Returns
the unsigned array index.
-
int sipConvertFromSliceObject(PyObject *slice, SIP_SSIZE_T length, SIP_SSIZE_T *start, SIP_SSIZE_T *stop, SIP_SSIZE_T *step, SIP_SSIZE_T *slicelength)¶
This is a thin wrapper around the Python
PySlice_GetIndicesEx()
function provided to make it easier to write handwritten code that is compatible with SIP v3.x and versions of Python earlier that v2.3.
-
PyObject *sipConvertFromType(void *cpp, const sipTypeDef *td, PyObject *transferObj)¶
This converts a C structure or a C++ class instance to an instance of the corresponding generated Python type.
- Parameters
cpp – the C/C++ instance.
td – the type’s generated type structure.
transferObj – this controls the ownership of the returned value.
- Returns
the Python object.
If the C/C++ instance has already been wrapped then the result is a new reference to the existing object.
If transferObj is
NULL
and the instance has already been wrapped then the ownership is unchanged.If transferObj is
NULL
and the instance is newly wrapped then ownership will be with C/C++.If transferObj is
Py_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and the instance associated with transferObj via a call to
sipTransferTo()
.The Python class is influenced by any applicable
%ConvertToSubClassCode
code.
-
PyObject *sipConvertFromVoidPtr(void *cpp)¶
This creates a
sip.voidptr
object for a memory address. The object will be writeable but has no associated size.- Parameters
cpp – the memory address.
- Returns
the
sip.voidptr
object.
-
PyObject *sipConvertFromVoidPtrAndSize(void *cpp, SIP_SSIZE_T size)¶
This creates a
sip.voidptr
object for a memory address. The object will be writeable and can be used as a mutable buffer object.- Parameters
cpp – the memory address.
size – the size associated with the address.
- Returns
the
sip.voidptr
object.
-
PyObject *sipConvertToArray(void *data, const char *format, SIP_SSIZE_T len, int flags)¶
New in version 4.15.
This converts a one dimensional array of fundamental types to a
sip.array
object.An array is very like a Python
memoryview
object. The underlying memory is not copied and may be modified in situ. Arrays support the buffer protocol and so can be passed to other modules, again without the underlying memory being copied.sip.array
objects are not supported by the sip code generator. They can only be created by handwritten code or bysip.voidptr.asarray()
.- Parameters
data – the address of the start of the C/C++ array.
format – the format, as defined by the
struct
module, of an array element. At the moment onlyb
(char),B
(unsigned char),h
(short),H
(unsigned short),i
(int),I
(unsigned int),f
(float) andd
(double) are supported.len – the number of elements in the array.
readonly – is non-zero if the array is read-only.
flags – any combination of the
SIP_READ_ONLY
andSIP_OWNS_MEMORY
flags.
- Returns
the
sip.array
object.
-
int sipConvertToBool(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to an integer corresponding to a C++
bool
.- Parameters
obj – the Python object to convert.
- Returns
the boolean value as an integer.
1
corresponds totrue
and0
corresponds tofalse
.-1
is returned, and an exception is raised, if there was an error.
-
int sipConvertToEnum(PyObject *obj, const sipTypeDef *td)¶
New in version 4.19.4.
This converts a Python object to the value of a named C/C++
enum
member. If the enum is a C++11 scoped enum then the Python object must be a member of the enum. Otherwise it may also be anint
corresponding to the name of the member.- Parameters
obj – the Python object to convert.
td – the enum’s generated type structure.
- Returns
the integer value. An exception is raised if there was an error.
-
void *sipConvertToInstance(PyObject *obj, sipWrapperType *type, PyObject *transferObj, int flags, int *state, int *iserr)¶
Deprecated since version 4.8: Use
sipConvertToType()
instead.This converts a Python object to an instance of a C structure or C++ class assuming that a previous call to
sipCanConvertToInstance()
has been successful.- Parameters
obj – the Python object.
type – the type’s generated type object.
transferObj – this controls any ownership changes to obj.
flags – any combination of the
SIP_NOT_NONE
andSIP_NO_CONVERTORS
flags.state – the state of the returned C/C++ instance is returned via this pointer.
iserr – the error flag is passed and updated via this pointer.
- Returns
the C/C++ instance.
If transferObj is
NULL
then the ownership is unchanged.If transferObj is
Py_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and obj associated with transferObj via a call to
sipTransferTo()
.If state is not
NULL
then the location it points to is set to describe the state of the returned C/C++ instance and is the value returned by any%ConvertToTypeCode
. The calling code must then release the value at some point to prevent a memory leak by callingsipReleaseInstance()
.If there is an error then the location iserr points to is set to a non-zero value. If it was initially a non-zero value then the conversion isn’t attempted in the first place. (This allows several calls to be made that share the same error flag so that it only needs to be tested once rather than after each call.)
-
void *sipConvertToMappedType(PyObject *obj, const sipMappedType *mt, PyObject *transferObj, int flags, int *state, int *iserr)¶
Deprecated since version 4.8: Use
sipConvertToType()
instead.This converts a Python object to an instance of a C structure or C++ class that is implemented as a mapped type assuming that a previous call to
sipCanConvertToMappedType()
has been successful.- Parameters
obj – the Python object.
mt – the opaque structure returned by
sipFindMappedType()
.transferObj – this controls any ownership changes to obj.
flags – this may be the
SIP_NOT_NONE
flag.state – the state of the returned C/C++ instance is returned via this pointer.
iserr – the error flag is passed and updated via this pointer.
- Returns
the C/C++ instance.
If transferObj is
NULL
then the ownership is unchanged.If transferObj is
Py_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and obj associated with transferObj via a call to
sipTransferTo()
.If state is not
NULL
then the location it points to is set to describe the state of the returned C/C++ instance and is the value returned by any%ConvertToTypeCode
. The calling code must then release the value at some point to prevent a memory leak by callingsipReleaseMappedType()
.If there is an error then the location iserr points to is set to a non-zero value. If it was initially a non-zero value then the conversion isn’t attempted in the first place. (This allows several calls to be made that share the same error flag so that it only needs to be tested once rather than after each call.)
-
void *sipConvertToType(PyObject *obj, const sipTypeDef *td, PyObject *transferObj, int flags, int *state, int *iserr)¶
This converts a Python object to an instance of a C structure, C++ class or mapped type assuming that a previous call to
sipCanConvertToType()
has been successful.- Parameters
obj – the Python object.
td – the type’s generated type structure.
transferObj – this controls any ownership changes to obj.
flags – any combination of the
SIP_NOT_NONE
andSIP_NO_CONVERTORS
flags.state – the state of the returned C/C++ instance is returned via this pointer.
iserr – the error flag is passed and updated via this pointer.
- Returns
the C/C++ instance.
If transferObj is
NULL
then the ownership is unchanged. If it isPy_None
then ownership is transferred to Python via a call tosipTransferBack()
.Otherwise ownership is transferred to C/C++ and obj associated with transferObj via a call to
sipTransferTo()
.Note that obj can also be managed by the C/C++ instance itself, but this can only be achieved by using
sipTransferTo()
.If state is not
NULL
then the location it points to is set to describe the state of the returned C/C++ instance and is the value returned by any%ConvertToTypeCode
. The calling code must then release the value at some point to prevent a memory leak by callingsipReleaseType()
.If there is an error then the location iserr points to is set to a non-zero value. If it was initially a non-zero value then the conversion isn’t attempted in the first place. (This allows several calls to be made that share the same error flag so that it only needs to be tested once rather than after each call.)
-
PyObject *sipConvertToTypedArray(void *data, const sipTypeDef *td, const char *format, size_t stride, SIP_SSIZE_T len, int flags)¶
New in version 4.15.
This converts a one dimensional array of instances of a C structure, C++ class or mapped type to a
sip.array
object.An array is very like a Python
memoryview
object but it’s elements correspond to C structures or C++ classes. The underlying memory is not copied and may be modified in situ. Arrays support the buffer protocol and so can be passed to other modules, again without the underlying memory being copied.sip.array
objects are not supported by the sip code generator. They can only be created by handwritten code.- Parameters
data – the address of the start of the C/C++ array.
td – an element’s type’s generated type structure.
format – the format, as defined by the
struct
module, of an array element.stride – the size of an array element, including any padding.
len – the number of elements in the array.
flags – the optional
SIP_READ_ONLY
flag.
- Returns
the
sip.array
object.
-
void *sipConvertToVoidPtr(PyObject *obj)¶
This converts a Python object to a memory address.
PyErr_Occurred()
must be used to determine if the conversion was successful.- Parameters
obj – the Python object which may be
Py_None
, asip.voidptr
or aPyCObject
.
- Returns
the memory address.
-
type sipDateDef¶
New in version 4.19.
This C structure is used with
sipGetDate()
,sipFromDate()
,sipGetDateTime()
andsipFromDateTime()
and encapsulates the components parts of a Python date. The structure elements are as follows.-
int pd_year¶
The year.
-
int pd_month¶
The month (1-12).
-
int pd_day¶
The day (1-31).
-
int pd_year¶
-
int sipEnableAutoconversion(const sipTypeDef *td, int enable)¶
New in version 4.14.7.
Instances of some classes may be automatically converted to other Python objects even though the class has been wrapped. This allows that behaviour to be suppressed so that an instances of the wrapped class is returned instead.
- Parameters
td – the type’s generated type structure. This must refer to a class.
enable – is non-zero if auto-conversion should be enabled for the type. This is the default behaviour.
- Returns
1
or0
depending on whether or not auto-conversion was previously enabled for the type. This allows the previous state to be restored later on.-1
is returned, and a Python exception raised, if there was an error.
-
int sipEnableGC(int enable)¶
New in version 4.19.1.
This enables or disables the Python garbarge collector.
- Parameters
enable – is greater than
0
if the garbage collector should be enabled.
- Returns
1
or0
depending on whether or not the garbage collector was previously enabled. This allows the previous state to be restored later on.-1
is returned if there was an error.
-
int sipEnableOverflowChecking(int enable)¶
New in version 4.19.4.
This enables or disables the checking for overflows when converting Python integer objects to C/C++ integer types. When it is enabled an exception is raised when the value of a Python integer object is too large to fit in the corresponding C/C++ type. By default it is disabled.
- Parameters
enable – is greater than
0
if overflow checking should be enabled.
- Returns
1
or0
depending on whether or not overflow chacking was previously enabled. This allows the previous state to be restored later on.
-
int sipExportSymbol(const char *name, void *sym)¶
Python does not allow extension modules to directly access symbols in another extension module. This exports a symbol, referenced by a name, that can subsequently be imported, using
sipImportSymbol()
, by another module.- Parameters
name – the name of the symbol.
sym – the value of the symbol.
- Returns
0 if there was no error. A negative value is returned if name is already associated with a symbol or there was some other error.
-
sipWrapperType *sipFindClass(const char *type)¶
Deprecated since version 4.8: Use
sipFindType()
instead.This returns a pointer to the generated type object corresponding to a C/C++ type.
- Parameters
type – the C/C++ declaration of the type.
- Returns
the generated type object. This will not change and may be saved in a static cache.
NULL
is returned if the C/C++ type doesn’t exist.
-
const sipMappedType *sipFindMappedType(const char *type)¶
Deprecated since version 4.8: Use
sipFindType()
instead.This returns a pointer to an opaque structure describing a mapped type.
- Parameters
type – the C/C++ declaration of the type.
- Returns
the opaque structure. This will not change and may be saved in a static cache.
NULL
is returned if the C/C++ type doesn’t exist.
-
PyTypeObject *sipFindNamedEnum(const char *type)¶
Deprecated since version 4.8: Use
sipFindType()
instead.This returns a pointer to the generated Python type object corresponding to a named unscoped C/C++ enum.
- Parameters
type – the C/C++ declaration of the enum.
- Returns
the generated Python type object. This will not change and may be saved in a static cache.
NULL
is returned if the C/C++ unscoped enum doesn’t exist.
-
const sipTypeDef *sipFindType(const char *type)¶
This returns a pointer to the generated type structure corresponding to a C/C++ type.
- Parameters
type – the C/C++ declaration of the type.
- Returns
the generated type structure. This will not change and may be saved in a static cache.
NULL
is returned if the C/C++ type doesn’t exist.
-
void *sipForceConvertToInstance(PyObject *obj, sipWrapperType *type, PyObject *transferObj, int flags, int *state, int *iserr)¶
Deprecated since version 4.8: Use
sipForceConvertToType()
instead.This converts a Python object to an instance of a C structure or C++ class by calling
sipCanConvertToInstance()
and, if it is successfull, callingsipConvertToInstance()
.See
sipConvertToInstance()
for a full description of the arguments.
-
void *sipForceConvertToMappedType(PyObject *obj, const sipMappedType *mt, PyObject *transferObj, int flags, int *state, int *iserr)¶
Deprecated since version 4.8: Use
sipForceConvertToType()
instead.This converts a Python object to an instance of a C structure or C++ class which has been implemented as a mapped type by calling
sipCanConvertToMappedType()
and, if it is successfull, callingsipConvertToMappedType()
.See
sipConvertToMappedType()
for a full description of the arguments.
-
void *sipForceConvertToType(PyObject *obj, const sipTypeDef *td, PyObject *transferObj, int flags, int *state, int *iserr)¶
This converts a Python object to an instance of a C structure, C++ class or mapped type by calling
sipCanConvertToType()
and, if it is successfull, callingsipConvertToType()
.See
sipConvertToType()
for a full description of the arguments.
-
void sipFree(void *mem)¶
This returns an area of memory allocated by
sipMalloc()
to the heap.- Parameters
mem – the memory address.
-
PyObject *sipFromDate(const sipDateDef *date)¶
New in version 4.19.
This creates a Python date object from its component parts.
- Parameters
date – the component parts of the date.
- Returns
the Python date object.
-
PyObject *sipFromDateTime(const sipDateDef *date, const sipTimeDef *time)¶
New in version 4.19.
This creates a Python datetime object from its component parts.
- Parameters
date – the date related component parts of the datetime.
time – the time related component parts of the datetime.
- Returns
the Python datetime object.
-
PyObject *sipFromMethod(const sipMethodDef *method)¶
New in version 4.19.
This creates a Python method object from its component parts.
- Parameters
method – the component parts of the method.
- Returns
the Python method object.
-
PyObject *sipFromTime(const sipTimeDef *time)¶
New in version 4.19.
This creates a Python time object from its component parts.
- Parameters
time – the component parts of the time.
- Returns
the Python time object.
-
void *sipGetAddress(sipSimpleWrapper *obj)¶
New in version 4.12.
This returns the address of the C structure or C++ class instance wrapped by a Python object.
- Parameters
obj – the Python object.
- Returns
the address of the C/C++ instance
-
int sipGetBufferInfo(PyObject *obj, sipBufferInfoDef *buffer_info)¶
New in version 4.19.
This checks to see if an object implements the Python buffer protocol and, if so, optionally returns the buffer information. It is similar to
PyObject_GetBuffer()
and should be used instead of that when the limited Python API is enabled. Note that, at the moment, only 1-dimensional buffers are supported.- Parameters
obj – the Python object.
buffer_info – if this is not
NULL
, and the object implements the buffer protocol, then the buffer information is returned in this structure. There should be a corresponding call tosipReleaseBuffer()
.
- Returns
> 0 if the object supports the buffer protocol and the buffer information was returned (if requested). 0 if the object does not support the buffer protocol. < 0 (and a Python exception is raised) if the object supports the buffer protocol but there was an error returning the requested buffer information.
-
int sipGetCFunction(PyObject *obj, sipCFunctionDef *c_function)¶
New in version 4.19.
This checks to see if an object is a Python C function object and, if so, optionally returns its component parts.
- Parameters
obj – the Python object.
c_function – if this is not
NULL
, and the object is a C function object, then the component parts are returned in this structure.
- Returns
a non-zero value if the object is a Python C function object.
-
int sipGetDate(PyObject *obj, sipDateDef *date)¶
New in version 4.19.
This checks to see if an object is a Python date object and, if so, optionally returns its component parts.
- Parameters
obj – the Python object.
date – if this is not
NULL
, and the object is a date object, then the component parts are returned in this structure.
- Returns
a non-zero value if the object is a Python date object.
-
int sipGetDateTime(PyObject *obj, sipDateDef *date, sipTimeDef *time)¶
New in version 4.19.
This checks to see if an object is a Python datetime object and, if so, optionally returns its component parts.
- Parameters
obj – the Python object.
date – if this is not
NULL
, and the object is a datetime object, then the date related component parts are returned in this structure.time – if this is not
NULL
, and the object is a datetime object, then the time related component parts are returned in this structure.
- Returns
a non-zero value if the object is a Python datetime object.
-
struct _frame sipGetFrame(int depth)¶
New in version 4.19.
This retrieves a frame object from the current execution stack.
- Parameters
depth – the depth of frame to retrieve where 0 is the current frame, 1 is the previous frame etc.
- Returns
the opaque frame or NULL if there wasn’t one at the given depth.
-
PyInterpreterState *sipGetInterpreter()¶
New in version 4.17.1.
This returns the address of the Python interpreter. If it is
NULL
then calls to the Python interpreter library must not be made.- Returns
the address of the Python interpreter
-
int sipGetMethod(PyObject *obj, sipMethodDef *method)¶
New in version 4.19.
This checks to see if an object is a Python method object and, if so, optionally returns its component parts.
- Parameters
obj – the Python object.
method – if this is not
NULL
, and the object is a method object, then the component parts are returned in this structure.
- Returns
a non-zero value if the object is a Python method object.
-
void *sipGetMixinAddress(sipSimpleWrapper *obj, const sipTypeDef *td)¶
New in version 4.15.
This returns the address of the C++ class instance that implements the mixin of a wrapped Python object.
- Parameters
obj – the Python object.
td – the generated type structure corresponding to the C++ type of the mixin.
- Returns
the address of the C++ instance
-
PyObject *sipGetPyObject(void *cppptr, const sipTypeDef *td)¶
This returns a borrowed reference to the Python object for a C structure or C++ class instance.
- Parameters
cppptr – the pointer to the C/C++ instance.
td – the generated type structure corresponding to the C/C++ type.
- Returns
the Python object or
NULL
(and no exception is raised) if the C/C++ instance hasn’t been wrapped.
-
int sipGetState(PyObject *transferObj)¶
The
%ConvertToTypeCode
directive requires that the provided code returns anint
describing the state of the converted value. The state usually depends on any transfers of ownership that have been requested. This is a convenience function that returns the correct state when the converted value is a temporary.- Parameters
transferObj – the object that describes the requested transfer of ownership.
- Returns
the state of the converted value.
-
int sipGetTime(PyObject *obj, sipTimeDef *time)¶
New in version 4.19.
This checks to see if an object is a Python time object and, if so, optionally returns its component parts.
- Parameters
obj – the Python object.
time – if this is not
NULL
, and the object is a time object, then the component parts are returned in this structure.
- Returns
a non-zero value if the object is a Python time object.
-
void *sipGetTypeUserData(const sipWrapperType *type)¶
New in version 4.19.
Each generated type corresponding to a wrapped C/C++ type, or a user sub-class of such a type, contains a pointer for the use of handwritten code. This returns the value of that pointer.
- Parameters
type – the type object.
- Returns
the type-specific pointer.
-
PyObject *sipGetUserObject(const sipSimpleWrapper *obj)¶
New in version 4.19.
Each wrapped object can contain a reference to a single Python object that can be used for any purpose by handwritten code and will automatically be garbage collected at the appropriate time. This returns that object.
- Parameters
obj – the wrapped object.
- Returns
the user object.
-
PyObject *sipGetWrapper(void *cppptr, sipWrapperType *type)¶
Deprecated since version 4.8: Use
sipGetPyObject()
instead.This returns a borrowed reference to the wrapped instance object for a C structure or C++ class instance.
- Parameters
cppptr – the pointer to the C/C++ instance.
type – the generated type object corresponding to the C/C++ type.
- Returns
the Python object or
NULL
(and no exception is raised) if the C/C++ instance hasn’t been wrapped.
-
void *sipImportSymbol(const char *name)¶
Python does not allow extension modules to directly access symbols in another extension module. This imports a symbol, referenced by a name, that has previously been exported, using
sipExportSymbol()
, by another module.- Parameters
name – the name of the symbol.
- Returns
the value of the symbol.
NULL
is returned if there is no such symbol.
-
void sipInstanceDestroyed(sipSimpleWrapper *obj)¶
New in version 4.19.3.
This should be called by handwritten code if it is able to detect that a wrapped C++ instance has been destroyed from C++. It should not be called if SIP is able to detect this itself, i.e. when the instance was created from Python and the class has a virtual destructor.
- Parameters
obj – the Python object that wraps the destroyed instance.
-
type sipIntTypeClassMap¶
Deprecated since version 4.8.
This C structure is used with
sipMapIntToClass()
to define a mapping between integer based RTTI and generated type objects. The structure elements are as follows.-
int typeInt¶
The integer RTTI.
- sipWrapperType **pyType.
A pointer to the corresponding generated type object.
-
int typeInt¶
-
int sipIsAPIEnabled(const char *name, int from, int to)¶
New in version 4.9.
This checks to see if the current version number of an API falls within a given range. See Managing Incompatible APIs for more detail.
- Parameters
name – the name of the API.
from – the lower bound of the range. For the API to be enabled its version number must be greater than or equal to from. If from is 0 then this check isn’t made.
to – the upper bound of the range. For the API to be enabled its version number must be less than to. If to is 0 then this check isn’t made.
- Returns
a non-zero value if the API is enabled.
-
int sipIsOwnedByPython(sipSimpleWrapper *obj)¶
New in version 4.19.19.
This determines if a wrapped object is currently owned by Python.
- Parameters
obj – the wrapped object.
- Returns
a non-zero value if the object is currently owned by Python.
-
int sipIsUserType(const sipWrapperType *type)¶
New in version 4.19.
This checks if a type corresponds to a wrapped C/C++ type or a user sub-class of such a type.
- Parameters
type – the type object.
- Returns
a non-zero value if the type is a user defined type.
-
char sipLong_AsChar(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ char. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
signed char sipLong_AsSignedChar(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ signed char. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
unsigned char sipLong_AsUnsignedChar(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ unsigned char. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
short sipLong_AsShort(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ short. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
unsigned short sipLong_AsUnsignedShort(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ unsigned short. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
int sipLong_AsInt(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ int. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
unsigned int sipLong_AsUnsignedInt(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ unsigned int. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
size_t sipLong_AsSizeT(PyObject *obj)¶
New in version 4.19.14.
This converts a Python object to a C/C++ size_t. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
long sipLong_AsLong(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ long. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
unsigned long sipLong_AsUnsignedLong(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ unsigned long. If the value is too large then an exception is raised if overflow checking is enabled.
- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
PY_LONG_LONG sipLong_AsLongLong(PyObject *obj)¶
New in version 4.19.4.
This converts a Python object to a C/C++ long long. If the value is too large then an exception is raised if overflow checking is enabled. It is not available if
Python.h
does not defineHAVE_LONG_LONG
.- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
- unsigned PY_LONG_LONG sipLong_AsUnsignedLongLong(PyObject *obj)
New in version 4.19.4.
This converts a Python object to a C/C++ unsigned long long. If the value is too large then an exception is raised if overflow checking is enabled. It is not available if
Python.h
does not defineHAVE_LONG_LONG
.- Parameters
obj – the Python object.
- Returns
the converted C/C++ value.
-
void *sipMalloc(size_t nbytes)¶
This allocates an area of memory on the heap using the Python
PyMem_Malloc()
function. The memory is freed by callingsipFree()
.- Parameters
nbytes – the number of bytes to allocate.
- Returns
the memory address. If there was an error then
NULL
is returned and a Python exception raised.
-
sipWrapperType *sipMapIntToClass(int type, const sipIntTypeClassMap *map, int maplen)¶
Deprecated since version 4.8.
This can be used in
%ConvertToSubClassCode
code as a convenient way of converting integer based RTTI to the corresponding generated type object.- Parameters
type – the integer RTTI.
map – the table of known RTTI and the corresponding type objects (see
sipIntTypeClassMap
). The entries in the table must be sorted in ascending order of RTTI.maplen – the number of entries in the table.
- Returns
the corresponding type object, or
NULL
if type wasn’t in map.
-
sipWrapperType *sipMapStringToClass(char *type, const sipStringTypeClassMap *map, int maplen)¶
Deprecated since version 4.8.
This can be used in
%ConvertToSubClassCode
code as a convenient way of converting'\0'
terminated string based RTTI to the corresponding generated type object.- Parameters
type – the string RTTI.
map – the table of known RTTI and the corresponding type objects (see
sipStringTypeClassMap
). The entries in the table must be sorted in ascending order of RTTI.maplen – the number of entries in the table.
- Returns
the corresponding type object, or
NULL
if type wasn’t in map.
-
type sipMethodDef¶
New in version 4.19.
This C structure is used with
sipGetMethod()
andsipFromMethod()
and encapsulates the components parts of a Python method. The structure elements are as follows.-
PyObject *pm_function¶
The function that implements the method.
-
PyObject *pm_self¶
The bound object.
-
PyObject *pm_class¶
The class. (Python v2 only.)
-
PyObject *pm_function¶
-
sipNewUserTypeFunc sipSetNewUserTypeHandler(const sipTypeDef *td, sipNewUserTypeFunc handler)¶
New in version 4.19.
The allows a function to be specified that is called whenever a user defined sub-class of a C/C++ type is created (i.e. one implemented in Python). It is normalled called from a module’s
%PostInitialisationCode
. It is provided as an alternative to providing a meta-type when the limited Python API is enabled.- Parameters
td – the generated type object corresponding to the C/C++ type.
handler – the function that is called whenever a user defined sub-class of the type is created. The function takes a single argument which is the
sipWrapperType
of the user defined class. It returns anint
which is 0 if there was no error. A Python exception is raised and -1 returned if there was an error.
- Returns
the previously installed handler. This allows handlers to be chained.
-
int sipParseResult(int *iserr, PyObject *method, PyObject *result, const char *format, ...)¶
This converts a Python object (usually returned by a method) to C/C++ based on a format string and associated values in a similar way to the Python
PyArg_ParseTuple()
function.- Parameters
iserr – if this is not
NULL
then the location it points to is set to a non-zero value if there was an error.method – the Python method that returned result.
result – the Python object returned by method.
format – the format string.
- Returns
0 if there was no error. Otherwise a negative value is returned, and an exception raised.
This is normally called by handwritten code specified with the
%VirtualCatcherCode
directive with method being the suppliedsipMethod
and result being the value returned bysipCallMethod()
.If format begins and ends with parentheses then result must be a Python tuple and the rest of format is applied to the tuple contents.
In the following description the first letter is the format character, the entry in parentheses is the Python object type that the format character will convert, and the entry in brackets are the types of the C/C++ values to be passed.
ae
(object) [char *]Convert a Python string-like object of length 1 to a C/C++
char
according to the encodinge
.e
can either beA
for ASCII,L
for Latin-1, or8
for UTF-8. For Python v2 the object may be either a string or a unicode object that can be encoded. For Python v3 the object may either be a bytes object or a string object that can be encoded. An object that supports the buffer protocol may also be used.b
(integer) [bool *]Convert a Python integer to a C/C++
bool
.c
(string/bytes) [char *]Convert a Python v2 string object or a Python v3 bytes object of length 1 to a C/C++
char
.d
(float) [double *]Convert a Python floating point number to a C/C++
double
.e
(integer) [enum *]Convert a Python integer to an anonymous C/C++
enum
.f
(float) [float *]Convert a Python floating point number to a C/C++
float
.g
(string/bytes) [const char **,SIP_SSIZE_T
*]Convert a Python v2 string object or a Python v3 bytes object to a C/C++ character array and its length. If the Python object is
Py_None
then the array and length areNULL
and zero respectively.h
(integer) [short *]Convert a Python integer to a C/C++
short
.i
(integer) [int *]Convert a Python integer to a C/C++
int
.l
(long) [long *]Convert a Python long to a C/C++
long
.m
(long) [unsigned long *]Convert a Python long to a C/C++
unsigned long
.n
(long) [long long *]Convert a Python long to a C/C++
long long
.o
(long) [unsigned long long *]Convert a Python long to a C/C++
unsigned long long
.s
(string/bytes) [const char **]Deprecated since version 4.8: Use
B
instead.Convert a Python v2 string object or a Python v3 bytes object to a C/C++
'\0'
terminated string. If the Python object isPy_None
then the string isNULL
.t
(long) [unsigned short *]Convert a Python long to a C/C++
unsigned short
.u
(long) [unsigned int *]Convert a Python long to a C/C++
unsigned int
.w
(unicode/string) [wchar_t *]Convert a Python v2 string or unicode object or a Python v3 string object of length 1 to a C/C++ wide character.
x
(unicode/string) [wchar_t **]Convert a Python v2 string or unicode object or a Python v3 string object to a C/C++
L'\0'
terminated wide character string. If the Python object isPy_None
then the string isNULL
.Ae
(object) [int, const char **]Convert a Python string-like object to a C/C++
'\0'
terminated string according to the encodinge
.e
can either beA
for ASCII,L
for Latin-1, or8
for UTF-8. If the Python object isPy_None
then the string isNULL
. The integer uniquely identifies the object in the context defined by theS
format character and allows an extra reference to the object to be kept to ensure that the string remains valid. For Python v2 the object may be either a string or a unicode object that can be encoded. For Python v3 the object may either be a bytes object or a string object that can be encoded. An object that supports the buffer protocol may also be used.B
(string/bytes) [int, const char **]Convert a Python v2 string object or a Python v3 bytes object to a C/C++
'\0'
terminated string. If the Python object isPy_None
then the string isNULL
. The integer uniquely identifies the object in the context defined by theS
format character and allows an extra reference to the object to be kept to ensure that the string remains valid.Cf
(wrapped class) [sipWrapperType
*, int *, void **]Deprecated since version 4.8: Use
Hf
instead.Convert a Python object to a C structure or a C++ class instance and return its state as described in
sipConvertToInstance()
.f
is a combination of the following flags encoded as an ASCII character by adding0
to the combined value:0x01 disallows the conversion of
Py_None
toNULL
- 0x02 implements the
Factory
andTransferBack
annotations
- 0x04 suppresses the return of the state of the returned C/C++
instance. Note that the
int *
used to return the state is not passed if this flag is specified.
- 0x02 implements the
Df
(wrapped instance) [constsipTypeDef
*, int *, void **]Deprecated since version 4.10.1: Use
Hf
instead.Convert a Python object to a C structure, C++ class or mapped type instance and return its state as described in
sipConvertToType()
.f
is a combination of the following flags encoded as an ASCII character by adding0
to the combined value:0x01 disallows the conversion of
Py_None
toNULL
- 0x02 implements the
Factory
andTransferBack
annotations
- 0x04 suppresses the return of the state of the returned C/C++
instance. Note that the
int *
used to return the state is not passed if this flag is specified.
- 0x02 implements the
E
(wrapped enum) [PyTypeObject *, enum *]Deprecated since version 4.8: Use
F
instead.Convert a Python named enum type to the corresponding C/C++
enum
.F
(wrapped enum) [sipTypeDef
*, enum *]Convert a Python named enum type to the corresponding C/C++
enum
.G
(unicode/string) [wchar_t **,SIP_SSIZE_T
*]Convert a Python v2 string or unicode object or a Python v3 string object to a C/C++ wide character array and its length. If the Python object is
Py_None
then the array and length areNULL
and zero respectively.Hf
(wrapped instance) [constsipTypeDef
*, int *, void **]Convert a Python object to a C structure, C++ class or mapped type instance as described in
sipConvertToType()
.f
is a combination of the following flags encoded as an ASCII character by adding0
to the combined value:0x01 disallows the conversion of
Py_None
toNULL
- 0x02 implements the
Factory
andTransferBack
annotations
0x04 returns a copy of the C/C++ instance.
- 0x02 implements the
L
(integer) [signed char *]New in version 4.12.
Convert a Python integer to a C/C++
signed char
.M
(long) [unsigned char *]New in version 4.12.
Convert a Python long to a C/C++
unsigned char
.N
(object) [PyTypeObject *, PyObject **]A Python object is checked to see if it is a certain type and then returned without any conversions. The reference count is incremented. The Python object may be
Py_None
.O
(object) [PyObject **]A Python object is returned without any conversions. The reference count is incremented.
S
[sipSimpleWrapper
*]This format character, if used, must be the first. It is used with other format characters to define a context and doesn’t itself convert an argument.
T
(object) [PyTypeObject *, PyObject **]A Python object is checked to see if it is a certain type and then returned without any conversions. The reference count is incremented. The Python object may not be
Py_None
.V
(sip.voidptr
) [void **]Convert a Python
sip.voidptr
object to a C/C++void *
.z
(object) [const char *, void **]New in version 4.14.1.
Convert a Python named capsule object to a C/C++
void *
.Z
(object) []Check that a Python object is
Py_None
. No value is returned.!
(object) [PyObject **]New in version 4.14.1.
A Python object is checked to see if it implements the buffer protocol and then returned without any conversions. The reference count is incremented. The Python object may not be
Py_None
.$
(object) [PyObject **]New in version 4.14.1.
A Python object is checked to see if it implements the buffer protocol and then returned without any conversions. The reference count is incremented. The Python object may be
Py_None
.=
(long) [size_t *]New in version 4.19.14.
Convert a Python long to a C/C++
size_t
.
-
PyObject *sipPyTypeDict(const PyTypeObject *py_type)¶
New in version 4.19.
This provides access to a Python type object’s
tp_dict
field and is typically used when the limited Python API is enabled.- Parameters
py_type – the type object.
- Returns
the value of the type object’s
tp_dict
field.
-
const char *sipPyTypeName(const PyTypeObject *py_type)¶
New in version 4.19.
This provides access to a Python type object’s
tp_name
field and is typically used when the limited Python API is enabled.- Parameters
py_type – the type object.
- Returns
the value of the type object’s
tp_name
field.
-
int sipRegisterAttributeGetter(const sipTypeDef *td, sipAttrGetterFunc getter)¶
This registers a getter that will called just before SIP needs to get an attribute from a wrapped type’s dictionary for the first time. The getter must then populate the type’s dictionary with any lazy attributes.
- Parameters
td – the optional generated type structure that determines which types the getter will be called for.
getter – the getter function.
- Returns
0 if there was no error, otherwise -1 is returned.
If td is not
NULL
then the getter will only be called for types with that type or that are sub-classed from it. Otherwise the getter will be called for all types.A getter has the following signature.
int getter(const
sipTypeDef
*td, PyObject *dict)td is the generated type definition of the type whose dictionary is to be populated.
dict is the dictionary to be populated.
0 is returned if there was no error, otherwise -1 is returned.
See the section Lazy Type Attributes for more details.
-
int sipRegisterExitNotifier(PyMethodDef *md)¶
New in version 4.19.19.
This registers a C function with Python’s
atexit
module that will be called when the interpreter terminates.- Parameters
md – the data structure that describes the C function to be called.
- Returns
0 if there was no error, otherwise -1 is returned.
-
int sipRegisterProxyResolver(const sipTypeDef *td, sipProxyResolverFunc resolver)¶
New in version 4.15.
This registers a resolver that will called just before SIP wraps a C/C++ pointer in a Python object. The resolver may choose to replace the C/C++ pointer with the address of another object. Typically this is used to replace a proxy by the object that is being proxied for.
- Parameters
td – the optional generated type structure that determines which type the resolver will be called for.
resolver – the resolver function.
- Returns
0 if there was no error, otherwise -1 is returned.
A resolver has the following signature.
void *resolver(void *proxy)
proxy is C/C++ pointer that is being wrapped.
The C/C++ pointer that will actually be wrapped is returned.
-
int sipRegisterPyType(PyTypeObject *type)¶
This registers a Python type object that can be used as the meta-type or super-type of a wrapped C++ type.
- Parameters
type – the type object.
- Returns
0 if there was no error, otherwise -1 is returned.
See the section Types and Meta-types for more details.
-
void sipReleaseBufferInfo(sipBufferInfoDef *buffer_info)¶
New in version 4.19.
This releases the buffer information related to a Python object that implements the buffer protocol that was created with a corresponding call to
sipGetBufferInfo()
. It is similar toPyBuffer_Release()
and should be used instead of that when the limited Python API is enabled.- Parameters
buffer_info – the buffer information to release.
-
void sipReleaseInstance(void *cpp, sipWrapperType *type, int state)¶
Deprecated since version 4.8: Use
sipReleaseType()
instead.This destroys a wrapped C/C++ instance if it was a temporary instance. It is called after a call to either
sipConvertToInstance()
orsipForceConvertToInstance()
.- Parameters
cpp – the C/C++ instance.
type – the type’s generated type object.
state – describes the state of the C/C++ instance.
-
void sipReleaseMappedType(void *cpp, const sipMappedType *mt, int state)¶
Deprecated since version 4.8: Use
sipReleaseType()
instead.This destroys a wrapped C/C++ mapped type if it was a temporary instance. It is called after a call to either
sipConvertToMappedType()
orsipForceConvertToMappedType()
.- Parameters
cpp – the C/C++ instance.
mt – the opaque structure returned by
sipFindMappedType()
.state – describes the state of the C/C++ instance.
-
void sipReleaseType(void *cpp, const sipTypeDef *td, int state)¶
This destroys a wrapped C/C++ or mapped type instance if it was a temporary instance. It is called after a call to either
sipConvertToType()
orsipForceConvertToType()
.- Parameters
cpp – the C/C++ instance.
td – the type’s generated type structure.
state – describes the state of the C/C++ instance.
-
const char *sipResolveTypedef(const char *name)¶
This returns the value of a C/C++ typedef.
- Parameters
name – the name of the typedef.
- Returns
the value of the typedef or
NULL
if there was no such typedef.
-
void sipSetDestroyOnExit(int destroy)¶
New in version 4.14.7.
When the Python interpreter exits it garbage collects those objects that it can. This means that any corresponding C++ instances and C structures owned by Python are destroyed. Unfortunately this happens in an unpredictable order and so can cause memory faults within the wrapped library. Calling this function with a value of zero disables the automatic destruction of C++ instances and C structures.
- Parameters
destroy – non-zero if all C++ instances and C structures owned by Python should be destroyed when the interpreter exits. This is the default.
-
void sipSetTypeUserData(sipWrapperType *type, void *data)¶
New in version 4.19.
Each generated type corresponding to a wrapped C/C++ type, or a user sub-class of such a type, contains a pointer for the use of handwritten code. This sets the value of that pointer.
- Parameters
type – the type object.
data – the type-specific pointer.
-
void sipSetUserObject(sipSimpleWrapper *obj, PyObject *user)¶
New in version 4.19.
Each wrapped object can contain a reference to a single Python object that can be used for any purpose by handwritten code and will automatically be garbage collected at the appropriate time. This sets that object.
- Parameters
obj – the wrapped object.
user – a borrowed reference to the user object.
-
type sipSimpleWrapper¶
This is a C structure that represents a Python wrapped instance whose type is
sip.simplewrapper
. It is an extension of thePyObject
structure and so may be safely cast to it.When the limited Python API is enabled and Python v3.2 or later is being used then it is only available as an opaque (i.e. incomplete) type and the following members are not available.
-
void *data¶
This is initialised to the address of the C/C++ instance. If an access function is subsequently provided then it may be used for any purpose by the access function.
-
sipAccessFunc access_func¶
This is the address of an optional access function that is called, with a pointer to this structure as its first argument. If its second argument is
UnguardedPointer
then it returns the address of the C/C++ instance, even if it is known that its value is no longer valid. If the second argument isGuardedPointer
then it returns the address of the C++ instance or0
if it is known to be invalid. If the second argument isReleaseGuard
then the structure is being deallocated and any dynamic resources used by the access function should be released. If there is no access function then thesipSimpleWrapper.data
is used as the address of the C/C++ instance. Typically a custom meta-type is used to set an access method after the Python object has been created.
-
PyObject *user¶
This can be used for any purpose by handwritten code and will automatically be garbage collected at the appropriate time.
-
void *data¶
-
PyTypeObject *sipSimpleWrapper_Type¶
This is the type of a
sipSimpleWrapper
structure and is the C implementation ofsip.simplewrapper
. It may be safely cast tosipWrapperType
.When the limited Python API is enabled and Python v3.2 or later is being used then it is only available as an opaque (i.e. incomplete) type.
-
type sipStringTypeClassMap¶
Deprecated since version 4.8.
This C structure is used with
sipMapStringToClass()
to define a mapping between'\0'
terminated string based RTTI and Generated Type Objects. The structure elements are as follows.-
char *typeString¶
The
'\0'
terminated string RTTI.
- sipWrapperType **pyType.
A pointer to the corresponding generated type object.
-
char *typeString¶
-
type sipTimeDef¶
New in version 4.19.
This C structure is used with
sipGetTime()
,sipFromTime()
,sipGetDateTime()
andsipFromDateTime()
and encapsulates the components parts of a Python time. The structure elements are as follows.-
int pt_hour¶
The hour (0-23).
-
int pt_minute¶
The minute (0-59).
-
int pt_second¶
The second (0-59).
-
int pt_microsecond¶
The microsecond (0-999999).
-
int pt_hour¶
-
void sipTransferBack(PyObject *obj)¶
This transfers ownership of a Python wrapped instance to Python (see Ownership of Objects).
- Parameters
obj – the wrapped instance.
In addition, any association of the instance with regard to the cyclic garbage collector with another instance is removed.
-
void sipTransferBreak(PyObject *obj)¶
Any association of a Python wrapped instance with regard to the cyclic garbage collector with another instance is removed. Ownership of the instance should be with C++.
- Parameters
obj – the wrapped instance.
Deprecated since version 4.14: Use the following instead:
sipTransferTo(obj, NULL);
-
void sipTransferTo(PyObject *obj, PyObject *owner)¶
This transfers ownership of a Python wrapped instance to C++ (see Ownership of Objects).
- Parameters
obj – the wrapped instance.
owner – an optional wrapped instance that obj becomes associated with with regard to the cyclic garbage collector. If owner is
NULL
then no such association is made. If owner isPy_None
then obj is given an extra reference which is removed when the C++ instance’s destructor is called. If owner is the same value as obj then any reference cycles involving obj can never be detected or broken by the cyclic garbage collector. Responsibility for calling the C++ instance’s destructor is always transfered to C++.
-
PyTypeObject *sipTypeAsPyTypeObject(const sipTypeDef *td)¶
This returns a pointer to the Python type object that SIP creates for a generated type structure.
- Parameters
td – the type structure.
- Returns
the Python type object. If the type structure refers to a mapped type then
NULL
will be returned.
If the type structure refers to a C structure or C++ class then the Python type object may be safely cast to a
sipWrapperType
.
-
const sipTypeDef *sipTypeFromPyTypeObject(PyTypeObject *py_type)¶
This returns the generated type structure for a Python type object.
- Parameters
py_type – the Python type object.
- Returns
the type structure or
NULL
if the Python type object doesn’t correspond to a type structure.
-
int sipTypeIsClass(sipTypeDef *td)¶
This checks if a generated type structure refers to a C structure or C++ class.
- Parameters
td – the type structure.
- Returns
a non-zero value if the type structure refers to a structure or class.
-
int sipTypeIsEnum(sipTypeDef *td)¶
This checks if a generated type structure refers to a C-style named enum.
- Parameters
td – the type structure.
- Returns
a non-zero value if the type structure refers to a C-style named enum.
-
int sipTypeIsMapped(sipTypeDef *td)¶
This checks if a generated type structure refers to a mapped type.
- Parameters
td – the type structure.
- Returns
a non-zero value if the type structure refers to a mapped type.
-
int sipTypeIsNamespace(sipTypeDef *td)¶
This checks if a generated type structure refers to a C++ namespace.
- Parameters
td – the type structure.
- Returns
a non-zero value if the type structure refers to a namespace.
-
int sipTypeIsScopedEnum(sipTypeDef *td)¶
New in version 4.19.4.
This checks if a generated type structure refers to a C++11 scoped enum.
- Parameters
td – the type structure.
- Returns
a non-zero value if the type structure refers to a C++11 scoped enum.
-
const char *sipTypeName(const sipTypeDef *td)¶
This returns the C/C++ name of a wrapped type.
- Parameters
td – the type’s generated type structure.
- Returns
the name of the C/C++ type.
-
const sipTypeDef *sipTypeScope(const sipTypeDef *td)¶
This returns the generated type structure of the enclosing scope of another generated type structure.
- Parameters
td – the type structure.
- Returns
the type structure of the scope or
NULL
if the type has no scope.
-
void *sipUnicodeData(PyObject *obj, int *char_size, SIP_SSIZE_T *len)¶
New in version 4.19.
This returns information about the contents of a Python unicode object.
This is only supported for Python v3.3 and later.
- Parameters
obj – the unicode object.
char_size – a pointer which will be updated with the number of bytes (either 1, 2 or 4) used to store a character. If there was an error then this will be a negative value.
len – a pointer which will be updated with the number of characters (not bytes) in the unicode object.
- Returns
the address of the buffer where the characters are stored. It will be undefined if the returned character size is a negative value.
-
PyObject *sipUnicodeNew(SIP_SSIZE_T len, unsigned maxchar, int *kind, void **data)¶
New in version 4.19.
This creates a Python unicode object that will hold a set number of characters, each character being of a certain size.
This is only supported for Python v3.3 and later.
- Parameters
len – the number of characters.
maxchar – the largest code point that will be placed in the object.
kind – a pointer which will be updated with a value that represents the number of bytes (either 1, 2 or 4) used to store a character.
data – a pointer which will be updated with the address of the buffer where the characters will be stored.
- Returns
the unicode object or
NULL
if there was an error.
-
void sipUnicodeWrite(int kind, void *data, int index, unsigned value)¶
New in version 4.19.
This updates the buffer of a Python unicode object with a character at a particular position.
This is only supported for Python v3.3 and later.
- Parameters
kind – the value that represents the number of bytes (either 1, 2 or 4) used to store a character.
data – the address of the buffer where the characters are stored.
index – the character (not byte) index of the character to be updated.
value – the value of the new character.
-
void sipVisitWrappers(sipWrapperVisitorFunc visitor, void *closure)¶
New in version 4.19.19.
This calls a visitor function for every wrapper object.
- Parameters
visitor – the visitor function.
closure – a pointer that is passed to the visitor.
A visitor has the following signature.
void visitor(sipSimpleWrapper *obj, void *closure)
obj is the wrapped object being visited.
closure is the pointer passed to
sipVisitWrappers()
.
-
PyTypeObject *sipVoidPtr_Type¶
This is the type of a
PyObject
structure that is used to wrap avoid *
.
-
type sipWrapper¶
This is a C structure that represents a Python wrapped instance whose type is
sip.wrapper
. It is an extension of thesipSimpleWrapper
andPyObject
structures and so may be safely cast to both.When the limited Python API is enabled and Python v3.2 or later is being used then it is only available as an opaque (i.e. incomplete) type.
-
int sipWrapper_Check(PyObject *obj)¶
Deprecated since version 4.8: Use the following instead:
PyObject_TypeCheck(obj, sipWrapper_Type)
This checks if a Python object is a wrapped instance.
- Parameters
obj – the Python object.
- Returns
a non-zero value if the Python object is a wrapped instance.
-
PyTypeObject *sipWrapper_Type¶
This is the type of a
sipWrapper
structure and is the C implementation ofsip.wrapper
. It may be safely cast tosipWrapperType
.
-
type sipWrapperType¶
This is a C structure that represents a SIP generated type object. It is an extension of the
PyTypeObject
structure (which is itself an extension of thePyObject
structure) and so may be safely cast toPyTypeObject
(andPyObject
).When the limited Python API is enabled and Python v3.2 or later is being used then it is only available as an opaque (i.e. incomplete) type.
-
PyTypeObject *sipWrapperType_Type¶
This is the type of a
sipWrapperType
structure and is the C implementation ofsip.wrappertype
.
Event Handlers¶
New in version 4.19.3.
The sip
module will trigger a number of events. Handwritten code can
supply handlers for these events to allow it to perform additional actions.
Each event has a type, described by the sipEventType
enum. An
event handler is registered using sipRegisterEventHandler()
. The
signature of an event handler is specific to the event type.
-
enum sipEventType¶
This is the enum that defines the different event types.
-
int sipRegisterEventHandler(sipEventType type, const sipTypeDef *td, void *handler)¶
This registers an event handler which will be called whenever an event is triggered.
- Parameters
type – the event type for which the handler is registered.
td – the generated type structure - the handler will only be invoked for Python object corresponding to this type or a sub-type.
handler – the handler that is called when the event is triggered.
- Returns
0 if there was no error, otherwise -1 is returned (and a Python exception is raised).
Generated Type Structures¶
SIP generates an opaque type structure for each C structure, C++ class, C++
namespace, named enum or mapped type being wrapped. These are
sipTypeDef
structures and are used extensively by the SIP API.
The names of these structure are prefixed by sipType_
.
For those structures that correspond to C structures, C++ classes, C++
namespaces or named enums the remaining part of the name is the fully
qualified name of the structure, class, namespace or enum name. Any ::
scope separators are replaced by an underscore. For example, the type object
for class Klass
is sipType_Klass
.
For those structure that correspond to mapped types the remaining part of the
name is generated by SIP. The only way for handwritten code to obtain a
pointer to a structure for a mapped type is to use sipFindType()
.
The type structures of all imported types explicitly used by a module are available to handwritten code.
Generated Type Objects¶
Deprecated since version 4.8: Use the corresponding generated type structure (see
Generated Type Structures) and sipTypeAsPyTypeObject()
instead.
SIP generates a sipWrapperType
type object for each C structure or
C++ class being wrapped.
These objects are named with the structure or class name prefixed by
sipClass_
. For example, the type object for class Klass
is
sipClass_Klass
.
Generated Named Enum Type Objects¶
Deprecated since version 4.8: Use the corresponding generated type structure (see
Generated Type Structures) and sipTypeAsPyTypeObject()
instead.
SIP generates a type object for each named enum being wrapped. These are PyTypeObject structures. (Anonymous enums are wrapped as Python integers.)
These objects are named with the fully qualified enum name (i.e. including any
enclosing scope) prefixed by sipEnum_
. For example, the type object for
enum Enum
defined in class Klass
is sipEnum_Klass_Enum
.
Generated Derived Classes¶
For most C++ classes being wrapped SIP generates a derived class with the same
name prefixed by sip
. For example, the derived class for class Klass
is sipKlass
.
If a C++ class doesn’t have any virtual or protected methods in it or any of it’s super-class hierarchy, or does not emit any Qt signals, then a derived class is not generated.
Most of the time handwritten code should ignore the derived classes. The only
exception is that handwritten constructor code specified using the
%MethodCode
directive should call the derived class’s constructor
(which has the same C++ signature) rather then the wrapped class’s constructor.
Generated Exception Objects¶
SIP generates a Python object for each exception defined with the
%Exception
directive.
These objects are named with the fully qualified exception name (i.e. including
any enclosing scope) prefixed by sipException_
. For example, the type
object for enum Except
defined in class Klass
is
sipException_Klass_Except
.
The objects of all imported exceptions are available to handwritten code.