[PyKDE] SIP: adding code to methods?

Phil Thompson phil at riverbankcomputing.co.uk
Wed Mar 22 15:36:18 GMT 2006


On Wednesday 22 March 2006 1:47 pm, Giovanni Bajo wrote:
> Phil Thompson <phil at riverbankcomputing.co.uk> wrote:
> >> - 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.
>
> I can't use PostHook as it can't get any argument. Maybe a %PostCode and a
> %PreCode (in which users might call sipCallHook, if they need) would be
> more powerful.
>
> >> - 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.
>
> Would exec work for this?

exec?

> And once it's in the module dictionary, how do I 
> access its type from a %MethodCode so to call PyErr_Format()?
> [sorry, I'm pretty newbie at Python API].

SIP generates something to access the type - see "Generated Exception Objects" 
in the SIP docs. Otherwise you can always look it up in the module 
dictionary.

Phil




More information about the PyQt mailing list