From 22ef8e59417f31bbf9d153c393ee4cd02936ba45 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Wed, 11 Oct 2006 23:43:33 +0000 Subject: [PATCH] lookup python code with c++ use boost::algorithm::split to split our python namespace reference. --- py/python.cpp | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/py/python.cpp b/py/python.cpp index 198d5f9..fe2364e 100644 --- a/py/python.cpp +++ b/py/python.cpp @@ -1,8 +1,13 @@ #include "py/python.hpp" +namespace py = boost::python; #include +#include +#include -namespace py = boost::python; +#include +#include +namespace alg = boost::algorithm; MussaPython::MussaPython() { @@ -64,21 +69,19 @@ void MussaPython::interpreter(FILE *fp) py::object MussaPython::operator[](std::string name) { - std::string code = "reduce(lambda m,n: m.__dict__[n], [__main__] + '"; - code += name; - code += "'.split('.'))"; - return eval(code); - /* - py::object py_name(name); - py::object name_list = py_name.attr("split")("."); - int name_list_len = extract(name_list.attr("__len__")()); + typedef std::vector< std::string > string_vector; + string_vector split_name; + alg::split(split_name, name, alg::is_any_of(".")); + py::object lookup = main_namespace["__main__"]; - for(int i=0; i < name_list_len; ++i) { - lookup = lookup.attr("__dict__")(name_list[i]); - //std::cout << lookup << std::endl; + + for (string_vector::const_iterator name_i = split_name.begin(); + name_i != split_name.end(); + ++name_i) + { + lookup = lookup.attr("__dict__")[*name_i]; } return lookup; - */ } //! return a reference to a single mussa python interpreter @@ -86,6 +89,4 @@ MussaPython& get_py() { static MussaPython py; return py; -} - - +} \ No newline at end of file -- 2.30.2