changeset 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 72ecb31b163a 3e0decdf59b0
files pycall.cc
diffstat 1 files changed, 6 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- 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<object> 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<PyObject> (result));
 
       // Ensure reasonable "ans" behaviour, consistent with Python's "_".
       if (nargout > 0 || ! res.is_none ())