Mercurial > pytave
changeset 232:c02f0a4c92e7
Merged in genuinelucifer/pytave_main (pull request #15)
Properly extract exception text from Python (fixes issue #24)
author | Mike Miller <mike@mtmxr.com> |
---|---|
date | Wed, 20 Jul 2016 13:15:32 -0700 |
parents | 262832ae684f (current diff) 6fffa6219b2c (diff) |
children | 0deddd1b0526 |
files | pycall.cc pyeval.cc pyexec.cc |
diffstat | 5 files changed, 50 insertions(+), 39 deletions(-) [+] |
line wrap: on
line diff
--- a/exceptions.cc Wed Jul 20 12:52:38 2016 -0700 +++ b/exceptions.cc Wed Jul 20 13:15:32 2016 -0700 @@ -45,4 +45,30 @@ && octave_parse_exception::init () && variable_name_exception::init ()); } + + std::string fetch_exception_message (void) + { + using namespace boost::python; + PyObject *ptype, *pvalue, *ptraceback; + PyErr_Fetch (&ptype, &pvalue, &ptraceback); + std::string message; + + try + { + object formatted_list, formatted; + handle<> htype (ptype), hval (allow_null (pvalue)); + object traceback (import ("traceback")); + object format_exception_only (traceback.attr ("format_exception_only")); + formatted_list = format_exception_only (htype, hval); + formatted = str ("\n").join (formatted_list); + message = extract<std::string> (formatted); + } + catch (error_already_set const &) + { + PyErr_Restore (ptype, pvalue, ptraceback); + PyErr_Print (); + message = std::string ("Something weird happened. See traceback above ^"); + } + return message; + } }
--- a/exceptions.h Wed Jul 20 12:52:38 2016 -0700 +++ b/exceptions.h Wed Jul 20 13:15:32 2016 -0700 @@ -159,6 +159,7 @@ }; bool init_exceptions (void); + std::string fetch_exception_message (void); } #endif
--- a/pycall.cc Wed Jul 20 12:52:38 2016 -0700 +++ b/pycall.cc Wed Jul 20 13:15:32 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; @@ -251,4 +240,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") */
--- a/pyeval.cc Wed Jul 20 12:52:38 2016 -0700 +++ b/pyeval.cc Wed Jul 20 13:15:32 2016 -0700 @@ -110,19 +110,8 @@ } catch (error_already_set const &) { - PyObject *ptype, *pvalue, *ptraceback; - PyErr_Fetch (&ptype, &pvalue, &ptraceback); - - try - { - std::string message = extract<std::string> (pvalue); - error ("pyeval: %s", message.c_str ()); - } - catch (error_already_set const &) - { - PyErr_Restore (ptype, pvalue, ptraceback); - PyErr_Print (); - } + std::string message = pytave::fetch_exception_message (); + error ("pyeval: %s", message.c_str ()); } return retval; @@ -163,4 +152,8 @@ %! assert (z{2}{2}, 22) %! assert (z{4}{2}{1}, 421) %! assert (z{4}{2}{2}, 422) + +%!error <NameError> +%! pyexec ("def raiseException ():\n raise NameError ('oops')") +%! pyeval ("raiseException ()") */
--- a/pyexec.cc Wed Jul 20 12:52:38 2016 -0700 +++ b/pyexec.cc Wed Jul 20 13:15:32 2016 -0700 @@ -81,19 +81,17 @@ } catch (error_already_set const &) { - PyObject *ptype, *pvalue, *ptraceback; - PyErr_Fetch (&ptype, &pvalue, &ptraceback); - - try - { - std::string message = extract<std::string> (pvalue); - error ("pyexec: %s", message.c_str ()); - } - catch (error_already_set const &) - { - PyErr_Restore (ptype, pvalue, ptraceback); - PyErr_Print (); - } + std::string message = pytave::fetch_exception_message (); + error ("pyexec: %s", message.c_str ()); } return retval; } + +/* +%!error <NameError> +%! pyexec ("raise NameError ('oops')") + +%!error <AttributeError> +%! pyexec ("import sys") +%! pyexec ("sys.no_such_thing") +*/