Conversions and argument parsing

Table of Contents
sipParseArgs — Parse the arguments to a C/C++ function without any side effects
sipConvertToCpp — Convert a Python instance of a class to a C/C++ object pointer
sipMapCppToSelf — Convert a C/C++ pointer to a Python instance
sipConvertToVoidPtr — A convenience function to convert a C/C++ void pointer from a Python object
sipConvertFromVoidPtr — A convenience function to convert a C/C++ void pointer to a Python object
sipConvertFromBool — A convenience function to convert a C/C++ boolean to a Python object
sipCheckNone — Check a None argument for a class pointer that we might dereference
sipBadVirtualResultType — Report a Python member function with an unexpected return type
sipBadSetType — Report a Python class variable with an unexpected type

sipParseArgs

Name

sipParseArgs -- Parse the arguments to a C/C++ function without any side effects

Synopsis

int sipParseArgs(int * argsParsedp, PyObject * sipArgs, char * fmt, ...);

Description

Return Value

false (or 0) if an error occurred, else true (or 1).

argsParsedp

Parsing stops with if an error is encountered or all arguments / format specifiers are exhausted. The number of arguments parsed so far is stored here along with error flags.

Table 5-1. Error flags in sipParseArgs()

Flag NameMeaning
PARSE_OKParse is Ok so far
PARSE_MANYToo many arguments
PARSE_FEWToo few arguments
PARSE_TYPEArgument with a bad type
PARSE_MASKMask covering the error flag bits

sipArgs

A pointer to a tuple which supplies the arguments to be parsed.

fmt

Format string describing arguments. A leading '-' in the format string disposes of the arguments on a successful parse. A '|' in the format string signifies, that the following arguments are optional. The following format specifiers are recognized:

Table 5-2. Format specifiers for sipParseArgs()

fmtOperand typeexpected C argument(s)
sString or Nonechar **
SSlot name, return the namechar **
GSignal name, return the namechar **
IClass instanceint (*convfunc)(PyObject *), PyObject **
OPython object of any typePyObject **
TPython object of given typePyTypeObject *, PyObject **
RSub-class of QObjectPyObject **
FPython callable objectPyObject **
aByte array or Nonechar **, int *
cCharacterchar *
iIntegerint *
hShort integershort *
lLong integerlong *
fFloatfloat *
dDouble floatdouble *
vVoid pointervoid **

...

A variable number of pointers to the arguments which will take the parsed values.

Examples

Example 5-1. Interface for QRegExp::match

    // Attempts to match in str, starting from position index. 
    // Returns the position of the match, or -1 if there was no match.
    // if len is not a null pointer, the length of the match is stored in *len. 
    int match(const char* str, int index=0, int*=0) const;
%MemberCode
        // The Python interface returns the position and length as a tuple.
        const char *str;
        int index = 0;

        if (sipParseArgs(&sipArgsParsed, sipArgs, "s|i", &str, &index))
        {
            int pos, len;
            QRegExp *ptr;

            if ((ptr = (QRegExp*) sipGetCppPtr(sipThis, sipClass_QRegExp)) == NULL)
                return NULL;
            pos = ptr -> QRegExp::match(str, index, &len);
            return Py_BuildValue("(ii)", pos, len);
        }
%End
				

sipConvertToCpp

Name

sipConvertToCpp -- Convert a Python instance of a class to a C/C++ object pointer

Synopsis

sipConvertToCpp sipConvertToCpp(PyObject * sipSelf, PyObject * baseclass, int * iserrp);

Description

Return Value

A pointer to the C++ class or NULL in case of an error (e.g. the instance's class is not derived from the given baseclass.

sipSelf

A pointer o the Python object instance.

baseclass

The base class of the Python object instance.

iserrp

Store TRUE here if we had an error.

sipMapCppToSelf

Name

sipMapCppToSelf -- Convert a C/C++ pointer to a Python instance

Synopsis

PyObject * sipMapCppToSelf(const void * cppPtr, PyObject * pyClass);

Description

If the C/C++ pointer is recognised and it is an instance of a sub-class of the expected class then the previously wrapped instance is returned. Otherwise a new Python instance is created with the expected class. The instance comes with a reference.

Return Value

A pointer to the Python object instance (or Py_None if a NULL pointer was passed in).

cppPtr

A pointer to the C++ object.

pyClass/title>

The expexted Python class.

sipConvertToVoidPtr

Name

sipConvertToVoidPtr -- A convenience function to convert a C/C++ void pointer from a Python object

Synopsis

void * sipConvertToVoidPtr(PyObject * obj);

Description

Return Value

The C/C++ pointer (or NULL if the object was Py_None).

obj

The Python object

sipConvertFromVoidPtr

Name

sipConvertFromVoidPtr -- A convenience function to convert a C/C++ void pointer to a Python object

Synopsis

PyObject * sipConvertFromVoidPtr(void * val);

Description

Return Value

A pointer to the Python object (or Py_None if val was a NULL pointer).

val

The C/C++ pointer.

sipConvertFromBool

Name

sipConvertFromBool -- A convenience function to convert a C/C++ boolean to a Python object

Synopsis

PyObject * sipConvertFromBool(int val);

Description

Return Value

Py_True or Py_False, depending on val.

val

The value to evaluate.

sipCheckNone

Name

sipCheckNone -- Check a None argument for a class pointer that we might dereference

Synopsis

void sipCheckNone(int willDeref, int * isErr, char * classname);

Description

Report a Python runtime error with this message: Cannot pass None as a classname argument in this call

willDeref

If this is TRUE, the error is generated.

isErr

Store TRUE here if the error is generated.

classname/title>

This goes into the error message.

sipBadVirtualResultType

Name

sipBadVirtualResultType -- Report a Python member function with an unexpected return type

Synopsis

void sipBadVirtualResultType(char * classname, char * method);

Description

Report a Python type error with this mesage: Invalid result type from classname.method();

classname

Classname used in error message.

method

Method name used in error message.

sipBadSetType

Name

sipBadSetType -- Report a Python class variable with an unexpected type

Synopsis

voidsipBadSetType(char * classname, char * var);

Description

Report a Python type error with this mesage: Invalid type for variable classname.var

classname

Classname used in error message.

var

Variable name used in error message.