From: Diane Trout Date: Thu, 7 Dec 2006 23:49:39 +0000 (+0000) Subject: provide get_py and get_py_ptr X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=mussa.git;a=commitdiff_plain;h=c713e41c854b6038dafe6405a6cee74a246bd0a0 provide get_py and get_py_ptr 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. --- diff --git a/py/python.cpp b/py/python.cpp index 75a844f..0de0676 100644 --- a/py/python.cpp +++ b/py/python.cpp @@ -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 diff --git a/py/python.hpp b/py/python.hpp index 990305f..4312f9c 100644 --- a/py/python.hpp +++ b/py/python.hpp @@ -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_ diff --git a/py/test/test_python.cpp b/py/test/test_python.cpp index a733bea..c33b7ad 100644 --- a/py/test/test_python.cpp +++ b/py/test/test_python.cpp @@ -8,21 +8,21 @@ namespace py = boost::python; BOOST_AUTO_TEST_CASE( execute_python ) { - get_py()->run("x = 3"); - int x = py::extract(get_py()->eval("x")); + get_py().run("x = 3"); + int x = py::extract(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(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(result2[1]); BOOST_CHECK_EQUAL(ext, ext2); diff --git a/qui/mussagl.cpp b/qui/mussagl.cpp index 3001cbb..54d737b 100644 --- a/qui/mussagl.cpp +++ b/qui/mussagl.cpp @@ -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; diff --git a/qui/threading/InterpreterThread.cpp b/qui/threading/InterpreterThread.cpp index bb7bae8..5f93287 100644 --- a/qui/threading/InterpreterThread.cpp +++ b/qui/threading/InterpreterThread.cpp @@ -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() diff --git a/qui/threading/InterpreterThread.hpp b/qui/threading/InterpreterThread.hpp index a2ad09f..330bad7 100644 --- a/qui/threading/InterpreterThread.hpp +++ b/qui/threading/InterpreterThread.hpp @@ -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();