Mercurial > jwe > octave
changeset 20743:b6408331bfa2
eliminate more uses of error_state
* graphics.cc, input.cc, max.cc, rand.cc, __eigs__.cc, ov-class.cc,
ov-classdef.cc, ov.cc, octave.cc: Eliminate more uses of error_state.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 23 Nov 2015 23:23:57 -0500 |
parents | 5e2da9a66510 |
children | 554a39b4dd2d |
files | libinterp/corefcn/graphics.cc libinterp/corefcn/input.cc libinterp/corefcn/max.cc libinterp/corefcn/rand.cc libinterp/dldfcn/__eigs__.cc libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-classdef.cc libinterp/octave-value/ov.cc libinterp/octave.cc |
diffstat | 9 files changed, 227 insertions(+), 249 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/graphics.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/corefcn/graphics.cc Mon Nov 23 23:23:57 2015 -0500 @@ -2859,12 +2859,13 @@ { std::string pname = q->first; - bgo.set (pname, q->second); - - if (error_state) + try + { + bgo.set (pname, q->second); + } + catch (const octave_execution_exception&) { error ("error setting default property %s", pname.c_str ()); - break; } } } @@ -10182,26 +10183,29 @@ if (parent.ok ()) { - graphics_handle h - = gh_manager::make_graphics_handle (go_name, parent, - integer_figure_handle, - false, false); - - if (! error_state) - { - adopt (parent, h); - - xset (h, xargs); - xcreatefcn (h); - xinitialize (h); - - retval = h.value (); - - Vdrawnow_requested = true; - } - else - error ("__go%s__: unable to create graphics handle", - go_name.c_str ()); + graphics_handle h; + + try + { + h = gh_manager::make_graphics_handle (go_name, parent, + integer_figure_handle, + false, false); + } + catch (const octave_execution_exception&) + { + error ("__go%s__: unable to create graphics handle", + go_name.c_str ()); + } + + adopt (parent, h); + + xset (h, xargs); + xcreatefcn (h); + xinitialize (h); + + retval = h.value (); + + Vdrawnow_requested = true; } else error ("__go_%s__: invalid parent", go_name.c_str ()); @@ -10278,7 +10282,7 @@ else if (val > 0 && D_NINT (val) == val) h = gh_manager::make_figure_handle (val, false); - if (! error_state && h.ok ()) + if (h.ok ()) { adopt (0, h); @@ -10714,7 +10718,7 @@ pl = loaded_toolkits.find (dtk); - if (error_state || pl == loaded_toolkits.end ()) + if (pl == loaded_toolkits.end ()) error ("failed to load %s graphics toolkit", dtk.c_str ()); else retval = pl->second; @@ -11455,10 +11459,9 @@ if (args.length () > 1) { - pname = args(1).string_value (); - - if (! error_state - && ! pname.empty () && ! pname.compare ("timeout")) + pname = args(1).xstring_value ("waitfor: PROP must be a string"); + + if (! pname.empty () && ! pname.compare ("timeout")) { if (pname.compare ("\\timeout")) pname = "timeout"; @@ -11552,23 +11555,18 @@ } } } - else if (error_state || pname.empty ()) + else if (pname.empty ()) error ("waitfor: PROP must be a non-empty string"); } if (timeout_index < 0 && args.length () > (max_arg_index + 1)) { - caseless_str s = args(max_arg_index + 1).string_value (); - - if (! error_state) - { - if (s.compare ("timeout")) - timeout_index = max_arg_index + 1; - else - error ("waitfor: invalid parameter '%s'", s.c_str ()); - } + caseless_str s = args(max_arg_index + 1).xstring_value ("waitfor: invalid parameter, expected 'timeout'"); + + if (s.compare ("timeout")) + timeout_index = max_arg_index + 1; else - error ("waitfor: invalid parameter, expected 'timeout'"); + error ("waitfor: invalid parameter '%s'", s.c_str ()); } if (timeout_index >= 0)
--- a/libinterp/corefcn/input.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/corefcn/input.cc Mon Nov 23 23:23:57 2015 -0500 @@ -199,16 +199,33 @@ if (Vdrawnow_requested && interactive) { - feval ("drawnow"); + bool eval_error = false; + + try + { + feval ("drawnow"); + } + catch (const octave_execution_exception& e) + { + eval_error = true; + + std::string stack_trace = e.info (); + + if (! stack_trace.empty ()) + std::cerr << stack_trace; + + if (interactive) + recover_from_exception (); + } flush_octave_stdout (); - // We set Vdrawnow_requested to false even if there is an error - // in drawnow so that the error doesn't reappear at every prompt. + // We set Vdrawnow_requested to false even if there is an error in + // drawnow so that the error doesn't reappear at every prompt. Vdrawnow_requested = false; - if (error_state) + if (eval_error) return "\n"; }
--- a/libinterp/corefcn/max.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/corefcn/max.cc Mon Nov 23 23:23:57 2015 -0500 @@ -240,7 +240,8 @@ if (nargin == 3) { dim = args(2).int_value (true) - 1; - if (error_state || dim < 0) + + if (dim < 0) { error ("%s: DIM must be a valid dimension", func); return retval; @@ -901,7 +902,8 @@ if (nargin == 2) { dim = args(1).int_value (true) - 1; - if (error_state || dim < 0) + + if (dim < 0) { error ("%s: DIM must be a valid dimension", func); return retval;
--- a/libinterp/corefcn/rand.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/corefcn/rand.cc Mon Nov 23 23:23:57 2015 -0500 @@ -208,26 +208,30 @@ } else if (tmp.is_matrix_type ()) { - Array<int> iv = tmp.int_vector_value (true); + Array<int> iv; - if (! error_state) + try { - octave_idx_type len = iv.numel (); - - dims.resize (len); + iv = tmp.int_vector_value (true); + } + catch (const octave_execution_exception&) + { + error ("%s: dimensions must be a scalar or array of integers", fcn); + } - for (octave_idx_type i = 0; i < len; i++) - { - // Negative dimensions are treated as zero for Matlab - // compatibility - octave_idx_type elt = iv(i); - dims(i) = elt >=0 ? elt : 0; - } + octave_idx_type len = iv.numel (); + + dims.resize (len); - goto gen_matrix; + for (octave_idx_type i = 0; i < len; i++) + { + // Negative dimensions are treated as zero for Matlab + // compatibility + octave_idx_type elt = iv(i); + dims(i) = elt >=0 ? elt : 0; } - else - error ("%s: dimensions must be a scalar or array of integers", fcn); + + goto gen_matrix; } else {
--- a/libinterp/dldfcn/__eigs__.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/dldfcn/__eigs__.cc Mon Nov 23 23:23:57 2015 -0500 @@ -56,13 +56,15 @@ if (eigs_fcn) { - octave_value_list tmp = eigs_fcn->do_multi_index_op (1, args); + octave_value_list tmp; - if (error_state) + try { - eigs_error = 1; + tmp = eigs_fcn->do_multi_index_op (1, args); + } + catch (const octave_execution_exception&) + { gripe_user_supplied_eval ("eigs"); - return retval; } if (tmp.length () && tmp(0).is_defined ()) @@ -73,13 +75,7 @@ warned_imaginary = true; } - retval = ColumnVector (tmp(0).vector_value ()); - - if (error_state) - { - eigs_error = 1; - gripe_user_supplied_eval ("eigs"); - } + retval = tmp(0).xvector_value ("eigs: evaluation of user-supplied function failed"); } else { @@ -100,24 +96,20 @@ if (eigs_fcn) { - octave_value_list tmp = eigs_fcn->do_multi_index_op (1, args); + octave_value_list tmp; - if (error_state) + try { - eigs_error = 1; + tmp = eigs_fcn->do_multi_index_op (1, args); + } + catch (const octave_execution_exception&) + { gripe_user_supplied_eval ("eigs"); - return retval; } if (tmp.length () && tmp(0).is_defined ()) { - retval = ComplexColumnVector (tmp(0).complex_vector_value ()); - - if (error_state) - { - eigs_error = 1; - gripe_user_supplied_eval ("eigs"); - } + retval = tmp(0).complex_vector_value ("eigs: evaluation of user-supplied function failed"); } else {
--- a/libinterp/octave-value/ov-class.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/octave-value/ov-class.cc Mon Nov 23 23:23:57 2015 -0500 @@ -613,13 +613,8 @@ { obvp->subsasgn (type, idx, rhs); - if (! error_state) - { - count++; - retval = octave_value (this); - } - else - gripe_failed_assignment (); + count++; + retval = octave_value (this); } else error ("malformed class"); @@ -734,121 +729,91 @@ } } - if (! error_state) + switch (type[0]) { - switch (type[0]) - { - case '(': + case '(': + { + if (n > 1 && type[1] == '.') { - if (n > 1 && type[1] == '.') + std::list<octave_value_list>::const_iterator p = idx.begin (); + octave_value_list key_idx = *++p; + + assert (key_idx.length () == 1); + + std::string key = key_idx(0).xstring_value ("assignment to class element failed"); + + map.assign (idx.front (), key, t_rhs); + + count++; + retval = octave_value (this); + } + else + { + if (t_rhs.is_object () || t_rhs.is_map ()) { - std::list<octave_value_list>::const_iterator p = idx.begin (); - octave_value_list key_idx = *++p; + octave_map rhs_map = t_rhs.xmap_value ("invalid class assignment"); - assert (key_idx.length () == 1); + map.assign (idx.front (), rhs_map); - std::string key = key_idx(0).xstring_value ("assignment to class element failed"); + count++; + retval = octave_value (this); + } + else + { + if (t_rhs.is_empty ()) + { + map.delete_elements (idx.front ()); - map.assign (idx.front (), key, t_rhs); - - if (! error_state) - { count++; retval = octave_value (this); } else - gripe_failed_assignment (); - } - else - { - if (t_rhs.is_object () || t_rhs.is_map ()) - { - octave_map rhs_map = t_rhs.map_value (); - - if (! error_state) - { - map.assign (idx.front (), rhs_map); - - if (! error_state) - { - count++; - retval = octave_value (this); - } - else - gripe_failed_assignment (); - } - else - error ("invalid class assignment"); - } - else - { - if (t_rhs.is_empty ()) - { - map.delete_elements (idx.front ()); - - if (! error_state) - { - count++; - retval = octave_value (this); - } - else - gripe_failed_assignment (); - } - else - error ("invalid class assignment"); - } + error ("invalid class assignment"); } } - break; + } + break; - case '.': - { - octave_value_list key_idx = idx.front (); + case '.': + { + octave_value_list key_idx = idx.front (); - assert (key_idx.length () == 1); + assert (key_idx.length () == 1); - std::string key = key_idx(0).string_value (); + std::string key = key_idx(0).string_value (); - if (t_rhs.is_cs_list ()) - { - Cell tmp_cell = Cell (t_rhs.list_value ()); + if (t_rhs.is_cs_list ()) + { + Cell tmp_cell = Cell (t_rhs.list_value ()); - // The shape of the RHS is irrelevant, we just want - // the number of elements to agree and to preserve the - // shape of the left hand side of the assignment. + // The shape of the RHS is irrelevant, we just want + // the number of elements to agree and to preserve the + // shape of the left hand side of the assignment. - if (numel () == tmp_cell.numel ()) - tmp_cell = tmp_cell.reshape (dims ()); + if (numel () == tmp_cell.numel ()) + tmp_cell = tmp_cell.reshape (dims ()); - map.setfield (key, tmp_cell); - } - else - { - Cell tmp_cell(1, 1); - tmp_cell(0) = t_rhs.storable_value (); - map.setfield (key, tmp_cell); - } + map.setfield (key, tmp_cell); + } + else + { + Cell tmp_cell(1, 1); + tmp_cell(0) = t_rhs.storable_value (); + map.setfield (key, tmp_cell); + } - if (! error_state) - { - count++; - retval = octave_value (this); - } - else - gripe_failed_assignment (); - } - break; + count++; + retval = octave_value (this); + } + break; - case '{': - gripe_invalid_index_type (type_name (), type[0]); - break; + case '{': + gripe_invalid_index_type (type_name (), type[0]); + break; - default: - panic_impossible (); - } + default: + panic_impossible (); } - else - gripe_failed_assignment (); return retval; } @@ -1829,34 +1794,29 @@ { if (fcn->is_class_constructor (id) || fcn->is_class_method (id)) { - octave_map m = args(0).map_value (); + octave_map m = args(0).xmap_value ("class: S must be a valid structure"); - if (! error_state) + if (nargin == 2) + retval + = octave_value (new octave_class + (m, id, std::list<std::string> ())); + else { - if (nargin == 2) - retval - = octave_value (new octave_class - (m, id, std::list<std::string> ())); - else - { - octave_value_list parents = args.slice (2, nargin-2); + octave_value_list parents = args.slice (2, nargin-2); - retval - = octave_value (new octave_class (m, id, parents)); - } + retval + = octave_value (new octave_class (m, id, parents)); + } - octave_class::exemplar_const_iterator it - = octave_class::exemplar_map.find (id); + octave_class::exemplar_const_iterator it + = octave_class::exemplar_map.find (id); - if (it == octave_class::exemplar_map.end ()) - octave_class::exemplar_map[id] - = octave_class::exemplar_info (retval); - else if (! it->second.compare (retval)) - error ("class: object of class '%s' does not match previously constructed objects", - id.c_str ()); - } - else - error ("class: S must be a valid structure"); + if (it == octave_class::exemplar_map.end ()) + octave_class::exemplar_map[id] + = octave_class::exemplar_info (retval); + else if (! it->second.compare (retval)) + error ("class: object of class '%s' does not match previously constructed objects", + id.c_str ()); } else error ("class: '%s' is invalid as a class name in this context",
--- a/libinterp/octave-value/ov-classdef.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/octave-value/ov-classdef.cc Mon Nov 23 23:23:57 2015 -0500 @@ -541,29 +541,24 @@ { cdef_class cls (to_cdef (args(0))); - if (! error_state) + std::string meth_name = args(1).xstring_value ("fevalStatic: method name must be a string"); + + cdef_method meth = cls.find_method (meth_name); + + if (meth.ok ()) { - std::string meth_name = args(1).xstring_value ("fevalStatic: method name must be a string"); - - cdef_method meth = cls.find_method (meth_name); - - if (meth.ok ()) - { - if (meth.is_static ()) - retval = meth.execute (args.splice (0, 2), nargout, - true, "fevalStatic"); - else - error ("fevalStatic: method `%s' is not static", - meth_name.c_str ()); - } + if (meth.is_static ()) + retval = meth.execute (args.splice (0, 2), nargout, + true, "fevalStatic"); else - error ("fevalStatic: method not found: %s", meth_name.c_str ()); + error ("fevalStatic: method `%s' is not static", + meth_name.c_str ()); } else - error ("fevalStatic: first argument must be a meta.class object"); + error ("fevalStatic: method not found: %s", meth_name.c_str ()); } else - error ("fevalStatic: invalid arguments"); + error ("fevalStatic: first argument must be a meta.class object"); return retval; } @@ -578,29 +573,24 @@ { cdef_class cls = to_cdef (args(0)); - if (! error_state) + std::string prop_name = args(1).xstring_value ("getConstant: property name must be a string"); + + cdef_property prop = cls.find_property (prop_name); + + if (prop.ok ()) { - std::string prop_name = args(1).xstring_value ("getConstant: property name must be a string"); - - cdef_property prop = cls.find_property (prop_name); - - if (prop.ok ()) - { - if (prop.is_constant ()) - retval(0) = prop.get_value (true, "getConstant"); - else - error ("getConstant: property `%s' is not constant", - prop_name.c_str ()); - } + if (prop.is_constant ()) + retval(0) = prop.get_value (true, "getConstant"); else - error ("getConstant: property not found: %s", + error ("getConstant: property `%s' is not constant", prop_name.c_str ()); } else - error ("getConstant: first argument must be a meta.class object"); + error ("getConstant: property not found: %s", + prop_name.c_str ()); } else - error ("getConstant: invalid arguments"); + error ("getConstant: first argument must be a meta.class object"); return retval; } @@ -621,10 +611,7 @@ \ cdef_class clsb = to_cdef (args(1)); \ \ - if (! error_state) \ - retval(0) = FUN (CLSA, CLSB); \ - else \ - error (#OP ": arguments must be meta.class objects"); \ + retval(0) = FUN (CLSA, CLSB); \ } \ else \ error (#OP ": invalid arguments"); \
--- a/libinterp/octave-value/ov.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/octave-value/ov.cc Mon Nov 23 23:23:57 2015 -0500 @@ -1668,7 +1668,7 @@ } Array<int> -octave_value::int_vector_value (bool force_string_conv, bool require_int, +octave_value::int_vector_value (bool require_int, bool force_string_conv, bool force_vector_conversion) const { Array<int> retval;
--- a/libinterp/octave.cc Mon Nov 23 21:00:14 2015 -0500 +++ b/libinterp/octave.cc Mon Nov 23 23:23:57 2015 -0500 @@ -857,13 +857,22 @@ // Execute any code specified with --eval 'CODE' if (! code_to_eval.empty ()) { - int parse_status = execute_eval_option_code (code_to_eval); + int parse_status = 0; + + try + { + parse_status = execute_eval_option_code (code_to_eval); + } + catch (const octave_execution_exception&) + { + parse_status = 1; + } if (! persist) { quitting_gracefully = true; - clean_up_and_exit (parse_status || error_state ? 1 : 0); + clean_up_and_exit (parse_status); } } @@ -878,15 +887,24 @@ // If we are running an executable script (#! /bin/octave) then // we should only see the args passed to the script. - intern_argv (remaining_args, octave_cmdline_argv+last_arg_idx); + int exit_status = 0; + + try + { + intern_argv (remaining_args, octave_cmdline_argv+last_arg_idx); - execute_command_line_file (octave_cmdline_argv[last_arg_idx]); + execute_command_line_file (octave_cmdline_argv[last_arg_idx]); + } + catch (const octave_execution_exception&) + { + exit_status = 1; + } if (! persist) { quitting_gracefully = true; - clean_up_and_exit (error_state ? 1 : 0); + clean_up_and_exit (exit_status); } }