Raising Python exceptions from C code

Kevin Smith <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
I’m trying to figure out how I can raise a Python exception from a function in my .i file.  It’s fairly easy to call PyErr_SetString in the function, but the problem is that Python expects that when an exception occurs no value should be returned by the called function.  My function returns a pointer, but it’s NULL if an error occurs.  This NULL gets wrapped in a SWIG_NewPointerObj(SWIG_as_voidptr(result), … ) call and returned to Python which results in Python complaining about the returned value.  What I’d like to have happen in the wrapper is:

   resultobj =  (!result && PyErr_Occurred()) ? NULL : SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_void, 0 |  0 );

This way, if the result is NULL and a Python exception happened during the function, a NULL would be returned to make Python happy.

Maybe there’s a better way to do what I’m attempting, but this was what I came up with.  I’m just not sure how to get this syntax into my wrappers without post-processing them.

Kevin Smith
[email protected]






_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.