[PyKDE] Handling exceptions in SIP

Denis S. Otkidach ods at strana.ru
Tue Jun 7 08:31:04 BST 2005


On Mon, 6 Jun 2005 17:54:43 +0100
Phil Thompson <phil at riverbankcomputing.co.uk> wrote:

> SIP's exception support was added by a user - I've never used it myself. 
> Suggestions for improvements always welcome.

I see the following scenario.  User creates exception class and expose
it in module namespace (Please correct me if there is a better way):

%ModuleCode
static PyObject *MyException_object;
%End

%PostInitialisationCode
    MyException_object = PyErr_NewException("MyModule.MyException", 0, 0);
    if (!MyException_object) return;
    PyDict_SetItemString(sip_mdict, "MyException", MyException_object);
%End

Then he has to define one way mapping for it (assuming MyException has
the same interface as std::exception):

%MappedType MyException
{

%ConvertFromTypeCode
    return PyObject_CallFunction(MyException_object, "S", sipCpp->what());
%End

%ConvertToTypeCode
    // We don't need backward convertion, but SIP requires it.
    *sipIsErr = 1;
    return 0;
%End

};


That's all.  Now generated code for method with signature 
"void error() throw (MyException);" should be equivalent to 
something like the following:

    try
    {
    sipCpp -> Test::error();
    }
    catch (MyException &e)
    {
            PyObject *MyException_instance = ... // %ConvertFromTypeCode
            PyObject *MyException_object = PyObject_Type(MyException_instance);
            PyErr_SetObject(MyException_object, MyException_instance);
            return NULL;
    }


-- 
Denis S. Otkidach
http://www.python.ru/      [ru]




More information about the PyQt mailing list