# HG changeset patch # User Colin Macdonald # Date 1470868865 25200 # Node ID d4744d829ee06ff917d23c32a5620f0ad6760d5d # Parent 7f039ffe501bec2eb0c75d15721cc231b2ca9efe Improve _InOctave dict handling by removing exec New code thanks to Abhinav Tripathi. * python_to_octave.cc: New implementation. diff -r 7f039ffe501b -r d4744d829ee0 python_to_octave.cc --- a/python_to_octave.cc Wed Aug 10 11:34:19 2016 -0700 +++ b/python_to_octave.cc Wed Aug 10 15:41:05 2016 -0700 @@ -315,7 +315,6 @@ const boost::python::object& py_object) { object main_module = import ("__main__"); - object main_namespace = main_module.attr ("__dict__"); object builtins_module; pytave::get_builtins_module (builtins_module); object hex_function = builtins_module.attr ("hex"); @@ -324,12 +323,10 @@ std::string id = extract (idtmp); // Ensure _InOctave dict exists - // TODO: don't need exec, just do here in C - exec ("if not getattr(__import__('__main__'), '_InOctave', None):\n" - " __import__('__main__')._InOctave = dict()", - main_namespace, main_namespace); + if (! PyObject_HasAttrString (main_module.ptr (), "_InOctave")) + main_module.attr ("_InOctave") = boost::python::dict (); - main_namespace["_InOctave"][id] = py_object; + main_module.attr ("_InOctave")[id] = py_object; // Create @pyobject oct_value = feval ("pyobject", ovl (0, id), 2); }