changeset 321:3afd890fff6d

Rewrite pyobject.cell, always return a cell array of the right size * @pyobject/cell.m: Rewrite, ensuring return value is always a cell array of the correct size. Add more %!tests.
author Mike Miller <mtmiller@octave.org>
date Thu, 11 Aug 2016 22:35:19 -0700
parents c2aa34730dc9
children 2461b86cb8fb
files @pyobject/cell.m
diffstat 1 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/cell.m	Thu Aug 11 16:12:35 2016 -0700
+++ b/@pyobject/cell.m	Thu Aug 11 22:35:19 2016 -0700
@@ -1,4 +1,5 @@
 ## Copyright (C) 2016 Colin B. Macdonald
+## Copyright (C) 2016 Mike Miller
 ##
 ## This file is part of Pytave
 ##
@@ -19,7 +20,8 @@
 ## -*- texinfo -*-
 ## @documentencoding UTF-8
 ## @defmethod @@pyobject cell (@var{x})
-## Convert an iterable Python object to a cell array.
+## Convert a Python list or other object implementing the Sequence protocol
+## to a cell array.
 ##
 ## For example, by default Python lists are not automatically
 ## converted into native Octave objects:
@@ -68,17 +70,23 @@
 
 
 function c = cell (x)
-  ## FIXME: subsref should take care of this case
-  if (length (x) == 0)
-    c = cell (0, 1);
-    return
+  ## FIXME: when subsref returns the right number of output args, this can
+  ##        simply be "c = {x{:}}"
+  n = length (x);
+  c = cell (1, n);
+  if (n > 0)
+    [c{:}] = subsref (x, struct ("type", "{}", "subs", {{":"}}));
   endif
-  c = subsref (x, struct ("type", "{}", "subs", {{":"}}));
 endfunction
 
 
-%!test
-%! L = pyeval ("(1, 2, 3)");
-%! C = cell (L);
-%! assert (iscell (C))
-%! assert (C, {1, 2, 3})
+%!assert (cell (pyeval ("[]")), cell (1, 0))
+%!assert (cell (pyeval ("[1]")), {1})
+%!assert (cell (pyeval ("[1, 2, 3]")), {1, 2, 3})
+%!assert (cell (pyeval ("(1, 2, 3)")), {1, 2, 3})
+%!assert (cell (pyobject ("asdf")), {"a", "s", "d", "f"})
+%!assert (cell (pyeval ("range(10)")), num2cell (0:9))
+
+%!error cell (pyobject ())
+%!error cell (pyeval ("None"))
+%!error cell (pyobject (1))