changeset 292:510ed59a61d3

pyobject: fix fieldnames and methods when result is an empty list * @pyobject/fieldnames.m, @pyobject/methods.m: Handle conversion of pyobject attribute list when empty. Add explicit conversion to char in anticipation of future changes. Add %!tests.
author Mike Miller <mtmiller@octave.org>
date Wed, 03 Aug 2016 16:23:09 -0700
parents a0dc81daf553
children 3f74eeb0a5da 2ecae5c6eeb6
files @pyobject/fieldnames.m @pyobject/methods.m
diffstat 2 files changed, 23 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/fieldnames.m	Wed Aug 03 15:41:39 2016 -0700
+++ b/@pyobject/fieldnames.m	Wed Aug 03 16:23:09 2016 -0700
@@ -53,10 +53,14 @@
                  " and not a.startswith('_')]"]);
 
   names_obj = pycall (cmd, x);
+  ## FIXME: names = cellfun (@char, cell (names_obj), "uniformoutput", false);
   len = length (names_obj);
-  idx = struct ("type", "{}", "subs", {{1:len}});
-  [names{1:len}] = subsref (names_obj, idx);
-  names = names(:);
+  names = cell (len, 1);
+  if (len > 0)
+    idx = struct ("type", "{}", "subs", {{1:len}});
+    [names{1:len}] = subsref (names_obj, idx);
+    names = cellfun (@char, names, "uniformoutput", false);
+  endif
 
 endfunction
 
@@ -78,3 +82,7 @@
 %! assert (any (strcmp (lst, "False")))
 %! assert (any (strcmp (lst, "None")))
 %! assert (any (strcmp (lst, "True")))
+
+%!assert (fieldnames (pyeval ("object()")), cell (0, 1))
+%!assert (fieldnames (pyeval ("{}")), cell (0, 1))
+%!assert (ismember ("denominator", fieldnames (pycall ("fractions.Fraction"))))
--- a/@pyobject/methods.m	Wed Aug 03 15:41:39 2016 -0700
+++ b/@pyobject/methods.m	Wed Aug 03 16:23:09 2016 -0700
@@ -72,9 +72,15 @@
                  " if callable(getattr(x, a)) and not a.startswith('_')]"]);
 
   mtds_list_obj = pycall (cmd, x);
+
+  ## FIXME: mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false);
   len = length (mtds_list_obj);
-  idx = struct ("type", "{}", "subs", {{1:len}});
-  [mtds_list{1:len}] = subsref (mtds_list_obj, idx);
+  mtds_list = cell (len, 1);
+  if (len > 0)
+    idx = struct ("type", "{}", "subs", {{1:len}});
+    [mtds_list{1:len}] = subsref (mtds_list_obj, idx);
+    mtds_list = cellfun (@char, mtds_list, "uniformoutput", false);
+  endif
 
   if (nargout == 0)
     ## FIXME: should this be available as @pyobject/ismodule.m ?
@@ -111,3 +117,7 @@
 %! assert (any (strcmp (m, "getcwd")))
 %! assert (any (strcmp (m, "getenv")))
 %! assert (any (strcmp (m, "getpid")))
+
+%!assert (methods (pyeval ("object()")), cell (0, 1))
+%!assert (ismember ("append", methods (pyeval ("[]"))))
+%!assert (ismember ("keys", methods (pyeval ("{}"))))