diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-08 21:16:29 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-08 21:16:29 -0400 |
commit | 3924a3da10145ede4a831a58ac184d3658d48aca (patch) | |
tree | 3384088b961869f75ed60268f3145cec67b7fa36 /python | |
parent | d4d7090f3c673f092fdc2bba54e41582c85cff8b (diff) | |
download | fork-ledger-3924a3da10145ede4a831a58ac184d3658d48aca.tar.gz fork-ledger-3924a3da10145ede4a831a58ac184d3658d48aca.tar.bz2 fork-ledger-3924a3da10145ede4a831a58ac184d3658d48aca.zip |
pyinterp.cc shares global session; accept full paths passed to --import.
Diffstat (limited to 'python')
-rw-r--r-- | python/pyinterp.cc | 21 | ||||
-rw-r--r-- | python/pyinterp.h | 44 | ||||
-rw-r--r-- | python/pyledger.cc | 12 |
3 files changed, 33 insertions, 44 deletions
diff --git a/python/pyinterp.cc b/python/pyinterp.cc index 3648cdf4..fcd5d2c6 100644 --- a/python/pyinterp.cc +++ b/python/pyinterp.cc @@ -35,9 +35,12 @@ namespace ledger { using namespace boost::python; +shared_ptr<python_interpreter_t> python_session; + void export_chain(); void export_commodity(); void export_entry(); +void export_expr(); void export_flags(); void export_format(); void export_global(); @@ -57,6 +60,7 @@ void initialize_for_python() export_chain(); export_commodity(); export_entry(); + export_expr(); export_flags(); export_format(); export_global(); @@ -269,8 +273,7 @@ value_t python_interpreter_t::functor_t::operator()(call_scope_t& args) } else if (PyObject * err = PyErr_Occurred()) { PyErr_Print(); - throw_(calc_error, - "Failed call to Python function '" << name << "': " << err); + throw_(calc_error, "Failed call to Python function '" << name << "'"); } else { assert(false); } @@ -287,18 +290,4 @@ value_t python_interpreter_t::functor_t::operator()(call_scope_t& args) return NULL_VALUE; } -value_t python_interpreter_t::lambda_t::operator()(call_scope_t& args) -{ - try { - assert(args.size() == 1); - value_t item = args[0]; - return call<value_t>(func.ptr(), item); - } - catch (const error_already_set&) { - PyErr_Print(); - throw_(calc_error, "Failed to evaluate Python lambda expression"); - } - return NULL_VALUE; -} - } // namespace ledger diff --git a/python/pyinterp.h b/python/pyinterp.h index 7303644c..9e1a86a9 100644 --- a/python/pyinterp.h +++ b/python/pyinterp.h @@ -42,7 +42,7 @@ namespace ledger { class python_interpreter_t : public session_t { public: - boost::python::dict main_nspace; + python::dict main_nspace; bool is_initialized; python_interpreter_t() @@ -59,7 +59,7 @@ public: void initialize(); - boost::python::object import(const string& name); + python::object import(const string& name); enum py_eval_mode_t { PY_EVAL_EXPR, @@ -67,11 +67,11 @@ public: PY_EVAL_MULTI }; - boost::python::object eval(std::istream& in, + python::object eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR); - boost::python::object eval(const string& str, + python::object eval(const string& str, py_eval_mode_t mode = PY_EVAL_EXPR); - boost::python::object eval(const char * c_str, + python::object eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR) { string str(c_str); return eval(str, mode); @@ -81,14 +81,14 @@ public: functor_t(); protected: - boost::python::object func; + python::object func; public: string name; - functor_t(const string& _name, boost::python::object _func) + functor_t(const string& _name, python::object _func) : func(_func), name(_name) { - TRACE_CTOR(functor_t, "const string&, boost::python::object"); + TRACE_CTOR(functor_t, "const string&, python::object"); } functor_t(const functor_t& other) : func(other.func), name(other.name) { @@ -103,26 +103,22 @@ public: virtual expr_t::ptr_op_t lookup(const string& name); value_t option_import_(call_scope_t& args) { - import(args[0].to_string()); + path file(args[0].to_string()); + + python::object module_sys = import("sys"); + python::object sys_dict = module_sys.attr("__dict__"); + + python::list paths(sys_dict["path"]); + paths.insert(0, file.parent_path().string()); + sys_dict["path"] = paths; + + import(file.stem()); return true; } - - class lambda_t : public functor_t { - lambda_t(); - public: - lambda_t(boost::python::object code) : functor_t("<lambda>", code) { - TRACE_CTOR(functor_t, "boost::python::object"); - } - lambda_t(const lambda_t& other) : functor_t(other) { - TRACE_CTOR(lambda_t, "copy"); - } - virtual ~lambda_t() throw() { - TRACE_DTOR(lambda_t); - } - virtual value_t operator()(call_scope_t& args); - }; }; +extern shared_ptr<python_interpreter_t> python_session; + } // namespace ledger #endif // HAVE_BOOST_PYTHON diff --git a/python/pyledger.cc b/python/pyledger.cc index 1a5b42f4..963efe11 100644 --- a/python/pyledger.cc +++ b/python/pyledger.cc @@ -33,14 +33,18 @@ using namespace boost::python; -ledger::python_interpreter_t python_session; - namespace ledger { extern void initialize_for_python(); } BOOST_PYTHON_MODULE(ledger) { - ledger::set_session_context(&python_session); - ledger::initialize_for_python(); + using namespace ledger; + + if (! python_session.get()) + python_session.reset(new python_interpreter_t); + + set_session_context(python_session.get()); + + initialize_for_python(); } |