# HG changeset patch # User Abhinav Tripathi # Date 1471005906 25200 # Node ID 778c91283a736cc3b7a4d292f29fff239f667007 # Parent 2b24602952182fa68d3ace95b1462ecf23407081 Add isequal method to allow comparing pyobjects (fixes issue #53) * @pyobject/pyobject.m: Add an 'isequal' method to fix a BIST test and some more tests. diff -r 2b2460295218 -r 778c91283a73 @pyobject/pyobject.m --- a/@pyobject/pyobject.m Fri Aug 12 00:57:46 2016 -0700 +++ b/@pyobject/pyobject.m Fri Aug 12 05:45:06 2016 -0700 @@ -217,6 +217,17 @@ r = size (x, index_pos); endif endfunction + + function res = isequal (varargin) + assert (nargin >= 2) + res = all (strcmp ("pyobject", cellfun ("class", varargin, "uniformoutput", false))); + for i = 2:nargin + if (! res) + return; + endif + res = res && pycall("bool", pycall ("operator.eq", varargin{1}, varargin{i})); + endfor + endfunction endmethods endclassdef @@ -329,3 +340,15 @@ %!error double (pyobject ("this is not a number")) %!error double (pyobject ()) %!error double (pyeval ("[1, 2, 3]")) + +%!error (isequal (pyobject ())) +%!assert (! isequal (pyobject (1.2), 1.2)) +%!assert (isequal (pyobject ("a string"), pyobject ("a string"))) +%!assert (isequal (pyeval ("None"), pyeval ("None"))) +%!assert (! isequal (pyeval ("None"), pyeval ("None"), pyobject (10))) +%!assert (isequal (pyobject (10), pyobject (10.0), pyobject (int8 (10)))) + +%!test +%! A = pyeval ("[1, 2, 3]"); +%! B = pycall ("list", {1, 2, 3}); +%! assert (isequal (A, B))