C ++ exception propagation for cython exception - python

I have a problem with Cython 0.17.1

My function throws std::runtime_error if the file does not exist, I would like to somehow extend this exception to my Cython code.

 void loadFile(const string &filename) { // some code, if filename doesn't exists throw std::runtime_error( std::string("File doesn't exists" ) ); } 

and from Cython after properly wrapping the function:

 try: loadFile(myfilename) except RuntimeError: print "Can't load file" 

but this exception is always ignored, how can I catch C ++ exceptions from Python?

+7
source share
2 answers

Do you declare exception handling with extern? You should read about C ++ exception handling: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#exceptions

Basically, you need to do something like the following:

 cdef extern from "some_file.h": cdef int foo() except + 
+2
source
+1
source

All Articles