[PyQt] Handling python exceptions in virtual overrides

Matt Newell newellm at blur.com
Wed Sep 3 22:31:38 BST 2008


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.

Thanks
Matt




More information about the PyQt mailing list