Fixes to the new Python/scope integration code.

This commit is contained in:
John Wiegley 2009-02-08 23:56:28 -04:00
parent f50def86c4
commit fccf7e1cb5
4 changed files with 18 additions and 13 deletions

View file

@ -40,10 +40,10 @@ using namespace boost::python;
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(value_overloads, value, 0, 2)
namespace {
expr_t py_value_getattr(value_t& value, const string& name)
expr_t py_value_getattr(const value_t& value, const string& name)
{
if (value.is_pointer()) {
if (scope_t * scope = value.as_pointer_lval<scope_t>())
if (scope_t * scope = value.as_pointer<scope_t>())
return expr_t(scope->lookup(name), scope);
}
throw_(value_error, "Cannot lookup attributes in " << value.label());

View file

@ -33,7 +33,7 @@
namespace ledger {
using namespace boost::python;
using namespace python;
shared_ptr<python_interpreter_t> python_session;
@ -100,7 +100,7 @@ void python_interpreter_t::initialize()
Py_Initialize();
assert(Py_IsInitialized());
object main_module = boost::python::import("__main__");
object main_module = python::import("__main__");
if (! main_module)
throw_(std::logic_error, "Python failed to initialize");
@ -108,7 +108,7 @@ void python_interpreter_t::initialize()
if (! main_nspace)
throw_(std::logic_error, "Python failed to initialize");
boost::python::detail::init_module("ledger", &initialize_for_python);
python::detail::init_module("ledger", &initialize_for_python);
is_initialized = true;
}
@ -128,7 +128,7 @@ object python_interpreter_t::import(const string& str)
try {
TRACE_START(python_import, 1, "Imported Python module: " << str);
object mod = boost::python::import(str.c_str());
object mod = python::import(str.c_str());
if (! mod)
throw_(std::logic_error, "Failed to import Python module " << str);
@ -228,14 +228,14 @@ expr_t::ptr_op_t python_interpreter_t::lookup(const string& name)
break;
}
if (is_initialized && main_nspace.has_key(name)) {
if (is_initialized && main_nspace.has_key(name.c_str())) {
DEBUG("python.interp", "Python lookup: " << name);
if (boost::python::object obj = main_nspace.get(name))
if (python::object obj = main_nspace.get(name.c_str()))
return WRAP_FUNCTOR(functor_t(name, obj));
}
return expr_t::ptr_op_t();
return NULL;
}
value_t python_interpreter_t::functor_t::operator()(call_scope_t& args)
@ -257,8 +257,7 @@ value_t python_interpreter_t::functor_t::operator()(call_scope_t& args)
arglist.append(args.value());
if (PyObject * val =
PyObject_CallObject(func.ptr(),
boost::python::tuple(arglist).ptr())) {
PyObject_CallObject(func.ptr(), python::tuple(arglist).ptr())) {
extract<value_t> xval(val);
value_t result;
if (xval.check()) {

View file

@ -141,6 +141,13 @@ public:
return calc(*context);
}
scope_t * get_context() {
return context;
}
void set_context(scope_t * scope) {
context = scope;
}
bool is_constant() const;
bool is_function() const;

View file

@ -1,5 +1,4 @@
import ledger
def get_amount(item):
print "I found an amount:", item.amount()
return 123
return item.amount() * 100