changeset 32055:dfd2b1a268a0

Don't list non-public or hidden functions from methods() (bug #64066) * ov-classdef.cc (Fproperties): Use property name from property_map variable , which is already known, rather than calling get_name() method which causes cascade of multiple methods to be invoked. * ov-classdef.cc (F__methods__): Change function signature to return a flag "found" if input was a classdef class or instance. Add code to check "Access" and "Hidden" properties before including in list of methods. Add BIST test for __methods__. * methods.m: Use second output from __methods__ to determine whether __methods__ call was successful.
author Rik <rik@octave.org>
date Tue, 25 Apr 2023 18:09:29 -0700
parents 5998f6639148
children d98157d51a97
files libinterp/octave-value/ov-classdef.cc scripts/miscellaneous/methods.m
diffstat 2 files changed, 27 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-classdef.cc	Tue Apr 25 09:49:20 2023 -0700
+++ b/libinterp/octave-value/ov-classdef.cc	Tue Apr 25 18:09:29 2023 -0700
@@ -701,11 +701,8 @@
     {
       // FIXME: this loop duplicates a significant portion of the loops
       // in octave_classdef::print_raw.
-
       const cdef_property& prop = pname_prop.second;
 
-      std::string nm = prop.get_name ();
-
       octave_value acc = prop.get ("GetAccess");
 
       if (! acc.is_string () || acc.string_value () != "public")
@@ -716,7 +713,7 @@
       if (hid.bool_value ())
         continue;
 
-      property_names.push_back (nm);
+      property_names.push_back (pname_prop.first);
     }
 
   if (nargout > 0)
@@ -743,8 +740,8 @@
 
 DEFMETHOD (__methods__, interp, args, ,
            doc: /* -*- texinfo -*-
-@deftypefn  {} {@var{mtds} =} __methods__ (@var{obj})
-@deftypefnx {} {@var{mtds} =} __methods__ ("classname")
+@deftypefn  {} {[@var{mtds}, @var{found}] =} __methods__ (@var{obj})
+@deftypefnx {} {[@var{mtds}, @var{found}] =} __methods__ ("classname")
 Implement @code{methods} for Octave class objects and classnames.
 @seealso{methods}
 @end deftypefn */)
@@ -762,6 +759,7 @@
     err_wrong_type_arg ("__methods__", arg);
 
   string_vector sv;
+  bool found = false;
 
   cdef_class cls = lookup_class (class_name, false, true);
 
@@ -775,12 +773,23 @@
 
       for (const auto& nm_mthd : method_map)
         {
-          std::string nm = nm_mthd.first;
+          const cdef_method& method = nm_mthd.second;
+
+          octave_value acc = method.get ("Access");
+
+          if (! acc.is_string () || acc.string_value () != "public")
+            continue;
 
-          method_names.push_back (nm);
+          octave_value hid = method.get ("Hidden");
+
+          if (hid.bool_value ())
+            continue;
+
+          method_names.push_back (nm_mthd.first);
         }
 
       sv = string_vector (method_names);
+      found = true;
     }
   else
     {
@@ -788,10 +797,17 @@
       load_path& lp = interp.get_load_path ();
 
       sv = string_vector (lp.methods (class_name));
+      found = ! sv.empty ();
     }
 
-  return ovl (Cell (sv));
+  return ovl (Cell (sv), found);
 }
 
+/*
+%!assert (__methods__ ("inputParser"),
+%!        {"addOptional"; "addParamValue"; "addParameter"; "addRequired";
+%!         "addSwitch"; "delete"; "disp"; "parse"; })
+*/
+
 OCTAVE_END_NAMESPACE(octave)
 
--- a/scripts/miscellaneous/methods.m	Tue Apr 25 09:49:20 2023 -0700
+++ b/scripts/miscellaneous/methods.m	Tue Apr 25 18:09:29 2023 -0700
@@ -65,8 +65,8 @@
   elseif (ischar (obj))
     ## Could be a classname for an Octave class or Java class.
     ## Try Octave class first.
-    mtds_list = __methods__ (obj);
-    if (isempty (mtds_list))
+    [mtds_list, valid] = __methods__ (obj);
+    if (! valid)
       mtds_str = javaMethod ("getMethods", "org.octave.ClassHelper", obj);
       mtds_list = ostrsplit (mtds_str, ';');
       mtds_list = mtds_list(:);  # return a column vector for compatibility