[PyQt] Handling python exceptions in virtual overrides

Phil Thompson phil at riverbankcomputing.com
Fri Sep 5 09:14:55 BST 2008


On Wed, 3 Sep 2008 14:31:38 -0700, Matt Newell <newellm at blur.com> wrote:
> 
> If I understand correctly, there is currently no way to "handle" python 
> exceptions that occur inside a python member function when called via a
> c++ 
> virtual function.
> 
> %MethodCode is insufficient because it is never called when the member 
> function is called via a c++ virtual function.
> 
> %VirtualCatcherCode is insufficient because you have no access to sipCpp,

> sipSelf, etc.
> 
> 
> I think a workable solution would be a new directive %VirtualErrorCode.
> 
> A generated virtual function with %VirtualErrorCode could look like the 
> following
> 
> void sipMyClass::myVirtualMember()
> {
>     extern void sipVH_MyModule_1(sip_gilstate_t,PyObject *);
> 
>     sip_gilstate_t sipGILState;
>     PyObject *meth;
> 
>     meth = 
>
sipIsPyMethod(&sipGILState,&sipPyMethods[16],sipPySelf,NULL,sipNm_MyModule_myVirtualMember);
> 
>     if (!meth)
>     {
>         MyClass::myVirtualMember();
>         return;
>     }
>     bool isError = false;
>     sipVH_MyModule_1(sipGILState,meth,&isError);
>     if( isError ) {
>         // %VirtualErrorCode inserted here
>     }
> }
> 
> The virtual handler functions would only need a param bool * isError. If 
> isError is 0, then they would call PyErr_Print() as they do currently,
> else 
> they will set *isError = true and return without clearing the exception.
> 
> The %VirtualErrorCode would be responsible for clearing the exception.
> 
> I can probably come up with a working patch next week if you think this
is
> a 
> workable solution.
> 
> This could probably later be expanded to do python -> c++ exception
> mapping.

Where would you specify %VirtualCode? I assume you don't plan to specify it
for every virtual method, but I don't know what your use case is.

The PyQt use case would be to specify the code once (somewhere in the
QtCore module) which would then invoke a "pyqtVirtualExceptionHandler"
callable that an application could set. If no callable was set then
PyErr_Print() would be called.

What would be the API, ie. what values would be set up for the code to use?

Phil



More information about the PyQt mailing list