# HG changeset patch # User Mike Miller # Date 1470266589 25200 # Node ID 510ed59a61d3564353fc8b91b610ce166b39eb02 # Parent a0dc81daf5537859a9f25a4d810c035047257aeb 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. diff -r a0dc81daf553 -r 510ed59a61d3 @pyobject/fieldnames.m --- 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")))) diff -r a0dc81daf553 -r 510ed59a61d3 @pyobject/methods.m --- 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 ("{}"))))