Fixed Ledger/Python byte vs. char Unicode bridge

This commit is contained in:
John Wiegley 2009-11-20 23:23:44 -05:00
parent bd455c9874
commit d5e957204c
7 changed files with 54 additions and 22 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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,

View file

@ -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)

View file

@ -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
}
};

View file

@ -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)

View file

@ -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