diff libinterp/corefcn/error.cc @ 21856:c9c5340d4758

lasterror: Allow setting error state with an empty stack * error.cc (Flasterror): Reset Vlast_error_stack to empty if the given error structure has an empty stack. Add BIST tests.
author Mike Miller <mtmiller@octave.org>
date Fri, 10 Jun 2016 17:06:16 -0700
parents b571fc85953f
children 0806871e3e1e
line wrap: on
line diff
--- a/libinterp/corefcn/error.cc	Fri Jun 10 17:23:27 2016 -0700
+++ b/libinterp/corefcn/error.cc	Fri Jun 10 17:06:16 2016 -0700
@@ -1875,6 +1875,7 @@
           std::string new_error_name;
           int new_error_line = -1;
           int new_error_column = -1;
+          bool initialize_stack = false;
 
           if (new_err.contains ("message"))
             {
@@ -1892,42 +1893,49 @@
 
           if (new_err.contains ("stack"))
             {
-              new_err_stack =
-                new_err.getfield ("stack").scalar_map_value ();
-
-              if (new_err_stack.contains ("file"))
-                {
-                  const std::string tmp =
-                    new_err_stack.getfield ("file").string_value ();
-                  new_error_file = tmp;
-                }
-
-              if (new_err_stack.contains ("name"))
+              if (new_err.getfield ("stack").numel () == 0)
+                initialize_stack = true;
+              else
                 {
-                  const std::string tmp =
-                    new_err_stack.getfield ("name").string_value ();
-                  new_error_name = tmp;
-                }
+                  new_err_stack =
+                    new_err.getfield ("stack").scalar_map_value ();
+
+                  if (new_err_stack.contains ("file"))
+                    {
+                      const std::string tmp =
+                        new_err_stack.getfield ("file").string_value ();
+                      new_error_file = tmp;
+                    }
 
-              if (new_err_stack.contains ("line"))
-                {
-                  const int tmp =
-                    new_err_stack.getfield ("line").nint_value ();
-                  new_error_line = tmp;
-                }
+                  if (new_err_stack.contains ("name"))
+                    {
+                      const std::string tmp =
+                        new_err_stack.getfield ("name").string_value ();
+                      new_error_name = tmp;
+                    }
 
-              if (new_err_stack.contains ("column"))
-                {
-                  const int tmp =
-                    new_err_stack.getfield ("column").nint_value ();
-                  new_error_column = tmp;
+                  if (new_err_stack.contains ("line"))
+                    {
+                      const int tmp =
+                        new_err_stack.getfield ("line").nint_value ();
+                      new_error_line = tmp;
+                    }
+
+                  if (new_err_stack.contains ("column"))
+                    {
+                      const int tmp =
+                        new_err_stack.getfield ("column").nint_value ();
+                      new_error_column = tmp;
+                    }
                 }
             }
 
           Vlast_error_message = new_error_message;
           Vlast_error_id = new_error_id;
 
-          if (new_err.contains ("stack"))
+          if (initialize_stack)
+            Vlast_error_stack = initialize_last_error_stack ();
+          else if (new_err.contains ("stack"))
             {
               new_err_stack.setfield ("file", new_error_file);
               new_err_stack.setfield ("name", new_error_name);
@@ -1951,6 +1959,19 @@
   return ovl (err);
 }
 
+/*
+## Test lasterror with empty error state
+%!test
+%! lasterror ("reset");
+%! x = lasterror ();
+%! assert (x.identifier, "")
+%! assert (x.message, "")
+%! assert (isempty (x.stack))
+%! lasterror (x);
+%! y = lasterror ();
+%! assert (y, x);
+*/
+
 DEFUN (lasterr, args, nargout,
        "-*- texinfo -*-\n\
 @deftypefn  {} {[@var{msg}, @var{msgid}] =} lasterr ()\n\