Mercurial > pytave
comparison pycall.cc @ 305:616ec5f18d95
pycall: refactor to call function with arbitrary number of positional args
* pycall.cc (Fpycall): Refactor to call Python function with a tuple of
positional arguments.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Mon, 08 Aug 2016 17:34:09 -0700 |
parents | a133564f4af7 |
children | 3e0decdf59b0 |
comparison
equal
deleted
inserted
replaced
304:a133564f4af7 | 305:616ec5f18d95 |
---|---|
75 \n\ | 75 \n\ |
76 @seealso{pyeval, pyexec}\n\ | 76 @seealso{pyeval, pyexec}\n\ |
77 @end deftypefn") | 77 @end deftypefn") |
78 { | 78 { |
79 octave_value_list retval; | 79 octave_value_list retval; |
80 object res; | |
81 std::string id; | 80 std::string id; |
82 | 81 |
83 int nargin = args.length (); | 82 int nargin = args.length (); |
84 | 83 |
85 if (nargin < 1) | 84 if (nargin < 1) |
145 octave_value_list tmp = feval ("getid", ovl (args(0)), 1); | 144 octave_value_list tmp = feval ("getid", ovl (args(0)), 1); |
146 std::string hexid = tmp(0).string_value (); | 145 std::string hexid = tmp(0).string_value (); |
147 callable = main_module.attr ("__InOct__")[hexid]; | 146 callable = main_module.attr ("__InOct__")[hexid]; |
148 } | 147 } |
149 | 148 |
150 std::vector<object> pyargs; | 149 PyObject *pyargs = PyTuple_New (nargin - 1); |
151 for (int i = 1; i < nargin; i++) | 150 for (int i = 1; i < nargin; i++) |
152 { | 151 { |
153 object arg; | 152 object arg; |
154 pytave::octvalue_to_pyobj (arg, args(i)); | 153 pytave::octvalue_to_pyobj (arg, args(i)); |
155 pyargs.push_back (arg); | 154 PyObject *obj = arg.ptr (); |
156 } | 155 Py_INCREF (obj); |
157 | 156 PyTuple_SET_ITEM (pyargs, i - 1, obj); |
158 switch (nargin - 1) | 157 } |
159 { | 158 |
160 case 0: | 159 PyObject *result = PyEval_CallObjectWithKeywords (callable.ptr (), pyargs, 0); |
161 res = callable (); | 160 object res = object (handle<PyObject> (result)); |
162 break; | |
163 case 1: | |
164 res = callable (pyargs[0]); | |
165 break; | |
166 case 2: | |
167 res = callable (pyargs[0], pyargs[1]); | |
168 break; | |
169 case 3: | |
170 res = callable (pyargs[0], pyargs[1], pyargs[2]); | |
171 break; | |
172 case 4: | |
173 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3]); | |
174 break; | |
175 case 5: | |
176 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
177 pyargs[4]); | |
178 break; | |
179 case 6: | |
180 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
181 pyargs[4], pyargs[5]); | |
182 break; | |
183 case 7: | |
184 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
185 pyargs[4], pyargs[5], pyargs[6]); | |
186 break; | |
187 case 8: | |
188 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
189 pyargs[4], pyargs[5], pyargs[6], pyargs[7]); | |
190 break; | |
191 case 9: | |
192 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
193 pyargs[4], pyargs[5], pyargs[6], pyargs[7], | |
194 pyargs[8]); | |
195 break; | |
196 case 10: | |
197 res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], | |
198 pyargs[4], pyargs[5], pyargs[6], pyargs[7], | |
199 pyargs[8], pyargs[9]); | |
200 break; | |
201 default: | |
202 error ("pycall: more than 10 arguments are not yet supported"); | |
203 break; | |
204 } | |
205 | 161 |
206 // Ensure reasonable "ans" behaviour, consistent with Python's "_". | 162 // Ensure reasonable "ans" behaviour, consistent with Python's "_". |
207 if (nargout > 0 || ! res.is_none ()) | 163 if (nargout > 0 || ! res.is_none ()) |
208 { | 164 { |
209 octave_value val; | 165 octave_value val; |