comparison @pyobject/pyobject.m @ 358:d41fc23d4b9f

pyobject.isa: New overload method to handle Python types (fixes issue #49) * __py_struct_from_dict__.cc (F__py_isinstance__): New function to check whether a pyobject is an instance of a named type. * @pyobject/pyobject.m (pyobject.isa): New method. Add %!tests.
author Mike Miller <mtmiller@octave.org>
date Tue, 23 Aug 2016 18:09:17 -0700
parents 6cd581661176
children b0677c492655
comparison
equal deleted inserted replaced
357:ebd83497ebda 358:d41fc23d4b9f
140 140
141 function y = int64 (x) 141 function y = int64 (x)
142 y = __py_int64_scalar_value__ (x); 142 y = __py_int64_scalar_value__ (x);
143 endfunction 143 endfunction
144 144
145 function y = isa (x, typestr)
146 assert (nargin == 2);
147 assert (ischar (typestr));
148 if ((numel (typestr) > 3) && (typestr(1:3) == "py."))
149 y = __py_isinstance__ (x, typestr);
150 else
151 y = builtin ("isa", x, typestr);
152 endif
153 endfunction
154
145 function y = struct (x) 155 function y = struct (x)
146 y = __py_struct_from_dict__ (x); 156 y = __py_struct_from_dict__ (x);
147 endfunction 157 endfunction
148 158
149 function vargout = help (x) 159 function vargout = help (x)
346 356
347 %!error double (pyobject ("this is not a number")) 357 %!error double (pyobject ("this is not a number"))
348 %!error double (pyobject ()) 358 %!error double (pyobject ())
349 %!error double (pyeval ("[1, 2, 3]")) 359 %!error double (pyeval ("[1, 2, 3]"))
350 360
361 ## Test class type check method pyobject.isa
362 %!assert (isa (pyobject (), "handle"))
363 %!assert (isa (pyobject (), "pyobject"))
364 %!assert (! isa (pyobject (), "py.None"))
365 %!assert (isa (pyobject (0), "handle"))
366 %!assert (isa (pyobject (0), "pyobject"))
367 %!assert (isa (pyobject (0), "py.float"))
368 %!assert (isa (pyobject (int32 (0)), "py.int"))
369 %!assert (isa (pyobject (true), "py.bool"))
370 %!assert (isa (pyobject ("a string"), "py.str"))
371 %!assert (isa (pyobject (struct ()), "py.dict"))
372 %!assert (isa (pyobject (cell ()), "py.tuple"))
373 %!assert (isa (pyobject ([1, 2, 3, 4]), "py.array.array"))
374
351 ## Test conversion method pyobject.int64 375 ## Test conversion method pyobject.int64
352 %!assert (int64 (pyobject (int8 (0))), int64 (0)) 376 %!assert (int64 (pyobject (int8 (0))), int64 (0))
353 %!assert (int64 (pyobject (int64 (42))), int64 (42)) 377 %!assert (int64 (pyobject (int64 (42))), int64 (42))
354 %!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64")) 378 %!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64"))
355 %!assert (int64 (pyobject (intmin ("int64"))), intmin ("int64")) 379 %!assert (int64 (pyobject (intmin ("int64"))), intmin ("int64"))