Extension types as exceptions (Wrapup)
Daniel Dittmar
daniel.dittmar at sap-ag.de
Tue Aug 17 06:28:58 EDT 1999
My code creates an actual instance of the exception class, which might
be a bit more self documenting (as the attributes have names) and is
easier to handle when there are many attributes.
This has the one disadvantage that you have to create a __str__ method
for the class (that's what the exceptionCode parameter in
createExceptionKind is for), because without it, a traceback will show
only the class name.
Actually, my current code first tries to create the instance. If this
fails, it will try to create the tuple.
There is a reference counting error in my raiseError routine:
PyInt_FromLong (code) will set the ref count to 1,
PyObject_SetAttrString will set it to 2, so the object can never be
reclaimed.
The hopefully correct version is:
static PyObject*
raiseCommunicationError (
int code,
const char * msg)
{
PyObject * exception = PyInstance_New (CommunicationErrorType,
NULL, NULL);
PyObject * pycode = NULL;
PyObject * pymsg = NULL;
pycode = PyInt_FromLong (code);
pymsg = PyString_FromString (msg);
if (exception != NULL) {
PyObject_SetAttrString (exception, "errorCode", pycode) ;
PyObject_SetAttrString (exception, "message", pymsg);
Py_XDECREF (pycode);
Py_XDECREF (pymsg);
}
else {
exception = Py_BuildValue ("NN", pycode, pymsg);
}
PyErr_SetObject (CommunicationErrorType, exception);
return NULL;
}
Daniel Dittmar
daniel.dittmar at sap-ag.de
SAP AG, Basis Entwicklung Berlin
More information about the Python-list
mailing list