[C++-sig] How to turn C++ error codes into Python exceptions?
baas at ira.uka.de
Mon Mar 27 09:57:25 CEST 2006
I'm currently wrapping a library that uses dedicated status objects for
passing around error information (basically, those status objects just
contain an error code). A function either returns such a status object
or it expects an optional pointer to such an object which it then uses
for indicating whether the function was successful or not.
In the Python bindings I want the functions to raise an exception if
something went wrong. Now the question is, what's the best way to
accomplish this using Boost.Python?
I suppose the most straightforward (but tedious) way would be to write a
wrapper for each function. For example, if I have a function
bool isSelected(MObject& object, MStatus* ReturnStatus=NULL)
I could write a wrapper:
bool isSelected_wrapped(MObject& object, MStatus* ReturnStatus=NULL)
// If the user provided a MStatus object then use this one...
finalstat = ReturnStatus;
// Call the original function...
bool res = isSelected(object, finalstat);
// Check for errors...
if (exceptions_enabled && finalstat->error())
But as there are a lot of functions that have to be wrapped that way I'm
wondering if there is some more automated solution. Would it be possible
to write custom call policy objects that do the above tests
automatically whenever a function has an argument of type MStatus*?
In the other case that a function returns a status object, would it be
possible to apply a custom to_python converter that actually does not
convert the result but perform an error check and throw an exception if
an error has occurred?
- Matthias -
More information about the Cplusplus-sig