Mercurial > pytave
changeset 20:84be5ee8cc44 lp-trunk
Improved pytave.OctaveError messages, now includes filename, function and line of offending m-file.
author | David Grundberg <individ@acc.umu.se> |
---|---|
date | Mon, 17 Nov 2008 12:19:57 +0100 |
parents | 1a2c103d1789 (current diff) 583f325a520f (diff) |
children | ad4ba83d0a43 5073ff11e2b6 |
files | |
diffstat | 2 files changed, 40 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Mon Nov 10 22:03:19 2008 +0100 +++ b/ChangeLog Mon Nov 17 12:19:57 2008 +0100 @@ -1,3 +1,8 @@ +2008-11-17 David Grundberg <individ@acc.umu.se> + + * pytave.cc: func_eval: Improved pytave.OctaveError messages, now + includes filename, function and line of offending m-file. + 2008-11-10 David Grundberg <individ@acc.umu.se> * octave_to_python.cc: Remove static storage class on explicitely
--- a/pytave.cc Mon Nov 10 22:03:19 2008 +0100 +++ b/pytave.cc Mon Nov 17 12:19:57 2008 +0100 @@ -24,6 +24,7 @@ #undef HAVE_STAT /* Both boost.python and octave define HAVE_STAT... */ #include <octave/oct.h> +#include <octave/oct-map.h> #include <octave/octave.h> #include <octave/ov.h> #include <octave/parse.h> @@ -95,12 +96,40 @@ // 1 general error int parse_status = 0; reset_error_handler(); - octave_value_list tmp = eval_string("lasterror.message", - true, parse_status, 1); - if (!tmp.empty() && tmp(0).is_string()) - throw octave_error_exception(tmp(0).string_value()); - else - throw octave_error_exception(""); + octave_value_list lasterror = eval_string("lasterror", + true, parse_status, 1); + if (!lasterror.empty() && lasterror(0).is_map()) { + ostringstream exceptionmsg; + Octave_map map = lasterror(0).map_value(); + string message = map.stringfield("message", ""); + string identifier = map.stringfield("identifier", ""); + Cell stackCell = map.contents("stack"); + + // Trim trailing new lines + message = message.substr(0, message.find_last_not_of("\r\n") + 1); + + if (!stackCell.is_empty() && stackCell(0).is_map()) { + // The struct element is called "stack" but only contain + // info about the top frame. + Octave_map stack = stackCell(0).map_value(); + string file = stack.stringfield("file", "d"); + string name = stack.stringfield("name", "a"); + int line = stack.intfield("line", 1); + int column = stack.intfield("column", 2); + + exceptionmsg << file << ":" << line << ":" << column << ": "; + if (!name.empty()) + exceptionmsg << "in '" << name << "': "; + } + + if (!identifier.empty()) { + exceptionmsg << "(identifier: " << identifier << ") "; + } + exceptionmsg << message; + + throw octave_error_exception(exceptionmsg.str()); + } else + throw octave_error_exception("No Octave error available"); } if (nargout > 0) {