diff options
author | John Wiegley <johnw@newartisans.com> | 2004-11-08 06:43:11 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 02:40:47 -0400 |
commit | c9fb11bd60a2170fb896d77ff8d7706f563ad597 (patch) | |
tree | 42bdf09e7d8727ba31d1d8dae9b4eb4b2a605441 /py_eval.h | |
parent | fa2ceaed13c031add578ee8eb33da0c9980b9fb1 (diff) | |
download | fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.tar.gz fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.tar.bz2 fork-ledger-c9fb11bd60a2170fb896d77ff8d7706f563ad597.zip |
updated to version 2.0
Diffstat (limited to 'py_eval.h')
-rw-r--r-- | py_eval.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/py_eval.h b/py_eval.h new file mode 100644 index 00000000..8327f9a4 --- /dev/null +++ b/py_eval.h @@ -0,0 +1,75 @@ +#ifndef _PYTHON_H +#define _PYTHON_H + +#include "valexpr.h" + +#include <string> +#include <iostream> + +#include <boost/python.hpp> + +using namespace boost::python; + +namespace ledger { + +enum py_eval_mode_t { + PY_EVAL_EXPR, + PY_EVAL_STMT, + PY_EVAL_MULTI +}; + +object python_eval(std::istream& in, py_eval_mode_t mode = PY_EVAL_EXPR); +object python_eval(const std::string& str, py_eval_mode_t mode = PY_EVAL_EXPR); +object python_eval(const char * c_str, py_eval_mode_t mode = PY_EVAL_EXPR); + +template <typename T> +bool python_call(const std::string& func_name, value_expr_t * arg_expr, + const details_t& details, T& result) +{ + try { + object func = python_eval(func_name); + if (arg_expr) { + if (arg_expr->right) { + list args; + args.append(details); + for (value_expr_t * arg = arg_expr; arg; arg = arg->right) { + assert(arg->kind == value_expr_t::O_ARG); + value_t value; + arg->left->compute(value, details); + args.append(value); + } + + if (PyObject * val = PyObject_CallObject(func.ptr(), + tuple(args).ptr())) { + result = extract<T>(val)(); + Py_DECREF(val); + } + else if (PyObject * err = PyErr_Occurred()) { + PyErr_Print(); + throw value_expr_error(std::string("While calling Python function '") + + func_name + "'"); + } + else { + return false; + } + } else { + assert(arg_expr->kind == value_expr_t::O_ARG); + value_t value; + arg_expr->left->compute(value, details); + result = call<T>(func.ptr(), details, value); + } + } else { + result = call<T>(func.ptr(), details); + } + return true; + } + catch(const boost::python::error_already_set&) { + PyErr_Print(); + throw value_expr_error(std::string("While calling Python function '") + + func_name + "'"); + } + } + +} // namespace ledger + +#endif // _PYTHON_H |