annotate @pyobject/subsref.m @ 240:708715efa18e

pyobject: support callables with arguments (fixes issue #18) * @pyobject/subsref.m: Refactor and implement callables with args.
author Colin Macdonald <cbm@m.fsf.org>
date Mon, 25 Jul 2016 13:27:17 -0700
parents c56b85f405ba
children cc046496e173
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
1 ## Copyright (C) 2016 Colin B. Macdonald
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
2 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
3 ## This file is part of PyTave.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
4 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
5 ## OctSymPy is free software; you can redistribute it and/or modify
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
7 ## by the Free Software Foundation; either version 3 of the License,
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
8 ## or (at your option) any later version.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
9 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
10 ## This software is distributed in the hope that it will be useful,
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
13 ## the GNU General Public License for more details.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
14 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
16 ## License along with this software; see the file COPYING.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
17 ## If not, see <http://www.gnu.org/licenses/>.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
18
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
19 ## -*- texinfo -*-
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
20 ## @documentencoding UTF-8
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
21 ## @defop Method @@pyobject subsref (@var{x}, @var{idx})
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
22 ## @defopx Operator @@pyobject {@var{x}.@var{property}} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
23 ## @defopx Operator @@pyobject {@var{x}.@var{method}(@var{a}, @dots)} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
24 ## @defopx Operator @@pyobject {@var{x}@{@var{i}@}} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
25 ## @defopx Operator @@pyobject {@var{x}@{@var{i}, @var{j}, @dots@}} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
26 ## @defopx Operator @@pyobject {@var{x}(@var{a})} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
27 ## @defopx Operator @@pyobject {@var{x}(@var{a}, @var{b}, @dots)} {}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
28 ## Call methods and access properties of a Python object.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
29 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
30 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
31 ## @seealso{@@pyobject/subsasgn}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
32 ## @end defop
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
33
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
34
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
35 function [varargout] = subsref (x, idx)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
36
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
37 t = idx(1);
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
38 switch t.type
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
39 case "()"
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
40 r = pycall (x, t.subs{:});
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
41
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
42 case "."
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
43 assert (ischar (t.subs))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
44 r = pycall ("getattr", x, t.subs);
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
45
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
46 case "{}"
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
47 subsstrs = {};
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
48 for j = 1:length (t.subs)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
49 thissub = t.subs{j};
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
50 if (ischar (thissub) && strcmp (thissub, ":"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
51 subsstrs{j} = ":";
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
52 elseif (ischar (thissub))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
53 subsstrs{j} = ["'" thissub "'"];
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
54 elseif (isnumeric (thissub) && isscalar (thissub))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
55 ## note: python indexed from 0
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
56 subsstrs{j} = num2str (thissub - 1);
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
57 else
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
58 thissub
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
59 error ("@pyobject/subsref: subs not supported")
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
60 endif
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
61 endfor
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
62 s = ["[" strjoin(subsstrs, ", ") "]"];
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
63 ## XXX: can we use .__getitem__ here?
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
64 r = pyeval (sprintf ("__InOct__['%s']%s", x.id, s));
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
65
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
66 otherwise
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
67 t
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
68 error ("@pyobject/subsref: not implemented")
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
69 endswitch
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
70
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
71 ## deal with additional indexing (might be recursive)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
72 if (length (idx) > 1)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
73 r = subsref (r, idx(2:end));
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
74 end
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
75
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
76 ## unpack results, ensure "ans" works (see also pycall)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
77 isNone = pyeval ("lambda x: x is None");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
78 if (nargout > 0 || ! pycall (isNone, r))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
79 varargout{1} = r;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
80 assert (nargout <= 1)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
81 end
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
82 endfunction
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
83
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
84
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
85 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
86 %! % list indexing
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
87 %! pyexec ("L = [10, 20]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
88 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
89 %! assert (L{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
90 %! assert (L{2}, 20)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
91
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
92 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
93 %! % list indexing
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
94 %! pyexec ("L = [10, 20, [30, 40]]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
95 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
96 %! L2 = L{:};
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
97 %! assert (L2{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
98 %! assert (L2{2}, 20)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
99 %! assert (L2{3}{1}, 30)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
100 %! assert (L2{3}{2}, 40)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
101
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
102 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
103 %! % list indexing, nested list
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
104 %! pyexec ("L = [1, 2, [10, 11, 12]]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
105 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
106 %! assert (L{2}, 2)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
107 %! assert (L{3}{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
108 %! assert (L{3}{3}, 12)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
109
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
110 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
111 %! % 2D array indexing
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
112 %! pyexec ("import numpy")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
113 %! pyexec ("A = numpy.array([[1, 2], [3, 4]])")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
114 %! A = pyobject.fromPythonVarName ("A");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
115 %! assert (A{1, 1}, 1)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
116 %! assert (A{2, 1}, 3)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
117 %! assert (A{1, 2}, 2)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
118
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
119 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
120 %! % dict: str key access
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
121 %! pyexec ("d = {'one':1, 5:5, 6:6}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
122 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
123 %! assert (d{"one"}, 1)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
124
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
125 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
126 %! % dict: integer key access
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
127 %! pyexec ("d = {5:42, 6:42}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
128 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
129 %! assert (d{6}, 42)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
130
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
131 %!xtest
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
132 %! % dict: integer key should not subtract one (FIXME: Issue #10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
133 %! pyexec ("d = {5:40, 6:42}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
134 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
135 %! assert (d{6}, 42)
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
136
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
137 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
138 %! % method call with args
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
139 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
140 %! s.add (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
141 %! assert (length (s) == 3)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
142
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
143 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
144 %! % get a callable
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
145 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
146 %! sa = s.add;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
147 %! assert (isa (sa, "pyobject"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
148 %! % and then call it
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
149 %! sa (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
150 %! assert (length (s) == 3)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
151
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
152 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
153 %! % callable can return something
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
154 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
155 %! v = s.pop ();
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
156 %! assert (length (s) == 1)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
157 %! assert (v == 1 || v == 2)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
158
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
159 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
160 %! % chain
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
161 %! pyexec ("import sys")
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
162 %! s = pyeval ("set({sys})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
163 %! ver = s.pop ().version;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
164 %! assert (ischar (ver))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
165
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
166 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
167 %! % don't set "ans" if no return value
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
168 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
169 %! sa = s.add;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
170 %! clear ans
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
171 %! sa (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
172 %! assert (! exist ("ans", "var"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
173
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
174 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
175 %! % *do* set "ans" if return value
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
176 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
177 %! clear ans
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
178 %! s.pop ();
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
179 %! assert (exist ("ans", "var"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
180 %! assert (length (s) == 1)