changeset 360:77ffcaaf8000

Make get_object_from_python only return reference to existing Python object * oct-py-util.cc (pytave::get_object_from_python): Only return existing Python object from a given pyobject instance. * pyeval.cc: Delete no longer valid test case.
author Mike Miller <mtmiller@octave.org>
date Wed, 24 Aug 2016 13:04:21 -0700
parents 4c2b748eaea0
children 07c1b457cb6b
files oct-py-util.cc pyeval.cc
diffstat 2 files changed, 5 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/oct-py-util.cc	Wed Aug 24 13:01:15 2016 -0700
+++ b/oct-py-util.cc	Wed Aug 24 13:04:21 2016 -0700
@@ -47,53 +47,17 @@
 
 void
 get_object_from_python (const octave_value& oct_value,
-                             boost::python::object& py_object)
+                        boost::python::object& py_object)
 {
-  bool by_name;
-  if (oct_value.is_string ())
-    by_name = true;
-  else if (oct_value.is_object () && oct_value.class_name () == "pyobject")
-    by_name = false;
-  else
-    {
-      py_object = boost::python::object();      //None
-      return;
-    }
-
-  object main_module = import ("__main__");
-  object builtins_module;
-  get_builtins_module (builtins_module);
-
-  if (by_name)
-    {
-      std::string module;
-      std::string func = oct_value.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 = import (module.c_str ());
-      py_object = mod.attr (func.c_str ());
-    }
-  else
+  if (oct_value.is_object () && oct_value.class_name () == "pyobject")
     {
       octave_value_list tmp = feval ("getid", ovl (oct_value), 1);
       std::string hexid = tmp(0).string_value ();
+      object main_module = import ("__main__");
       py_object = main_module.attr ("_in_octave")[hexid];
     }
+  else
+    py_object = boost::python::object (); // None
 }
 
 PyObject *
--- a/pyeval.cc	Wed Aug 24 13:01:15 2016 -0700
+++ b/pyeval.cc	Wed Aug 24 13:04:21 2016 -0700
@@ -194,13 +194,6 @@
 %! assert (pyeval ("myvar", myNS1), 2)
 %! assert (pyeval ("myvar", myNS2), 3)
 
-%!test
-%! pyexec ("if 'myvar' in globals(): del myvar")
-%! % Namespaces can also be passed as strings
-%! pyexec ("myNS = {}");
-%! pyexec ("myvar = 1.", "myNS");
-%! assert (pyeval ("myvar", "myNS"), 1);
-
 %!error <NameError>
 %! pyexec ("if 'myvar' in globals(): del myvar")
 %! % Variable defined in local namespace MUST not be available globally