# HG changeset patch # User Mike Miller # Date 1469049849 25200 # Node ID 237b71aaf6e7396a45660d4671178c7f985d3754 # Parent 0deddd1b0526322f699b14e7883bc26a10695b25 pycall: support Python reference to a callable object * pycall.cc (Fpycall): Add support for calling a reference to a Python function or callable object. diff -r 0deddd1b0526 -r 237b71aaf6e7 pycall.cc --- a/pycall.cc Wed Jul 20 13:32:29 2016 -0700 +++ b/pycall.cc Wed Jul 20 14:24:09 2016 -0700 @@ -27,7 +27,8 @@ #include #include -#include +#include +#include #define PYTAVE_DO_DECLARE_SYMBOL #include "arrayobjectdefs.h" @@ -68,15 +69,14 @@ return retval; } - std::string module; - std::string func = args(0).string_value (); + bool func_by_name = false; - size_t idx = func.rfind ("."); - if (idx != std::string::npos) - { - module = func.substr (0, idx); - func = func.substr (idx + 1); - } + if (args(0).is_string ()) + func_by_name = true; + else if (args(0).is_object () && args(0).class_name () == "pyobject") + func_by_name = false; + else + error ("pycall: FUNC must be a string or a Python reference"); Py_Initialize (); @@ -93,19 +93,39 @@ try { - object mod; + object callable; - if (module.empty ()) + if (func_by_name) { - if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) - mod = main_module; + std::string module; + std::string func = args(0).string_value (); + + size_t idx = func.rfind ("."); + if (idx != std::string::npos) + { + module = func.substr (0, idx); + func = func.substr (idx + 1); + } + + object mod; + if (module.empty ()) + { + if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) + mod = main_module; + else + mod = builtins_module; + } else - mod = builtins_module; + mod = import (module.c_str ()); + + callable = mod.attr (func.c_str ()); } else - mod = import (module.c_str ()); - - object callable = mod.attr (func.c_str ()); + { + octave_value_list tmp = feval ("getid", ovl (args(0)), 1); + std::string hexid = tmp(0).string_value (); + callable = main_module.attr ("__InOct__")[hexid]; + } std::vector pyargs; for (int i = 1; i < nargin; i++)