provide get_py and get_py_ptr
authorDiane Trout <diane@caltech.edu>
Thu, 7 Dec 2006 23:49:39 +0000 (23:49 +0000)
committerDiane Trout <diane@caltech.edu>
Thu, 7 Dec 2006 23:49:39 +0000 (23:49 +0000)
I didn't like (*get_py)['name' as a way of getting a python namespace
object. So I changed get_py back to returning a reference by introducing
a new function get_py_ptr which creates the python singleton and returns
a pointer to it.

py/python.cpp
py/python.hpp
py/test/test_python.cpp
qui/mussagl.cpp
qui/threading/InterpreterThread.cpp
qui/threading/InterpreterThread.hpp

index 75a844fbe6d9080b4111e764ab5b6e2592e8f7ee..0de06766470fd882e0f7a0bd857800973718fa7d 100644 (file)
@@ -133,11 +133,16 @@ py::object MussaPython::operator[](std::string name)
 }
 
 //! return a reference to a single mussa python interpreter
-MussaPython *get_py()
+MussaPython *get_py_ptr()
 {
   static MussaPython *py;
   if (!py) {
     py = new MussaPython;
   }
   return py;
+}
+
+MussaPython &get_py()
+{
+  return *get_py_ptr();
 }
\ No newline at end of file
index 990305f611e4f97c6e68245b1831edbbe1c88a56..4312f9cc0df070ef21c0fdf6830a94605bf26516 100644 (file)
@@ -37,6 +37,7 @@ class MussaPython {
 };
 
 //! return a reference to a single mussa python interpreter
-MussaPython *get_py();
+MussaPython *get_py_ptr();
+MussaPython &get_py();
 
 #endif // _MUSSA_PYTHON_HPP_
index a733bea1eaacc345d1d99bb1cc45b07072cc5583..c33b7ad8278e0038769fd8eead8b97ec70a0bd49 100644 (file)
@@ -8,21 +8,21 @@ namespace py = boost::python;
 
 BOOST_AUTO_TEST_CASE( execute_python )
 {
-  get_py()->run("x = 3");
-  int x = py::extract<int>(get_py()->eval("x"));
+  get_py().run("x = 3");
+  int x = py::extract<int>(get_py().eval("x"));
   BOOST_CHECK_EQUAL(x, 3);
 }
 
 BOOST_AUTO_TEST_CASE( lookup_python )
 {
-  get_py()->run("import os");
-  py::object splitext = (*get_py())["os.path.splitext"];
+  get_py().run("import os");
+  py::object splitext = get_py()["os.path.splitext"];
   py::object result = splitext("/home/diane/foo.txt");
   std::string ext = py::extract<std::string>(result[1]);
   BOOST_CHECK_EQUAL(ext, ".txt");
 
-  get_py()->run("from os.path import splitext");
-  py::object splitext2 = (*get_py())["splitext"];
+  get_py().run("from os.path import splitext");
+  py::object splitext2 = get_py()["splitext"];
   py::object result2 = splitext("/home/diane/bar.txt");
   std::string ext2 = py::extract<std::string>(result2[1]);
   BOOST_CHECK_EQUAL(ext, ext2);
index 3001cbb29ddaeb127b0e89f5541aa8651abdd399..54d737be7f7dbabce71394b5b64104178bc39bb5 100644 (file)
@@ -72,7 +72,7 @@ int main(int argc, char **argv)
         std::cerr << "Unable to initialize interpeter thread" << std::endl;
         return 1;
       }
-      MussaPython *py = interp->get_py();
+      MussaPython *py = interp->get_py_ptr();
       if (!py) {
         std::cerr << "Unable to initialize python interpreter" << std::endl;
         return 1;
index bb7bae8cc47c9ac6f7fa10970bd7fc82b972c380..5f93287f0db24293f3d105f32b4dafc0729e29f2 100644 (file)
@@ -4,12 +4,12 @@
 
 void InterpreterThread::run()
 {
-  ::get_py()->interpreter();
+  ::get_py_ptr()->interpreter();
 }
 
-MussaPython *InterpreterThread::get_py()
+MussaPython *InterpreterThread::get_py_ptr()
 {
-  return ::get_py();
+  return ::get_py_ptr();
 }
 
 InterpreterThread::InterpreterThread()
index a2ad09f38f05567dbeb71485fbc4bb51ac0150a7..330bad7bf04410ea8e5a119b54f47225bfe9969c 100644 (file)
@@ -8,7 +8,7 @@ class InterpreterThread : public QThread {
   Q_OBJECT
 public:
   void run();
-  MussaPython *get_py();
+  MussaPython *get_py_ptr();
 private:
   //! only let ThreadManager create this object.
   InterpreterThread();