changeset 367:9d7188514f2c

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.
author Mike Miller <mtmiller@octave.org>
date Thu, 25 Aug 2016 15:26:32 -0700
parents 5c900b8383c4
children 4d54fb68de71
files @py/subsref.m @pyobject/methods.m @pyobject/pyobject.m @pyobject/subsasgn.m @pyobject/subsref.m
diffstat 5 files changed, 9 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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
--- 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
--- 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");