diff pycall.cc @ 231:6fffa6219b2c

Properly extract exception text from Python (fixes issue #24) * exceptions.{h, cc}: Add a new method to properly extract string for a python exception. Also add a new error message if we can't extract the error string. * pycall.cc, pyeval.cc, pyexec.cc: Use the new method to extract string when an exception occurs.
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Sat, 16 Jul 2016 05:50:36 -0700
parents 377f2dc057ea
children c02f0a4c92e7
line wrap: on
line diff
--- a/pycall.cc	Fri Jul 15 00:51:23 2016 -0700
+++ b/pycall.cc	Sat Jul 16 05:50:36 2016 -0700
@@ -182,19 +182,8 @@
     }
   catch (error_already_set const &)
     {
-      PyObject *ptype, *pvalue, *ptraceback;
-      PyErr_Fetch (&ptype, &pvalue, &ptraceback);
-
-      try
-        {
-          std::string message = extract<std::string> (pvalue);
-          error ("pycall: %s", message.c_str ());
-        }
-      catch (error_already_set const &)
-        {
-          PyErr_Restore (ptype, pvalue, ptraceback);
-          PyErr_Print ();
-        }
+      std::string message = pytave::fetch_exception_message ();
+      error ("pycall: %s", message.c_str ());
     }
 
   return retval;
@@ -247,4 +236,8 @@
 %! assert (pycall ("pyfunc", true), 30)
 %! assert (pycall ("pyfunc", false), 20)
 %! assert (pycall ("pyfunc", 10), 10)
+
+%!error <NameError>
+%! pyexec ("def raiseException ():\n  raise NameError ('oops')")
+%! pycall ("raiseException")
 */