diff options
-rw-r--r-- | python/pyinterp.cc | 21 | ||||
-rw-r--r-- | python/pyinterp.h | 44 | ||||
-rw-r--r-- | python/pyledger.cc | 12 | ||||
-rw-r--r-- | src/global.cc | 13 | ||||
-rw-r--r-- | src/global.h | 2 |
5 files changed, 46 insertions, 46 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(); } diff --git a/src/global.cc b/src/global.cc index 939a51b2..4d2f97fc 100644 --- a/src/global.cc +++ b/src/global.cc @@ -39,7 +39,14 @@ global_scope_t::global_scope_t(char ** envp) { TRACE_CTOR(global_scope_t, ""); - session_ptr.reset(new LEDGER_SESSION_T); +#if defined(HAVE_BOOST_PYTHON) + if (! python_session.get()) { + python_session.reset(new ledger::python_interpreter_t); + session_ptr = python_session; + } +#else + session_ptr.reset(new session_t); +#endif set_session_context(session_ptr.get()); @@ -75,6 +82,10 @@ global_scope_t::~global_scope_t() // object, and then shutting down the memory tracing subsystem. // Otherwise, let it all leak because we're about to exit anyway. IF_VERIFY() set_session_context(NULL); + +#if defined(HAVE_BOOST_PYTHON) + python_session.reset(); +#endif } void global_scope_t::read_init() diff --git a/src/global.h b/src/global.h index 85745ba2..0851cdaa 100644 --- a/src/global.h +++ b/src/global.h @@ -44,7 +44,7 @@ namespace ledger { class global_scope_t : public noncopyable, public scope_t { - scoped_ptr<session_t> session_ptr; + shared_ptr<session_t> session_ptr; ptr_list<report_t> report_stack; public: |