diff options
-rw-r--r-- | src/py_account.cc | 8 | ||||
-rw-r--r-- | src/py_amount.cc | 7 | ||||
-rw-r--r-- | src/py_balance.cc | 8 | ||||
-rw-r--r-- | src/py_commodity.cc | 6 | ||||
-rw-r--r-- | src/py_utils.cc | 9 | ||||
-rw-r--r-- | src/py_value.cc | 29 | ||||
-rw-r--r-- | src/pyutils.h | 9 |
7 files changed, 54 insertions, 22 deletions
diff --git a/src/py_account.cc b/src/py_account.cc index d1d35cda..056cd722 100644 --- a/src/py_account.cc +++ b/src/py_account.cc @@ -32,6 +32,7 @@ #include <system.hh> #include "pyinterp.h" +#include "pyutils.h" #include "account.h" #include "post.h" @@ -90,6 +91,10 @@ namespace { return account.xdata(); } + PyObject * py_account_unicode(account_t& account) { + return str_to_py_unicode(account.fullname()); + } + } // unnamed namespace void export_account() @@ -180,7 +185,8 @@ void export_account() .def_readwrite("note", &account_t::note) .def_readonly("depth", &account_t::depth) - .def(self_ns::str(self)) + .def("__str__", &account_t::fullname) + .def("__unicode__", py_account_unicode) .def("fullname", &account_t::fullname) .def("partial_name", &account_t::partial_name) diff --git a/src/py_amount.cc b/src/py_amount.cc index 09d3294e..8fb507a3 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -98,6 +98,10 @@ namespace { return amount.strip_annotations(keep); } + PyObject * py_amount_unicode(amount_t& amount) { + return str_to_py_unicode(amount.to_string()); + } + } // unnamed namespace #define EXC_TRANSLATOR(type) \ @@ -248,8 +252,9 @@ internal precision.")) .def("__int__", &amount_t::to_long) .def("fits_in_long", &amount_t::fits_in_long) - .def("to_string", &amount_t::to_string) .def("__str__", &amount_t::to_string) + .def("to_string", &amount_t::to_string) + .def("__unicode__", py_amount_unicode) .def("to_fullstring", &amount_t::to_fullstring) .def("__repr__", &amount_t::to_fullstring) .def("quantity_string", &amount_t::quantity_string) diff --git a/src/py_balance.cc b/src/py_balance.cc index 760730a7..8c0c4c58 100644 --- a/src/py_balance.cc +++ b/src/py_balance.cc @@ -105,6 +105,10 @@ namespace { return balance.strip_annotations(keep); } + PyObject * py_balance_unicode(balance_t& balance) { + return str_to_py_unicode(balance.to_string()); + } + } // unnamed namespace #define EXC_TRANSLATOR(type) \ @@ -152,7 +156,9 @@ void export_balance() .def(self != long()) .def(! self) - .def(self_ns::str(self)) + .def("__str__", &balance_t::to_string) + .def("to_string", &balance_t::to_string) + .def("__unicode__", py_balance_unicode) .def("negated", &balance_t::negated) .def("in_place_negate", &balance_t::in_place_negate, diff --git a/src/py_commodity.cc b/src/py_commodity.cc index c201d370..984be5f0 100644 --- a/src/py_commodity.cc +++ b/src/py_commodity.cc @@ -232,6 +232,10 @@ namespace { return ann.price = price; } + PyObject * py_commodity_unicode(commodity_t& commodity) { + return str_to_py_unicode(commodity.symbol()); + } + } // unnamed namespace void export_commodity() @@ -331,6 +335,8 @@ void export_commodity() make_getter(&commodity_t::european_by_default), make_setter(&commodity_t::european_by_default)) + .def("__str__", &commodity_t::symbol) + .def("__unicode__", py_commodity_unicode) .def("__nonzero__", &commodity_t::operator bool) .def(self == self) diff --git a/src/py_utils.cc b/src/py_utils.cc index 2736ed3e..5203599f 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -79,15 +79,8 @@ struct string_to_python { static PyObject* convert(const string& str) { -#if 1 - // Return a Unicode object - PyObject * pstr = PyString_FromString(str.c_str()); - PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); - return object(handle<>(borrowed(uni))).ptr(); -#else - // Return a 7-bit ASCII string + // Return bytes, not characters; see __unicode__ methods for that return incref(object(static_cast<const std::string&>(str)).ptr()); -#endif } }; diff --git a/src/py_value.cc b/src/py_value.cc index 1a77da72..713dc3d4 100644 --- a/src/py_value.cc +++ b/src/py_value.cc @@ -103,6 +103,11 @@ namespace { value_t py_strip_annotations_1(value_t& value, const keep_details_t& keep) { return value.strip_annotations(keep); } + + PyObject * py_value_unicode(value_t& value) { + return str_to_py_unicode(value.to_string()); + } + } // unnamed namespace #define EXC_TRANSLATOR(type) \ @@ -115,16 +120,16 @@ EXC_TRANSLATOR(value_error) void export_value() { enum_< value_t::type_t >("ValueType") - .value("VOID", value_t::VOID) - .value("BOOLEAN", value_t::BOOLEAN) - .value("DATETIME", value_t::DATETIME) - .value("DATE", value_t::DATE) - .value("INTEGER", value_t::INTEGER) - .value("AMOUNT", value_t::AMOUNT) - .value("BALANCE", value_t::BALANCE) - .value("STRING", value_t::STRING) - .value("SEQUENCE", value_t::SEQUENCE) - .value("SCOPE", value_t::SCOPE) + .value("Void", value_t::VOID) + .value("Boolean", value_t::BOOLEAN) + .value("DateTime", value_t::DATETIME) + .value("Date", value_t::DATE) + .value("Integer", value_t::INTEGER) + .value("Amount", value_t::AMOUNT) + .value("Balance", value_t::BALANCE) + .value("String", value_t::STRING) + .value("Sequence", value_t::SEQUENCE) + .value("Scope", value_t::SCOPE) ; class_< value_t > ("Value") @@ -309,11 +314,13 @@ void export_value() .def("to_date", &value_t::to_date) .def("to_amount", &value_t::to_amount) .def("to_balance", &value_t::to_balance) + .def("__str__", &value_t::to_string) + .def("__unicode__", py_value_unicode) .def("to_string", &value_t::to_string) .def("to_mask", &value_t::to_mask) .def("to_sequence", &value_t::to_sequence) - .def("__str__", py_dump_relaxed) + .def("__unicode__", py_dump_relaxed) .def("__repr__", py_dump) .def("casted", &value_t::casted) diff --git a/src/pyutils.h b/src/pyutils.h index d8a46527..54d6fa28 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -126,6 +126,15 @@ struct map_value_type_converter } }; +template <typename T> +PyObject * str_to_py_unicode(const T& str) +{ + using namespace boost::python; + PyObject * pstr = PyString_FromString(str.c_str()); + PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); + return object(handle<>(borrowed(uni))).ptr(); +} + namespace boost { namespace python { // Use expr to create the PyObject corresponding to x |