# HG changeset patch # User Mike Miller # Date 1470702849 25200 # Node ID 616ec5f18d957efa1920f22425991ed4e6d3ac9e # Parent a133564f4af7f5185e17b9bb61676bd3d6b7e988 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. diff -r a133564f4af7 -r 616ec5f18d95 pycall.cc --- a/pycall.cc Mon Aug 08 16:17:21 2016 -0700 +++ b/pycall.cc Mon Aug 08 17:34:09 2016 -0700 @@ -77,7 +77,6 @@ @end deftypefn") { octave_value_list retval; - object res; std::string id; int nargin = args.length (); @@ -147,61 +146,18 @@ callable = main_module.attr ("__InOct__")[hexid]; } - std::vector pyargs; + PyObject *pyargs = PyTuple_New (nargin - 1); for (int i = 1; i < nargin; i++) { object arg; pytave::octvalue_to_pyobj (arg, args(i)); - pyargs.push_back (arg); + PyObject *obj = arg.ptr (); + Py_INCREF (obj); + PyTuple_SET_ITEM (pyargs, i - 1, obj); } - switch (nargin - 1) - { - case 0: - res = callable (); - break; - case 1: - res = callable (pyargs[0]); - break; - case 2: - res = callable (pyargs[0], pyargs[1]); - break; - case 3: - res = callable (pyargs[0], pyargs[1], pyargs[2]); - break; - case 4: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3]); - break; - case 5: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4]); - break; - case 6: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4], pyargs[5]); - break; - case 7: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4], pyargs[5], pyargs[6]); - break; - case 8: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4], pyargs[5], pyargs[6], pyargs[7]); - break; - case 9: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4], pyargs[5], pyargs[6], pyargs[7], - pyargs[8]); - break; - case 10: - res = callable (pyargs[0], pyargs[1], pyargs[2], pyargs[3], - pyargs[4], pyargs[5], pyargs[6], pyargs[7], - pyargs[8], pyargs[9]); - break; - default: - error ("pycall: more than 10 arguments are not yet supported"); - break; - } + PyObject *result = PyEval_CallObjectWithKeywords (callable.ptr (), pyargs, 0); + object res = object (handle (result)); // Ensure reasonable "ans" behaviour, consistent with Python's "_". if (nargout > 0 || ! res.is_none ())