changeset 239:0401489ea58c

Allow pycall to return None It returns "None" when there is an explicit lvalue, but does not set "ans" to "None". * pycall.cc: Change return behaviour, add tests.
author Colin Macdonald <cbm@m.fsf.org>
date Mon, 25 Jul 2016 14:00:11 -0700
parents 89a1f5a82467
children 708715efa18e
files pycall.cc
diffstat 1 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pycall.cc	Fri Jul 22 10:23:40 2016 -0700
+++ b/pycall.cc	Mon Jul 25 14:00:11 2016 -0700
@@ -191,7 +191,8 @@
       object idtmp = hex_function (id_function (res));
       id = extract<std::string> (idtmp);
 
-      if (! res.is_none ())
+      // Ensure reasonable "ans" behaviour, consistent with Python's "_".
+      if (nargout > 0 || ! res.is_none ())
         {
           octave_value val;
           pytave::pyobj_to_octvalue (val, res);
@@ -280,4 +281,18 @@
 %!error <NameError>
 %! pyexec ("def raiseException ():\n  raise NameError ('oops')")
 %! pycall ("raiseException")
+
+## None as a return value
+%!test
+%! f = pyeval ("lambda: None");
+%! r = pycall (f);
+%! isNone = pyeval("lambda a: a is None");
+%! assert (isNone (r))
+
+## But returning None will not set "ans"
+%!test
+%! f = pyeval ("lambda: None");
+%! clear ans
+%! pycall (f);
+%! assert (! exist ("ans", "var"))
 */