[PyKDE] sip and const void * typedefs.

Jon jon at totient.co.uk
Sun Mar 12 00:55:20 GMT 2006


Hi

I've been using sip (successfully) with library which defines things like
typedef const void * InstanceHandle;
typedef const void * SessionHandle;

and then has functions like
	InstanceHandle getInstance(bool);
	SessionHandle createSession(InstanceHandle,int,int);
I constructed a sip file with those definitions but found that the

The problem I found was that the generated code would not compile with an 
error
	error: invalid conversion from 'const void*' to 'void*'

and this is because of the sipConvertFromVoidPtr in the generated code:-

static PyObject *func_getInstance(PyObject *,PyObject *sipArgs)
{
        int sipArgsParsed = 0;

        {
                bool a0 = 0;

                if (sipParseArgs(&sipArgsParsed,sipArgs,"|b",&a0))
                {
                        const void *sipRes;

                        sipRes = getInstance(a0);

                        return sipConvertFromVoidPtr(sipRes);
                }
        }

        /* Raise an exception if the arguments couldn't be parsed. */
        sipNoFunction(sipArgsParsed,sipNm_TestMod_getInstance);

        return NULL;
}

I fixed this problem by defining MappedTypes based on the examples in the doc 
for the InstanceHandle etc and removing the typedef const void * 
InstanceHandle in the sip file;

%MappedType InstanceHandle
{
%TypeHeaderCode
#include <someApi.h>
%End

%ConvertToTypeCode
	// from python
if (sipIsErr == NULL)
        return PyInt_Check(sipPy);

  
if (sipPy == Py_None)
{
        *sipCppPtr = 0;
	return 0;
}
InstanceHandle * handle = new InstanceHandle;
PyErr_Clear();
*handle = (InstanceHandle)PyInt_AsLong(sipPy);
if (PyErr_Occurred() != NULL){
	delete handle;
	*sipIsErr =1;
	return 0;
}
*sipCppPtr = handle;
return 1;
%End	

%ConvertFromTypeCode
if (!sipCpp)
	return NULL;
PyErr_Clear();

PyObject* l = PyInt_FromLong((long)*sipCpp);

if (PyErr_Occurred() != NULL){
	Py_DECREF(l);
	return 0;
}
return l;
%End
};

This is with sip 4.2.1 (4.2.1-297) as packaged by Mandriva

My main question is, is there an easier way to do this?


Had the library I am wrapping used instead unsigned int for the these Handle 
typedefs, then as far I as can tell I would not have had to do anything other 
than

typedef unsigned int InstanceHandle;
typedef unsigned int SessionHandle;
InstanceHandle getInstance(bool);
SessionHandle createSession(InstanceHandle,int,int);

and the generated code would work and no MappedType is needed.


Obviously if I could change the library API I would, but it's not mine........

I actually have most of the API working but thought I'd check on this minor 
niggle.

Thanks

Jon




More information about the PyQt mailing list