[C++-sig] Handling exception thrown by Boost.Python c-extension in Python code

English, Mark Mark.English at rbccm.com
Thu Apr 5 15:06:01 CEST 2007


X-Replace-Address: mark.ignorethisbit.english at xxxrbccmxxx.ignorethisbittoo_and_removethosepreceding_xxxs_.com
This is a cross-posting of a reply-less message I posted in comp.lang.python a week ago, so apologies if I'm breaking rules. I suspect that was the wrong place.

I'm just getting started on Boost Python and may have missed this 
obvious looking problem somewhere. 

Given a c-extension "testext" written using Boost Python containing a 
base class "Base", a derived class "Derived", and a function 
"doSomething" which expects a "Derived" parameter, if I pass it a 
"Base" parameter an exception is thrown. This is a 
Boost.Python.ArgumentError. My question is how do I catch this error ? 


I tried the following bit of investigation: 
#Start code 
import testext 
b = testext.Base() 
try: 
   testext.doSomething(b) 
except Exception, e: 
    pass 
help(e.__class__) 
#End code 


which produces 
#Start output 
Help on class ArgumentError: 

class ArgumentError(exceptions.TypeError) 
 |  Method resolution order: 
 |      ArgumentError 
 |      exceptions.TypeError 
 |      exceptions.StandardError 
 |      exceptions.Exception 
 | 
 |  Methods inherited from exceptions.Exception: 
 | 
 |  __getitem__(...) 
 | 
 |  __init__(...) 
 | 
 |  __str__(...) 
#End output 

"print e" produces "<Boost.Python.ArgumentError instance>" 

So I could handle this by writing an except clause for TypeError. 

Boost.Python doesn't exist as a module i.e. it's not in sys.modules, 
and I don't know how to import it - should there be a Boost.Python 
module somewhere on my PythonPath that I've forgotten to setup ? 
Is there a standard way of catching these errors by their actual 
type ? 
Is there an easy way to export the exception classes from my c- 
extension (testext) so that I can use that ? Thus "except 
testext.ArgumentError" would catch the "Boost.Python.ArgumentError".


Thanks for any help, 
Mark 
mark.ignorethisbit.english at xxxrbccmxxx.ignorethisbittoo_and_removethosepreceding_xxxs_.com
________________________________________

This E-Mail (including any attachments) may contain privileged or confidential information.  It is intended only for the addressee(s) indicated above.

The sender does not waive any of its rights, privileges or other protections respecting this information.  

Any distribution, copying or other use of this E-Mail or the information it contains, by other than an intended recipient, is not sanctioned and is prohibited.

If you received this E-Mail in error, please delete it and advise the sender (by return E-Mail or otherwise) immediately. 

This E-Mail (including any attachments) has been scanned for viruses. 

It is believed to be free of any virus or other defect that might affect any computer system into which it is received and opened. 

However, it is the responsibility of the recipient to ensure that it is virus free. 

The sender accepts no responsibility for any loss or damage arising in any way from its use.

E-Mail received by or sent from RBC Capital Markets is subject to review by Supervisory personnel. 

Such communications are retained and may be produced to regulatory authorities or others with legal rights to the information.

IRS CIRCULAR 230 NOTICE:  TO COMPLY WITH U.S. TREASURY REGULATIONS, WE ADVISE YOU THAT ANY U.S. FEDERAL TAX ADVISE INCLUDED IN THIS COMMUNICATION IS NOT INTENDED OR WRITTEN TO BE USED, AND CANNOT BE USED, TO AVOID ANY U.S. FEDERAL TAX PENALTIES OR TO PROMOTE, MARKET, OR RECOMMEND TO ANOTHER PARTY ANY TRANSACTION OR MATTER.



More information about the Cplusplus-sig mailing list