[PyKDE] SIP: adding code to methods?

Phil Thompson phil at riverbankcomputing.co.uk
Wed Mar 22 00:03:07 GMT 2006


On Tuesday 21 March 2006 2:22 pm, Giovanni Bajo wrote:
> Hello,
>
> I'm wrapping a C++ class which is defined like this:
>
> class Foo
> {
> public:
>      [ many methods that can fail...]
>      int Error();
> };
>
> The idea is that instead of returning error codes, the C++ methods silenty
> succeed, and whenever the client sees fit, he can check Error() to see if
> there were errors. What I wanted to do is to generate some code in the SIP
> module so that Error() is automatically called after each method
> invokation, and if it returns non-zero, a Python exception is raised.
>
> I don't know how to do this in SIP. In details:
>
> - I was thinking of defining a common utility function with %TypeCode, and
> then calling it for each method. How can I define C++ code which is
> executed after the method invokation? I don't want to use %MethodCode
> because I don't want to rewrite the method invokation code itself, since
> the default is perfectly fine.

You might be able to use the PostHook annotation. Generally though, SIP wraps 
an API - if you don't like the API then change it.

> - How can I make my module export a custom Python exception class (the
> equivalent of "class MyException(Exception): pass") which has no match in
> the C++ code?

You just need to define a new type/class in C++ and add it to the module 
dictionary - probably using %PostInitialisationCode.

At the moment I don't document any of the things that might be available to 
post-init code - but I'm happy to do so (like a reference to the module and 
it's dictionary).

> - Should I use the %Exception support instead? If so, how? The
> documentation for exception support is sparse at best.

I don't think this is what you want.

Phil




More information about the PyQt mailing list