[PyKDE] Handling exceptions in SIP
Phil Thompson
phil at riverbankcomputing.co.uk
Tue Jun 7 11:06:29 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
>
> };
So if I introduce something like...
%ExceptionType MyException
%TypeHeaderCode
#include <whatever>
%End
%ConvertFromTypeCode
// C++ code to return a PyObject instance of the exception.
// sipCpp (the C++ poiter) and sipException_MyException (the
// PyObject exception type) will be available, eg...
return PyObject_CallFunction(sipException_MyException, "S",
sipCpp->what());
%End
%End
...that has the same effect as your code above.
> 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;
> }
...and generate the above code automatically, then that gives you what you
want? If MyException isn't defined then it reverts to the current
behaviour.
Phil
More information about the PyQt
mailing list