From e0b108ff3a64ae76d9fa420b35e35f77f30a54e0 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sun, 8 Feb 2009 21:17:23 -0400 Subject: Attribute lookup on a Value object which is a Scope now searches the scope. --- python/py_value.cc | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'python/py_value.cc') diff --git a/python/py_value.cc b/python/py_value.cc index f4fe11bc..4962f182 100644 --- a/python/py_value.cc +++ b/python/py_value.cc @@ -39,28 +39,40 @@ using namespace boost::python; BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(value_overloads, value, 0, 2) -string py_dump(const value_t& value) { - std::ostringstream buf; - value.dump(buf); - return buf.str(); -} +namespace { + expr_t py_value_getattr(value_t& value, const string& name) + { + if (value.is_pointer()) { + if (scope_t * scope = value.as_pointer_lval()) + return expr_t(scope->lookup(name), scope); + } + throw_(value_error, "Cannot lookup attributes in " << value.label()); + return expr_t(); + } -string py_dump_relaxed(const value_t& value) { - std::ostringstream buf; - value.dump(buf, true); - return buf.str(); -} + string py_dump(const value_t& value) { + std::ostringstream buf; + value.dump(buf); + return buf.str(); + } -void py_set_string(value_t& amount, const string& str) { - return amount.set_string(str); -} + string py_dump_relaxed(const value_t& value) { + std::ostringstream buf; + value.dump(buf, true); + return buf.str(); + } + + void py_set_string(value_t& amount, const string& str) { + return amount.set_string(str); + } #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ } -EXC_TRANSLATOR(value_error) + EXC_TRANSLATOR(value_error) +} void export_value() { @@ -262,6 +274,8 @@ void export_value() .def("print", &value_t::print) .def("valid", &value_t::valid) + + .def("__getattr__", py_value_getattr) ; enum_< value_t::type_t >("ValueType") -- cgit v1.2.3