changeset 348:d4744d829ee0

Improve _InOctave dict handling by removing exec New code thanks to Abhinav Tripathi. * python_to_octave.cc: New implementation.
author Colin Macdonald <cbm@m.fsf.org>
date Wed, 10 Aug 2016 15:41:05 -0700
parents 7f039ffe501b
children 140e37e8e95a
files python_to_octave.cc
diffstat 1 files changed, 3 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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<std::string> (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);
   }