# HG changeset patch # User Rik # Date 1403725915 25200 # Node ID de8c67ba7ac41383d6bccfc16c696e98d9c81ee3 # Parent 47d4b680d0e07f39e32c1c676cbaa5f785208f2f Use Matlab return hierarchy for exist() codes when no type is specified. * variables.cc (symbol_exist): Simplify code by removing obsolete code testing for structures in the input name. Re-order testing blocks so that built-in code (5) is returned before generic file code (2). * variables.cc (Fexist): Add more comprehensive %!tests for exist. diff -r 47d4b680d0e0 -r de8c67ba7ac4 libinterp/corefcn/variables.cc --- a/libinterp/corefcn/variables.cc Fri Jun 20 18:55:38 2014 -0400 +++ b/libinterp/corefcn/variables.cc Wed Jun 25 12:51:55 2014 -0700 @@ -388,17 +388,7 @@ int symbol_exist (const std::string& name, const std::string& type) { - std::string struct_elts; - std::string symbol_name = name; - - size_t pos = name.find ('.'); - - if (pos != std::string::npos && pos > 0) - { - struct_elts = name.substr (pos+1); - symbol_name = name.substr (0, pos); - } - else if (is_keyword (symbol_name)) + if (is_keyword (name)) return 0; bool search_any = type == "any"; @@ -409,21 +399,30 @@ if (search_any || search_var) { - bool not_a_struct = struct_elts.empty (); - bool var_ok = not_a_struct; // || val.is_map_element (struct_elts) - octave_value val = symbol_table::varval (name); - if (var_ok && (val.is_constant () || val.is_object () - || val.is_function_handle () - || val.is_anonymous_function () - || val.is_inline_function ())) + if (val.is_constant () || val.is_object () + || val.is_function_handle () + || val.is_anonymous_function () + || val.is_inline_function ()) return 1; if (search_var) return 0; } + // We shouldn't need to look in the global symbol table, since any name + // that is visible in the current scope will be in the local symbol table. + + octave_value val = safe_symbol_lookup (name); + + if (val.is_defined ()) + { + if ((search_any || search_builtin) + && val.is_builtin_function ()) + return 5; + } + if (search_any || search_file || search_dir) { std::string file_name = lookup_autoload (name); @@ -464,24 +463,18 @@ return 0; } - // We shouldn't need to look in the global symbol table, since any - // name that is visible in the current scope will be in the local - // symbol table. - - octave_value val = safe_symbol_lookup (symbol_name); - - if (val.is_defined () && struct_elts.empty ()) + if (val.is_defined ()) { - if ((search_any || search_builtin) - && val.is_builtin_function ()) - return 5; - if ((search_any || search_file) && (val.is_user_function () || val.is_dld_function ())) { octave_function *f = val.function_value (true); std::string s = f ? f->fcn_file_name () : std::string (); + // FIXME: I believe that by this point in the code the only + // return value is 103. User functions should have + // been located above. Maybe replace entire if block + // code with "return 103;" return s.empty () ? 103 : (val.is_user_function () ? 2 : 3); } } @@ -583,18 +576,41 @@ } /* +%!shared dirtmp, __var1 +%! dirtmp = P_tmpdir (); +%! __var1 = 1; + +%!assert (exist ("__%Highly_unlikely_name%__"), 0) +%!assert (exist ("__var1"), 1) +%!assert (exist ("__var1", "var"), 1) +%!assert (exist ("__var1", "builtin"), 0) +%!assert (exist ("__var1", "dir"), 0) +%!assert (exist ("__var1", "file"), 0) + %!test %! if (isunix ()) -%! assert (exist ("/tmp") == 7); -%! assert (exist ("/tmp", "file") == 7); -%! assert (exist ("/tmp", "dir") == 7); -%! assert (exist ("/bin/sh") == 2); -%! assert (exist ("/bin/sh", "file") == 2); -%! assert (exist ("/bin/sh", "dir") == 0); -%! assert (exist ("/dev/null") == 2); -%! assert (exist ("/dev/null", "file") == 2); -%! assert (exist ("/dev/null", "dir") == 0); +%! assert (exist ("/bin/sh"), 2); +%! assert (exist ("/bin/sh", "file"), 2); +%! assert (exist ("/bin/sh", "dir"), 0); +%! assert (exist ("/dev/null"), 2); +%! assert (exist ("/dev/null", "file"), 2); +%! assert (exist ("/dev/null", "dir"), 0); %! endif + +%!assert (exist ("colon"), 2) +%!assert (exist ("colon.m"), 2) + +%!testif HAVE_CHOLMOD +%! assert (exist ("chol"), 3); +%! assert (exist ("chol", "file"), 3); +%! assert (exist ("chol", "builtin"), 0); + +%!assert (exist ("sin"), 5) + +%!assert (exist (dirtmp), 7) +%!assert (exist (dirtmp, "dir"), 7) +%!assert (exist (dirtmp, "file"), 7) + */ octave_value