[PyQt] [SIP] Raise exception if Boolean return value is False

Christian Aichinger Greek0 at gmx.net
Wed Jan 22 14:03:15 GMT 2014


Hi!
I have several C functions that take an /Out/ parameter and return a 
bool to indicate success. SIP transforms this into a (bool, out_val) 
return on the Python side. Is there any way to generate a Python 
signature that returns the /Out/ value and raises an Exception if the 
boolean error return is False?

What I have:
SIP: bool MetersPerPixel(const MapPixelCoord &pos, double *mpp /Out/);
 >>> success, mpp = MetersPerPixel(coord)

What I want:
 >>> try: mpp = MetersPerPixel(coord)
 >>> except: # Handle the error case

Currently I work around the issue by defining a new C++ function in 
%ModuleCode, which is then wrapped by SIP:

%ModuleCode
double MetersPerPixel(const MapPixelCoord &pos) {
     double dbl;
     if (!MetersPerPixel(pos, &dbl)) {
         std::runtime_error("MetersPerPixel() failed.");
     }
     return dbl;
}
%End
double MetersPerPixel(const MapPixelCoord &pos);

However, I have a lot of these functions, and would rather not 
explicitly wrap all of them.

Best wishes,
Chris



More information about the PyQt mailing list