In this section we describe the API that can be used by handwritten code in specification files.
New in version 4.10.
This is a C preprocessor macro that is set automatically by the build system to specify that the generated code is being compiled with protected redefined as public. This allows handwritten code to determine if the generated helper functions for accessing protected C++ functions are available (see %MethodCode).
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: |
|
---|---|
Returns: | the value that should be assigned to sipError. |
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.
Parameter: | method – the Python method and would normally be the supplied sipMethod. |
---|
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: |
|
---|
This creates a Python object based on a format string and associated values in a similar way to the Python Py_BuildValue() function.
Parameters: |
|
---|---|
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.
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 is NULL and the instance has already been wrapped then the ownership is unchanged. If it is NULL or Py_None then ownership will be with Python. Otherwise ownership will be with C/C++ and the instance associated with the PyObject * argument. The Python class is influenced by any applicable %ConvertToSubClassCode code.
Note
This is deprecated from SIP v4.8. Instead you should use N.
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 is NULL and the instance has already been wrapped then the ownership is unchanged. If it is NULL and the instance is newly wrapped then ownership will be with C/C++. If it is Py_None then ownership is transferred to Python via a call to sipTransferBack(). Otherwise ownership is transferred to C/C++ and the instance associated with the PyObject * argument via a call to sipTransferTo(). The Python class is influenced by any applicable %ConvertToSubClassCode code.
Note
This is deprecated from SIP v4.8. Instead you should use D.
Convert a named C/C++ enum to an instance of the corresponding Python named enum type.
Note
This is deprecated from SIP v4.8. Instead you should use F.
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: |
|
---|---|
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 supplied sipMethod.
This checks if a Python object can be converted to a named enum.
Parameters: |
|
---|---|
Returns: | a non-zero value if the object can be converted. |
This checks if a Python object can be converted to an instance of a C structure or C++ class.
Parameters: |
|
---|---|
Returns: | a non-zero value if the object can be converted. |
Note
This is deprecated from SIP v4.8. Instead you should use sipCanConvertToType().
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: |
|
---|---|
Returns: | a non-zero value if the object can be converted. |
Note
This is deprecated from SIP v4.8. Instead you should use sipCanConvertToType().
This checks if a Python object can be converted to an instance of a C structure, C++ class or mapped type.
Parameters: |
|
---|---|
Returns: | a non-zero value if the object can be converted. |
This gets the class name of a wrapped instance as a Python string. It comes with a reference.
Parameter: | obj – the wrapped instance. |
---|---|
Returns: | the name of the instance’s class. |
Note
This is deprecated from SIP v4.8. Instead you should use the following:
PyString_FromString(obj->ob_type->tp_name)
This creates a sip.voidptr object for a memory address. The object will not be writeable and has no associated size.
Parameter: | cpp – the memory address. |
---|---|
Returns: | the sip.voidptr object. |
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: |
|
---|---|
Returns: | the sip.voidptr object. |
This converts a named C/C++ enum to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
Returns: | the Python object. |
This converts a C structure or a C++ class instance to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
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 to sipTransferBack().
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.
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertFromType().
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: |
|
---|---|
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 to sipTransferBack().
Otherwise ownership is transferred to C/C++ and the instance associated with transferObj argument via a call to sipTransferTo().
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertFromType().
This converts a named C/C++ enum to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
Returns: | the Python object. |
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertFromEnum().
This converts a new C structure or a C++ class instance to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
Returns: | the Python object. |
If transferObj is NULL or Py_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.
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertFromNewType().
This converts a new C structure or a C++ class instance to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
Returns: | the Python object. |
If transferObj is NULL or Py_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.
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: |
|
---|---|
Returns: | the unsigned array index. |
This converts a C structure or a C++ class instance to an instance of the corresponding generated Python type.
Parameters: |
|
---|---|
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 to sipTransferBack().
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.
This creates a sip.voidptr object for a memory address. The object will be writeable but has no associated size.
Parameter: | cpp – the memory address. |
---|---|
Returns: | the sip.voidptr object. |
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: |
|
---|---|
Returns: | the sip.voidptr object. |
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: |
|
---|---|
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 to sipTransferBack().
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 calling sipReleaseInstance().
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.)
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertToType().
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: |
|
---|---|
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 to sipTransferBack().
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 calling sipReleaseMappedType().
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.)
Note
This is deprecated from SIP v4.8. Instead you should use sipConvertToType()
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: |
|
---|---|
Returns: | the C/C++ instance. |
If transferObj is NULL then the ownership is unchanged. If it is Py_None then ownership is transferred to Python via a call to sipTransferBack().
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 calling sipReleaseType().
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.)
This converts a Python object to a memory address. PyErr_Occurred() must be used to determine if the conversion was successful.
Parameter: | obj – the Python object which may be Py_None, a sip.voidptr or a PyCObject. |
---|---|
Returns: | the memory address. |
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: |
|
---|---|
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. |
This returns a pointer to the generated type object corresponding to a C/C++ type.
Parameter: | 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. |
Note
This is deprecated from SIP v4.8. Instead you should use sipFindType().
This returns a pointer to an opaque structure describing a mapped type.
Parameter: | 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. |
Note
This is deprecated from SIP v4.8. Instead you should use sipFindType().
This returns a pointer to the generated Python type object corresponding to a named C/C++ enum.
Parameter: | 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++ enum doesn’t exist. |
Note
This is deprecated from SIP v4.8. Instead you should use sipFindType().
This returns a pointer to the generated type structure corresponding to a C/C++ type.
Parameter: | 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. |
This converts a Python object to an instance of a C structure or C++ class by calling sipCanConvertToInstance() and, if it is successfull, calling sipConvertToInstance().
See sipConvertToInstance() for a full description of the arguments.
Note
This is deprecated from SIP v4.8. Instead you should use sipForceConvertToType().
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, calling sipConvertToMappedType().
See sipConvertToMappedType() for a full description of the arguments.
Note
This is deprecated from SIP v4.8. Instead you should use sipForceConvertToType().
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, calling sipConvertToType().
See sipConvertToType() for a full description of the arguments.
This returns an area of memory allocated by sipMalloc() to the heap.
Parameter: | mem – the memory address. |
---|
This returns a borrowed reference to the Python object for a C structure or C++ class instance.
Parameters: |
|
---|---|
Returns: | the Python object or NULL (and no exception is raised) if the C/C++ instance hasn’t been wrapped. |
The %ConvertToTypeCode directive requires that the provided code returns an int 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.
Parameter: | transferObj – the object that describes the requested transfer of ownership. |
---|---|
Returns: | the state of the converted value. |
This returns a borrowed reference to the wrapped instance object for a C structure or C++ class instance.
Parameters: |
|
---|---|
Returns: | the Python object or NULL (and no exception is raised) if the C/C++ instance hasn’t been wrapped. |
Note
This is deprecated from SIP v4.8. Instead you should use sipGetPyObject().
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.
Parameter: | name – the name of the symbol. |
---|---|
Returns: | the value of the symbol. NULL is returned if there is no such symbol. |
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.
Note
This is deprecated from SIP v4.8.
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: |
|
---|---|
Returns: | a non-zero value if the API is enabled. |
This allocates an area of memory on the heap using the Python PyMem_Malloc() function. The memory is freed by calling sipFree().
Parameter: | 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. |
This can be used in %ConvertToSubClassCode code as a convenient way of converting integer based RTTI to the corresponding generated type object.
Parameters: |
|
---|---|
Returns: | the corresponding type object, or NULL if type wasn’t in map. |
Note
This is deprecated from SIP v4.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: |
|
---|---|
Returns: | the corresponding type object, or NULL if type wasn’t in map. |
Note
This is deprecated from SIP v4.8.
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: |
|
---|---|
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 supplied sipMethod and result being the value returned by sipCallMethod().
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.
Convert a Python v2 string object or a Python v3 bytes object to a C/C++ '\0' terminated string. If the Python object is Py_None then the string is NULL.
Note
This is deprecated from SIP v4.8. Instead you should use B.
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 adding 0 to the combined value:
0x01 disallows the conversion of Py_None to NULL
- 0x02 implements the Factory and TransferBack
- 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.
Note
This is deprecated from SIP v4.8. Instead you should use Hf.
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 adding 0 to the combined value:
0x01 disallows the conversion of Py_None to NULL
- 0x02 implements the Factory and TransferBack
- 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.
Note
This is deprecated from SIP v4.10.1. Instead you should use Hf.
Convert a Python named enum type to the corresponding C/C++ enum.
Note
This is deprecated from SIP v4.8. Instead you should use F.
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 adding 0 to the combined value:
0x01 disallows the conversion of Py_None to NULL
- 0x02 implements the Factory and TransferBack
- annotations
0x04 returns a copy of the C/C++ instance.
This registers a handler that will called just before SIP needs to get an attribute from a wrapped type’s dictionary for the first time. The handler must then populate the type’s dictionary with any lazy attributes.
Parameters: |
|
---|---|
Returns: | 0 if there was no error, otherwise -1 is returned. |
If td is not NULL then the handler will only be called for types with that type or that are sub-classed from it. Otherwise the handler will be called for all types.
A handler has the following signature.
int handler(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 if there was no error, otherwise -1 is returned.
See the section Lazy Type Attributes for more details.
This registers a Python type object that can be used as the meta-type or super-type of a wrapped C++ type.
Parameter: | 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.
This destroys a wrapped C/C++ instance if it was a temporary instance. It is called after a call to either sipConvertToInstance() or sipForceConvertToInstance().
Parameters: |
|
---|
Note
This is deprecated from SIP v4.8. Instead you should use sipReleaseType().
This destroys a wrapped C/C++ mapped type if it was a temporary instance. It is called after a call to either sipConvertToMappedType() or sipForceConvertToMappedType().
Parameters: |
|
---|
Note
This is deprecated from SIP v4.8. Instead you should use sipReleaseType().
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() or sipForceConvertToType().
Parameters: |
|
---|
This returns the value of a C/C++ typedef.
Parameter: | name – the name of the typedef. |
---|---|
Returns: | the value of the typedef or NULL if there was no such typedef. |
This is a C structure that represents a Python wrapped instance whose type is sip.simplewrapper. It is an extension of the PyObject structure and so may be safely cast to it.
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.
Note
This is deprecated from SIP v4.8.
This transfers ownership of a Python wrapped instance to Python (see Ownership of Objects).
Parameter: | obj – the wrapped instance. |
---|
In addition, any association of the instance with regard to the cyclic garbage collector with another instance is removed.
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++.
Parameter: | obj – the wrapped instance. |
---|
This transfers ownership of a Python wrapped instance to C++ (see Ownership of Objects).
Parameters: |
|
---|
This returns a pointer to the Python type object that SIP creates for a generated type structure.
Parameter: | 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.
This returns the generated type structure for a Python type object.
Parameter: | py_type – the Python type object. |
---|---|
Returns: | the type structure or NULL if the Python type object doesn’t correspond to a type structure. |
This checks if a generated type structure refers to a C structure or C++ class.
Parameter: | td – the type structure. |
---|---|
Returns: | a non-zero value if the type structure refers to a structure or class. |
This checks if a generated type structure refers to a named enum.
Parameter: | td – the type structure. |
---|---|
Returns: | a non-zero value if the type structure refers to an enum. |
This checks if a generated type structure refers to a mapped type.
Parameter: | td – the type structure. |
---|---|
Returns: | a non-zero value if the type structure refers to a mapped type. |
This checks if a generated type structure refers to a C++ namespace.
Parameter: | td – the type structure. |
---|---|
Returns: | a non-zero value if the type structure refers to a namespace. |
This returns the C/C++ name of a wrapped type.
Parameter: | td – the type’s generated type structure. |
---|---|
Returns: | the name of the C/C++ type. |
This returns the generated type structure of the enclosing scope of another generated type structure.
Parameter: | td – the type structure. |
---|---|
Returns: | the type structure of the scope or NULL if the type has no scope. |
This checks if a Python object is a wrapped instance.
Parameter: | obj – the Python object. |
---|---|
Returns: | a non-zero value if the Python object is a wrapped instance. |
Note
This is deprecated from SIP v4.8. Instead you should use the following:
PyObject_TypeCheck(obj, sipWrapper_Type)
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 are available to handwritten code.
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.
Note
Using these names is deprecated from SIP v4.8. Instead use the corresponding generated type structure (see Generated Type Structures) and sipTypeAsPyTypeObject().
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.
Note
Using these names is deprecated from SIP v4.8. Instead use the corresponding generated type structure (see Generated Type Structures) and sipTypeAsPyTypeObject().
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 TQt 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.
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.