# HG changeset patch # User Mike Miller # Date 1472163992 25200 # Node ID 9d7188514f2c71e99e86b69fb9e49b452d6b3871 # Parent 5c900b8383c41ec946c1709a00ccefd61015f721 Use pyobject.isa instead of lambda to check Python types * @py/subsref.m, @pyobject/methods.m, @pyobject/pyobject.m, @pyobject/subsasgn.m, @pyobject/subsref.m: Use pyobject.isa to check Python types more efficiently. diff -r 5c900b8383c4 -r 9d7188514f2c @py/subsref.m --- a/@py/subsref.m Thu Aug 25 14:57:29 2016 -0700 +++ b/@py/subsref.m Thu Aug 25 15:26:32 2016 -0700 @@ -60,8 +60,7 @@ ## is a Python callable, then call it with no arguments to be compatible ## with how Octave functions are evaluated. if (idx(end).type == ".") - is_callable = pyeval ("lambda x: isinstance(x, __import__('collections').Callable)"); - if (pycall (is_callable, y)) + if (isa (y, "py.collections.Callable")) y = pycall (y); endif endif diff -r 5c900b8383c4 -r 9d7188514f2c @pyobject/methods.m --- a/@pyobject/methods.m Thu Aug 25 14:57:29 2016 -0700 +++ b/@pyobject/methods.m Thu Aug 25 15:26:32 2016 -0700 @@ -75,10 +75,7 @@ mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false); if (nargout == 0) - ## FIXME: should this be available as @pyobject/ismodule.m ? - is_module = pyeval ("lambda x: isinstance(x, __import__('types').ModuleType)"); - - if (pycall (is_module, x)) + if (isa (x, "py.types.ModuleType")) modulename = char (pycall ("getattr", x, "__name__")); printf ("Methods for Python module '%s':\n", modulename); else diff -r 5c900b8383c4 -r 9d7188514f2c @pyobject/pyobject.m --- a/@pyobject/pyobject.m Thu Aug 25 14:57:29 2016 -0700 +++ b/@pyobject/pyobject.m Thu Aug 25 15:26:32 2016 -0700 @@ -104,8 +104,7 @@ endfunction function y = double (x) - fn = pyeval ("lambda x: isinstance(x, __import__('array').array)"); - if (pycall (fn, x)) + if (isa (x, "py.array.array")) c = cell (x); y = cellfun (@(t) eval ("double (t)"), c); else diff -r 5c900b8383c4 -r 9d7188514f2c @pyobject/subsasgn.m --- a/@pyobject/subsasgn.m Thu Aug 25 14:57:29 2016 -0700 +++ b/@pyobject/subsasgn.m Thu Aug 25 15:26:32 2016 -0700 @@ -50,14 +50,11 @@ ## XXX: doesn't support slices or anything like that yet ## Subtract one from index: do this for lists, numpy arrays, etc - pyexec ("import collections") - pyexec ("import numpy") - x_is_list = pycall (pyeval ( - "lambda x: isinstance(x, (collections.Sequence, numpy.ndarray))"), - x); + x_is_sequence = any (isa (x, {"py.collections.Sequence", ... + "py.numpy.ndarray"})); for i = 1:length (idx.subs) j = idx.subs{i}; - if (x_is_list && isindex (j) && isnumeric (j)) + if (x_is_sequence && isindex (j) && isnumeric (j)) idx.subs{i} = cast (j, class (sizemax ())) - 1; endif endfor diff -r 5c900b8383c4 -r 9d7188514f2c @pyobject/subsref.m --- a/@pyobject/subsref.m Thu Aug 25 14:57:29 2016 -0700 +++ b/@pyobject/subsref.m Thu Aug 25 15:26:32 2016 -0700 @@ -45,12 +45,9 @@ case "{}" ## Determine the types and protocols that we are able to index into - x_is_mapping = pycall (pyeval ( - "lambda x: isinstance(x, __import__('collections').Mapping)"), x); - x_is_sequence = pycall (pyeval ( - ["lambda x: isinstance(x, (__import__('collections').Sequence, " ... - "__import__('array').array, " ... - "__import__('numpy').ndarray))"]), x); + x_is_mapping = isa (x, "py.collections.Mapping"); + x_is_sequence = any (isa (x, {"py.collections.Sequence", ... + "py.array.array", "py.numpy.ndarray"})); if (! (x_is_mapping || x_is_sequence)) error ("subsref: cannot index Python object, not sequence or mapping");