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.
}
//! return a reference to a single mussa python interpreter
}
//! return a reference to a single mussa python interpreter
+MussaPython *get_py_ptr()
{
static MussaPython *py;
if (!py) {
py = new MussaPython;
}
return py;
{
static MussaPython *py;
if (!py) {
py = new MussaPython;
}
return py;
+}
+
+MussaPython &get_py()
+{
+ return *get_py_ptr();
}
\ No newline at end of file
}
\ No newline at end of file
};
//! return a reference to a single mussa python interpreter
};
//! return a reference to a single mussa python interpreter
+MussaPython *get_py_ptr();
+MussaPython &get_py();
#endif // _MUSSA_PYTHON_HPP_
#endif // _MUSSA_PYTHON_HPP_
BOOST_AUTO_TEST_CASE( execute_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 )
{
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");
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);
py::object result2 = splitext("/home/diane/bar.txt");
std::string ext2 = py::extract<std::string>(result2[1]);
BOOST_CHECK_EQUAL(ext, ext2);
std::cerr << "Unable to initialize interpeter thread" << std::endl;
return 1;
}
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;
if (!py) {
std::cerr << "Unable to initialize python interpreter" << std::endl;
return 1;
void InterpreterThread::run()
{
void InterpreterThread::run()
{
- ::get_py()->interpreter();
+ ::get_py_ptr()->interpreter();
-MussaPython *InterpreterThread::get_py()
+MussaPython *InterpreterThread::get_py_ptr()
}
InterpreterThread::InterpreterThread()
}
InterpreterThread::InterpreterThread()
Q_OBJECT
public:
void run();
Q_OBJECT
public:
void run();
+ MussaPython *get_py_ptr();
private:
//! only let ThreadManager create this object.
InterpreterThread();
private:
//! only let ThreadManager create this object.
InterpreterThread();