annotate @py/subsref.m @ 277:3bdd924a5fc7

py: call function or object with no arguments to be consistent with Octave * @py/subsref.m: If the returned Python object is callable and no argument list is given, call it with no arguments.
author Mike Miller <mtmiller@octave.org>
date Fri, 29 Jul 2016 14:54:15 -0700
parents e4175c38b012
children 9d7188514f2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
252
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
1 ## Copyright (C) 2016 Mike Miller
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
2 ##
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
3 ## This file is part of Pytave.
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
4 ##
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
5 ## Pytave is free software; you can redistribute it and/or modify it
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
8 ## your option) any later version.
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
9 ##
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
10 ## Pytave is distributed in the hope that it will be useful, but
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
14 ##
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
16 ## along with Pytave; see the file COPYING. If not, see
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
18
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
20 ## @deftypefn {} {} subsref (@var{x}, @var{idx})
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
21 ## Implements Python name lookup via dot indexing.
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
22 ## @enddeftypefn
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
23
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
24 function varargout = subsref (x, idx)
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
25
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
26 if (nargin != 2)
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
27 print_usage ();
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
28 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
29
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
30 if (! isa (x, "py"))
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
31 error ("py: invalid call to subsref");
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
32 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
33
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
34 if (isempty (idx) || ! isstruct (idx))
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
35 error ("py: invalid call to subsref without indices");
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
36 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
37
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
38 type = idx(1).type;
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
39 subs = idx(1).subs;
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
40
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
41 if (type != ".")
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
42 error ("py: invalid indexing type");
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
43 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
44
276
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
45 if (type == "." && ((numel (idx) == 1) || (idx(2).type != ".")))
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
46 try
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
47 y = pyeval (subs);
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
48 catch
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
49 y = pycall ("__import__", subs);
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
50 end_try_catch
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
51 else
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
52 y = pycall ("__import__", subs);
e4175c38b012 py: handle calling builtin functions with "py.foo" syntax (fixes issue #43)
Mike Miller <mtmiller@octave.org>
parents: 252
diff changeset
53 endif
252
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
54
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
55 if (numel (idx) > 1)
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
56 y = subsref (y, idx(2:end));
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
57 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
58
277
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
59 ## If the *last* indexing operation is ".name", and the object returned
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
60 ## is a Python callable, then call it with no arguments to be compatible
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
61 ## with how Octave functions are evaluated.
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
62 if (idx(end).type == ".")
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
63 is_callable = pyeval ("lambda x: isinstance(x, __import__('collections').Callable)");
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
64 if (pycall (is_callable, y))
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
65 y = pycall (y);
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
66 endif
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
67 endif
3bdd924a5fc7 py: call function or object with no arguments to be consistent with Octave
Mike Miller <mtmiller@octave.org>
parents: 276
diff changeset
68
252
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
69 is_none = pyeval ("lambda x: x is None");
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
70 if (nargout > 0 || ! pycall (is_none, y))
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
71 varargout{1} = y;
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
72 endif
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
73
8ebebd557e15 py: initial support for calling Python functions with "py.foo" syntax
Mike Miller <mtmiller@octave.org>
parents:
diff changeset
74 endfunction