Mercurial > pytave
changeset 192:9077907490d8
pycall: clean up determination of which module to import from
* pycall.cc (Fpycall): Reduce nested logic by importing the main and builtins
namespaces first.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 14 Jul 2016 23:05:56 -0700 |
parents | 0500459a739b |
children | d2385636bae5 |
files | pycall.cc |
diffstat | 1 files changed, 14 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/pycall.cc Tue Jul 05 12:00:15 2016 -0700 +++ b/pycall.cc Thu Jul 14 23:05:56 2016 -0700 @@ -87,29 +87,25 @@ try { object main_module = import ("__main__"); - object main_namespace = main_module.attr ("__dict__"); - - object mod, callable; +#if PY_VERSION_HEX >= 0x03000000 + object builtins_module = import ("builtins"); +#else + object builtins_module = import ("__builtin__"); +#endif - if (! module.empty ()) - { - mod = import (module.c_str ()); - } - if (! PyObject_HasAttrString (mod.ptr (), func.c_str ())) + object mod; + + if (module.empty ()) { - if (! PyObject_HasAttrString (main_module.ptr (), func.c_str ())) - { -#if PY_VERSION_HEX >= 0x03000000 - mod = import ("builtins"); -#else - mod = import ("__builtin__"); -#endif - } + if (PyObject_HasAttrString (main_module.ptr (), func.c_str ())) + mod = main_module; else - mod = main_module; + mod = builtins_module; } + else + mod = import (module.c_str ()); - callable = mod.attr (func.c_str ()); + object callable = mod.attr (func.c_str ()); std::vector<object> pyargs; for (int i = 1; i < nargin; i++)