[PyKDE] sipping char[N] datatypes in structs

Jim Bublitz jbublitz at nwinternet.com
Fri May 16 17:46:01 BST 2003


On Friday 16 May 2003 01:33, Phil Thompson wrote:
> > This sucker won't sip, though:

> > struct PGnotify
> > {
> > 	char		relname[NAMEDATALEN];
> > 	int			be_pid;
> > };

> > If I do "char *relname", then it won't compile because char
> > * is not the same as char[32] (NAMEDATALEN is defined to be
> > 32).

> > Any tips?

Yes - avoid these at all costs :)

There are some of these in KDE, but they're in socket-related 
classes which already have good Python support, so I haven't 
done bindings for them in PyKDE.

> SIP doesn't support arrays (as you have found). In fact I've
> never used SIP to wrap a C library, so I'm pleasently
> surprised you've got as far as you have.

> The only thing I can think of at the moment is to create a
> thin C++ class library that implements exactly the API you
> want and SIP that - but it's obviously not the bare metal.

I couldn't think of anything else either, which is why I waited 
for Phil to reply. I'd also point out is that in converting 
Python -> C with fixed arrays, you need to prevent possible 
buffer overflows.

You could try:

%MappedType PGNotify
...

where the mapped type takes or returns a tuple. You won't be able 
to instantiate PGNotify in Python at all, but you can build a 
tuple of values the same as the struct and pass/return that 
from/to Python wherever PGNotify is used. There are lots of 
mapped type examples in PyQt and PyKDE to get you started. In 
that case, you need to write C/C++ for handling the fixed array 
inside the mapped type conversion code.

The other thing I'd point out (won't help here, but may be useful 
for other stuff) is that you can also treat structs as classes:

struct SomeStruct { ... };

is the same as:

class SomeStruct 
{ 
public:  // structs default to public, classes to private
   ...
};

PyKDE is done that way and in those cases the result seems more 
natural in Python. It's mostly my personal preference though.

Jim 




More information about the PyQt mailing list