# HG changeset patch # User Mike Miller # Date 1472167666 25200 # Node ID 4d54fb68de714b112612bed122f9999e25e9c236 # Parent 9d7188514f2c71e99e86b69fb9e49b452d6b3871 __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. diff -r 9d7188514f2c -r 4d54fb68de71 @py/subsref.m --- 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 diff -r 9d7188514f2c -r 4d54fb68de71 @pyobject/subsref.m --- 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; diff -r 9d7188514f2c -r 4d54fb68de71 __py_struct_from_dict__.cc --- 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\ diff -r 9d7188514f2c -r 4d54fb68de71 pycall.cc --- 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