diff libinterp/corefcn/variables.cc @ 29356:65c57984a65e

compatibility with undocumented Matlab behavior for exist function (bug #59950) * variables.cc (Fexist): If one argument, return 0 for any empty builtin object. If two arguments, return 0 if second argument is any empty builtin object. * test/bug-59950.tst: New test file. * test/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Tue, 09 Feb 2021 12:18:31 -0500
parents c27ce309c079
children 7854d5752dd2
line wrap: on
line diff
--- a/libinterp/corefcn/variables.cc	Sun Feb 07 11:16:54 2021 +0100
+++ b/libinterp/corefcn/variables.cc	Tue Feb 09 12:18:31 2021 -0500
@@ -469,11 +469,26 @@
   if (nargin < 1 || nargin > 2)
     print_usage ();
 
+  // For compatibility with undocumented Matlab behavior, return 0 if
+  // there is an empty built-in object as the only argument.
+
+  if (nargin == 1 && args(0).builtin_type () != btyp_unknown
+      && args(0).isempty ())
+    return ovl (0);
+
+  // Also for compatibility, return 0 if the second argument is an empty
+  // built-in object.
+
+  if (nargin == 2 && args(1).builtin_type () != btyp_unknown
+      && args(1).isempty ())
+    return ovl (0);
+
   std::string name = args(0).xstring_value ("exist: NAME must be a string");
 
   if (nargin == 2)
     {
-      std::string type = args(1).xstring_value ("exist: TYPE must be a string");
+      std::string type
+        = args(1).xstring_value ("exist: TYPE must be a string");
 
       if (type == "class")
         warning (R"(exist: "class" type argument is not implemented)");