comparison pycall.cc @ 351:040aff46e4db

Refactor calling a Python function into a set of wrapper functions * oct-py-eval.cc, oct-py-eval.h (pytave::py_call_function): New function overloads to call a Python function in various ways. * pycall.cc (Fpycall): Use pytave::py_call_function.
author Mike Miller <mtmiller@octave.org>
date Wed, 17 Aug 2016 14:02:33 -0700
parents 252b458bd904
children eac35d84ef0d
comparison
equal deleted inserted replaced
350:e89a8a37fd8a 351:040aff46e4db
31 #include <octave/parse.h> 31 #include <octave/parse.h>
32 32
33 #define PYTAVE_DO_DECLARE_SYMBOL 33 #define PYTAVE_DO_DECLARE_SYMBOL
34 #include "arrayobjectdefs.h" 34 #include "arrayobjectdefs.h"
35 #include "exceptions.h" 35 #include "exceptions.h"
36 #include "oct-py-eval.h"
36 #include "oct-py-util.h" 37 #include "oct-py-util.h"
37 #include "octave_to_python.h" 38 #include "octave_to_python.h"
38 #include "python_to_octave.h" 39 #include "python_to_octave.h"
39 40
40 using namespace boost::python; 41 using namespace boost::python;
99 object callable; 100 object callable;
100 pytave::get_object_from_python (args(0), callable); 101 pytave::get_object_from_python (args(0), callable);
101 if (callable.is_none ()) 102 if (callable.is_none ())
102 error("pycall: FUNC must be a string or a Python reference"); 103 error("pycall: FUNC must be a string or a Python reference");
103 104
104 PyObject *args_list = PyList_New (0); 105 octave_value_list arglist = args.slice (1, nargin - 1);
105 PyObject *kwargs = 0; 106 PyObject *result = pytave::py_call_function (callable.ptr (), arglist);
106 for (int i = 1; i < nargin; i++)
107 {
108 object arg;
109 pytave::octvalue_to_pyobj (arg, args(i));
110 PyObject *obj = arg.ptr ();
111
112 if (pytave::is_py_kwargs_argument (obj))
113 kwargs = pytave::update_py_dict (kwargs, obj);
114 else
115 {
116 Py_INCREF (obj);
117 PyList_Append (args_list, obj);
118 }
119 }
120
121 PyObject *pyargs = PyList_AsTuple (args_list);
122 PyObject *result = PyEval_CallObjectWithKeywords (callable.ptr (), pyargs, kwargs);
123 object res = object (handle<PyObject> (result)); 107 object res = object (handle<PyObject> (result));
124
125 Py_DECREF (args_list);
126 Py_DECREF (pyargs);
127 if (kwargs)
128 Py_DECREF (kwargs);
129 108
130 // Ensure reasonable "ans" behaviour, consistent with Python's "_". 109 // Ensure reasonable "ans" behaviour, consistent with Python's "_".
131 if (nargout > 0 || ! res.is_none ()) 110 if (nargout > 0 || ! res.is_none ())
132 { 111 {
133 octave_value val; 112 octave_value val;