comparison pycall.cc @ 359:4c2b748eaea0

Use py_find_function where specifically searching for a function by name * oct-py-eval.cc (pytave::py_call_function): Use py_find_function to look up Python function by name. * pycall.cc (Fpycall): Likewise, when argument is a string.
author Mike Miller <mtmiller@octave.org>
date Wed, 24 Aug 2016 13:01:15 -0700
parents eac35d84ef0d
children 1470ed26917a
comparison
equal deleted inserted replaced
358:d41fc23d4b9f 359:4c2b748eaea0
87 { 87 {
88 print_usage (); 88 print_usage ();
89 return retval; 89 return retval;
90 } 90 }
91 91
92 if (! (args(0).is_string () || (args(0).is_object ()
93 && args(0).class_name () == "pyobject")))
94 error ("pycall: FUNC must be a string or a Python reference");
95
92 Py_Initialize (); 96 Py_Initialize ();
93 97
94 pytave::init_exceptions (); 98 pytave::init_exceptions ();
95 numeric::array::set_module_and_type ("numpy", "ndarray"); 99 numeric::array::set_module_and_type ("numpy", "ndarray");
96 _import_array (); 100 _import_array ();
97 101
98 try 102 try
99 { 103 {
100 object callable; 104 PyObject *callable = 0;
101 pytave::get_object_from_python (args(0), callable); 105 if (args(0).is_string ())
102 if (callable.is_none ()) 106 callable = pytave::py_find_function (args(0).string_value ());
103 error("pycall: FUNC must be a string or a Python reference"); 107 else
108 {
109 // FIXME: callable = get existing pyobject reference (args(0))
110 boost::python::object obj;
111 pytave::get_object_from_python (args(0), obj);
112 if (obj.is_none ())
113 error("pycall: FUNC must be a string or a Python reference");
114 callable = obj.ptr ();
115 Py_INCREF (callable);
116 }
117
118 if (! callable)
119 error ("pycall: no such Python function or callable: %s",
120 args(0).string_value ().c_str ());
104 121
105 octave_value_list arglist = args.slice (1, nargin - 1); 122 octave_value_list arglist = args.slice (1, nargin - 1);
106 PyObject *result = pytave::py_call_function (callable.ptr (), arglist); 123 PyObject *result = pytave::py_call_function (callable, arglist);
107 object res = object (handle<PyObject> (result)); 124 object res = object (handle<PyObject> (result));
125 Py_DECREF (callable);
108 126
109 // Ensure reasonable "ans" behaviour, consistent with Python's "_". 127 // Ensure reasonable "ans" behaviour, consistent with Python's "_".
110 if (nargout > 0 || ! res.is_none ()) 128 if (nargout > 0 || ! res.is_none ())
111 { 129 {
112 octave_value val; 130 octave_value val;