diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/amount.h | 3 | ||||
-rw-r--r-- | src/balance.h | 13 | ||||
-rw-r--r-- | src/draft.h | 2 | ||||
-rw-r--r-- | src/expr.h | 2 | ||||
-rw-r--r-- | src/exprbase.h | 3 | ||||
-rw-r--r-- | src/format.h | 2 | ||||
-rw-r--r-- | src/predicate.h | 2 | ||||
-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 | ||||
-rw-r--r-- | src/query.h | 2 | ||||
-rw-r--r-- | src/value.cc | 17 |
16 files changed, 86 insertions, 36 deletions
diff --git a/src/amount.h b/src/amount.h index a8c08905..a37efdb8 100644 --- a/src/amount.h +++ b/src/amount.h @@ -496,6 +496,9 @@ public: long to_long() const; bool fits_in_long() const; + operator string() const { + return to_string(); + } string to_string() const; string to_fullstring() const; string quantity_string() const; diff --git a/src/balance.h b/src/balance.h index 8a40dea9..826de134 100644 --- a/src/balance.h +++ b/src/balance.h @@ -461,6 +461,15 @@ public: * Conversion methods. A balance can be converted to an amount, but * only if contains a single component amount. */ + operator string() const { + return to_string(); + } + string to_string() const { + std::ostringstream buf; + print(buf); + return buf.str(); + } + amount_t to_amount() const { if (is_empty()) throw_(balance_error, _("Cannot convert an empty balance to an amount")); @@ -532,8 +541,8 @@ public: void print(std::ostream& out, const int first_width = -1, const int latter_width = -1, - const bool right_justify = true, - const bool colorize = true) const; + const bool right_justify = false, + const bool colorize = false) const; /** * Debugging methods. There are two methods defined to help with diff --git a/src/draft.h b/src/draft.h index 003dcefa..93e98ff5 100644 --- a/src/draft.h +++ b/src/draft.h @@ -86,7 +86,7 @@ public: if (! args.empty()) parse_args(args); } - ~draft_t() { + virtual ~draft_t() { TRACE_DTOR(draft_t); } @@ -87,7 +87,7 @@ public: parse(in, flags); } - ~expr_t() throw() { + virtual ~expr_t() { TRACE_DTOR(expr_t); } diff --git a/src/exprbase.h b/src/exprbase.h index d2bf5a6d..0b3466b0 100644 --- a/src/exprbase.h +++ b/src/exprbase.h @@ -92,8 +92,7 @@ public: { TRACE_CTOR(expr_base_t, "scope_t *"); } - - ~expr_base_t() throw() { + virtual ~expr_base_t() { TRACE_DTOR(expr_base_t); } diff --git a/src/format.h b/src/format.h index fc1272aa..a2bf1015 100644 --- a/src/format.h +++ b/src/format.h @@ -132,7 +132,7 @@ public: if (! _str.empty()) parse_format(_str); } - ~format_t() { + virtual ~format_t() { TRACE_DTOR(format_t); } diff --git a/src/predicate.h b/src/predicate.h index 943b5a12..b3b81f9b 100644 --- a/src/predicate.h +++ b/src/predicate.h @@ -75,7 +75,7 @@ public: : expr_t(in, flags), what_to_keep(_what_to_keep) { TRACE_CTOR(predicate_t, "std::istream&, keep_details_t, parse_flags_t"); } - ~predicate_t() throw() { + virtual ~predicate_t() { TRACE_DTOR(predicate_t); } 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 diff --git a/src/query.h b/src/query.h index fb73ef2a..ebc14020 100644 --- a/src/query.h +++ b/src/query.h @@ -276,7 +276,7 @@ public: if (! args.empty()) parse_args(args); } - ~query_t() throw() { + virtual ~query_t() { TRACE_DTOR(query_t); } diff --git a/src/value.cc b/src/value.cc index e2e748f4..cce4c4e8 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1114,18 +1114,18 @@ void value_t::in_place_cast(type_t cast_type) break; } - case BALANCE: + case BALANCE: { + const balance_t& bal(as_balance()); switch (cast_type) { case AMOUNT: { - const balance_t& temp(as_balance()); - if (temp.amounts.size() == 1) { + if (bal.amounts.size() == 1) { // Because we are changing the current balance value to an amount // value, and because set_amount takes a reference (and that memory is // about to be repurposed), we must pass in a copy. - set_amount(amount_t((*temp.amounts.begin()).second)); + set_amount(amount_t((*bal.amounts.begin()).second)); return; } - else if (temp.amounts.size() == 0) { + else if (bal.amounts.size() == 0) { set_amount(0L); return; } @@ -1135,10 +1135,17 @@ void value_t::in_place_cast(type_t cast_type) } break; } + case STRING: + if (bal.is_empty()) + set_string(""); + else + set_string(as_balance().to_string()); + return; default: break; } break; + } case STRING: switch (cast_type) { |