changeset 368:4d54fb68de71

__py_is_none__: new compiled function to test whether an object is None * __py_struct_from_dict__.cc (F__py_is_none__): New function. * @py/subsref.m, @pyobject/subsref.m, pycall.cc: Use __py_is_none__ instead of ad hoc lambda expression.
author Mike Miller <mtmiller@octave.org>
date Thu, 25 Aug 2016 16:27:46 -0700
parents 9d7188514f2c
children 540b36e797c8
files @py/subsref.m @pyobject/subsref.m __py_struct_from_dict__.cc pycall.cc
diffstat 4 files changed, 36 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/@py/subsref.m	Thu Aug 25 15:26:32 2016 -0700
+++ b/@py/subsref.m	Thu Aug 25 16:27:46 2016 -0700
@@ -65,8 +65,7 @@
     endif
   endif
 
-  is_none = pyeval ("lambda x: x is None");
-  if (nargout > 0 || ! pycall (is_none, y))
+  if (nargout > 0 || ! __py_is_none__ (y))
     varargout{1} = y;
   endif
 
--- a/@pyobject/subsref.m	Thu Aug 25 15:26:32 2016 -0700
+++ b/@pyobject/subsref.m	Thu Aug 25 16:27:46 2016 -0700
@@ -96,8 +96,7 @@
   endif
 
   ## unpack results, ensure "ans" works (see also pycall)
-  is_none = pyeval ("lambda x: x is None");
-  if (nargout == 0 && ! pycall (is_none, r))
+  if (nargout == 0 && ! __py_is_none__ (r))
     varargout{1} = r;
   elseif (nargout == 1)
     varargout{1} = r;
--- a/__py_struct_from_dict__.cc	Thu Aug 25 15:26:32 2016 -0700
+++ b/__py_struct_from_dict__.cc	Thu Aug 25 16:27:46 2016 -0700
@@ -78,6 +78,39 @@
   return retval;
 }
 
+DEFUN_DLD (__py_is_none__, args, nargout,
+           "-*- texinfo -*-\n\
+@deftypefn  {} {} __py_is_none__ (@var{x})\n\
+Check whether the Python object @var{obj} is the @code{None} object.\n\
+\n\
+This is a private internal function not intended for direct use.\n\
+@end deftypefn")
+{
+  if (args.length () != 1)
+    print_usage ();
+
+  Py_Initialize ();
+
+  PyObject *obj = pytave::pyobject_unwrap_object (args(0));
+
+  bool retval = (obj && (obj == Py_None));
+  Py_XDECREF (obj);
+
+  return ovl (retval);
+}
+
+/*
+%!assert (__py_is_none__ (pyobject ()))
+%!assert (__py_is_none__ (pyeval ("None")))
+%!assert (! __py_is_none__ (1))
+%!assert (! __py_is_none__ ("None"))
+%!assert (! __py_is_none__ (pyobject (1)))
+%!assert (! __py_is_none__ (pyobject ("None")))
+
+%!error __py_is_none__ ()
+%!error __py_is_none__ (1, 2)
+*/
+
 DEFUN_DLD (__py_isinstance__, args, nargout,
            "-*- texinfo -*-\n\
 @deftypefn  {} {} __py_isinstance__ (@var{x})\n\
--- a/pycall.cc	Thu Aug 25 15:26:32 2016 -0700
+++ b/pycall.cc	Thu Aug 25 16:27:46 2016 -0700
@@ -278,8 +278,7 @@
 %!test
 %! f = pyeval ("lambda: None");
 %! r = pycall (f);
-%! is_none = pyeval ("lambda x: x is None");
-%! assert (is_none (r))
+%! assert (__py_is_none__ (r))
 
 ## But returning None will not set "ans"
 %!test