comparison @pyobject/pyobject.m @ 346:b1eff49bd139

pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62) * @pyobject/pyobject.m: Add conversion method struct, thin wrapper around __py_struct_from_dict__ internal function. * @pyobject/struct.m: Delete previous faulty implementation.
author Mike Miller <mtmiller@octave.org>
date Tue, 16 Aug 2016 16:08:43 -0700
parents fe6b9e618c98
children 7f039ffe501b
comparison
equal deleted inserted replaced
345:baff3b90dcb1 346:b1eff49bd139
137 else 137 else
138 y = pycall ("float", x); 138 y = pycall ("float", x);
139 endif 139 endif
140 endfunction 140 endfunction
141 141
142 function y = struct (x)
143 y = __py_struct_from_dict__ (x);
144 endfunction
145
142 function vargout = help (x) 146 function vargout = help (x)
143 idx = struct ("type", ".", "subs", "__doc__"); 147 idx = struct ("type", ".", "subs", "__doc__");
144 s = subsref (x, idx); 148 s = subsref (x, idx);
145 if (nargout == 0) 149 if (nargout == 0)
146 disp (s) 150 disp (s)
339 343
340 %!error double (pyobject ("this is not a number")) 344 %!error double (pyobject ("this is not a number"))
341 %!error double (pyobject ()) 345 %!error double (pyobject ())
342 %!error double (pyeval ("[1, 2, 3]")) 346 %!error double (pyeval ("[1, 2, 3]"))
343 347
348 ## Test conversion method pyobject.struct
349 %!assert (struct (pycall ("dict")), struct ())
350 %!assert (struct (pyobject (struct ())), struct ())
351 %!test
352 %! a = struct ("a", 1, "b", 2, "three", 3);
353 %! b = pyobject (a);
354 %! c = struct (b);
355 %! assert (c, a)
356 %!test
357 %! a = struct ("a", 1, "b", 2, "three", 3);
358 %! b = pycall ("dict", pyargs ("a", 1, "b", 2, "three", 3));
359 %! c = struct (b);
360 %! assert (c, a)
361
344 ## Octave fails to resolve function overloads via function handles 362 ## Octave fails to resolve function overloads via function handles
345 %!xtest 363 %!xtest
346 %! fn = @double; 364 %! fn = @double;
347 %! x = pyobject (int64 (42)); 365 %! x = pyobject (int64 (42));
348 %! assert (fn (x), double (x)) 366 %! assert (fn (x), double (x))