[PyKDE] More sip on C fun!

Phil Thompson phil at riverbankcomputing.co.uk
Tue May 20 09:38:01 BST 2003


On Tuesday 20 May 2003 3:22 am, Jonathan Gardner wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I have three questions about using sip to wrap C->C++. I have done all the
> "easy" stuff. If anyone would like to see my code, I'll post it.
>
> 1) I am running into a bit of a problem translating a struct to sip.
>
> The struct in question has an attribute 'char **fieldNames' -- which is
> meant to be a null-terminated list of null-terminated strings. Of course,
> sip doesn't know what to do with this.
>
> Is there something akin to '%MemberCode' that I can put after the attribute
> that allows me to specify how to set and get the value 'fieldNames'? Or, do
> I need to wrap it with a C++ class that doesn't provide a direct interface
> to fieldNames?

%VariableCode

> 2) One function requires a struct as an argument. This struct is so deep
> into C it doesn't make any sense to translate it to Python at all (it has a
> union with a void pointer and an integer for one of the attributes, and
> half the other attributes describe the length and types of the other
> attributes). Can I just invent a function that takes a dict or a list as an
> argument in place of the struct? How would I specify this?
>
> Here is the prototype for reference (wrapped as far as I dare go in C++)
>
> class Conn {
> 	...
> 	public:
> 	result *fn(int fnid, int *result_buf, int *result_len, int result_is_int,
> 		const PQArgBlock *args, int nargs);
> 	...
> };

If you provide %MemberCode for a function then you can do anything you want. 
The function doesn't have to exist in C++. SIP ignores the argument list of a 
function with %MemberCode.

> 3) There is a callback function that I would like to use. How would I go
> about doing this in sip? What kind of function should I put as the
> callback? What kind of arguments should I expect from sip?
>
> Let's just keep it simple:
>
> The callback prototype: typedef void (*callback_func)(int);
>
> The "set callback" prototype: int set_callback(callback_func *);
> (And there is a corresponding release_callback() function as well, of
> course).
>
> What I would like to see in python:
>
> def callback(x):	print x
> set_callback(callback)
>
> how do I get from C to Python via sip?

You will have to store the Python callback object somewhere (remembering that 
there may be several of them around at a time). You will have to provide a C 
callback that is permanently installed which will find the correct Python 
callback object and use the standard Python API to call it - after having 
converted the C callback arguments to the appropriate Python equivalents.

Phil




More information about the PyQt mailing list