From e851c02d2747be0acc336bc278da9e0460b75738 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 28 Jan 2009 20:49:26 -0400 Subject: Unit test for Python are now auto-generated from the C++ tests. --- python/py_amount.cc | 58 +++++++++++++++++++++++++++++++++++++++----------- python/py_commodity.cc | 11 +++++++++- python/py_times.cc | 22 +++++++++++++------ 3 files changed, 71 insertions(+), 20 deletions(-) (limited to 'python') diff --git a/python/py_amount.cc b/python/py_amount.cc index 5a68b6d5..195429a0 100644 --- a/python/py_amount.cc +++ b/python/py_amount.cc @@ -97,13 +97,23 @@ void py_parse_str_2(amount_t& amount, const string& str, unsigned char flags) { amount.parse(str, flags); } +void py_print(amount_t& amount, object out) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast(out.ptr())); + amount.print(outstr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.print_(file) is not a file object"); + } +} + void py_read_1(amount_t& amount, object in) { if (PyFile_Check(in.ptr())) { pyifstream instr(reinterpret_cast(in.ptr())); amount.read(instr); } else { PyErr_SetString(PyExc_IOError, - "Argument to amount.parse(file) is not a file object"); + "Argument to amount.read(file) is not a file object"); } } void py_read_2(amount_t& amount, const std::string& str) { @@ -111,6 +121,25 @@ void py_read_2(amount_t& amount, const std::string& str) { amount.read(p); } +void py_write_xml_1(amount_t& amount, object out) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast(out.ptr())); + amount.write_xml(outstr); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.write_xml(file) is not a file object"); + } +} + void py_write_xml_2(amount_t& amount, object out, const int depth) { + if (PyFile_Check(out.ptr())) { + pyofstream outstr(reinterpret_cast(out.ptr())); + amount.write_xml(outstr, depth); + } else { + PyErr_SetString(PyExc_IOError, + "Argument to amount.write_xml(file, depth) is not a file object"); + } +} + #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_ArithmeticError, err.what()); \ @@ -124,12 +153,10 @@ void export_amount() scope().attr("AMOUNT_PARSE_NO_REDUCE") = AMOUNT_PARSE_NO_REDUCE; class_< amount_t > ("Amount") -#if 0 .def("initialize", &amount_t::initialize) .staticmethod("initialize") .def("shutdown", &amount_t::shutdown) .staticmethod("shutdown") -#endif .add_static_property("current_pool", make_getter(&amount_t::current_pool, @@ -266,7 +293,7 @@ internal precision.") .def(double() / self) #endif - .add_property("precision", &amount_t::precision) + .def("precision", &amount_t::precision) .def("negate", &amount_t::negate) .def("in_place_negate", &amount_t::in_place_negate, @@ -317,22 +344,21 @@ internal precision.") #endif .def("fits_in_long", &amount_t::fits_in_long) - .add_property("quantity_string", &amount_t::quantity_string) + .def("quantity_string", &amount_t::quantity_string) - .add_property("commodity", - make_function(&amount_t::commodity, - return_value_policy()), - make_function(&amount_t::set_commodity, - with_custodian_and_ward<1, 2>())) + .def("commodity", &amount_t::commodity, + return_value_policy()) + .def("set_commodity", &amount_t::set_commodity, + with_custodian_and_ward<1, 2>()) .def("has_commodity", &amount_t::has_commodity) .def("clear_commodity", &amount_t::clear_commodity) - .add_property("number", &amount_t::number) + .def("number", &amount_t::number) .def("annotate", &amount_t::annotate) .def("is_annotated", &amount_t::is_annotated) #if 0 - .add_property("annotation", &amount_t::annotation) + .def("annotation", &amount_t::annotation) #endif .def("strip_annotations", &amount_t::strip_annotations) @@ -344,10 +370,18 @@ internal precision.") .def("parse_conversion", &amount_t::parse_conversion) .staticmethod("parse_conversion") + .def("print_", py_print) + .def("read", py_read_1) .def("read", py_read_2) .def("write", &amount_t::write) + .def("read_xml", &amount_t::read_xml) + .def("write_xml", py_write_xml_1) + .def("write_xml", py_write_xml_2) + + .def("dump", &amount_t::dump) + .def("valid", &amount_t::valid) ; diff --git a/python/py_commodity.cc b/python/py_commodity.cc index 6611a900..df9cca18 100644 --- a/python/py_commodity.cc +++ b/python/py_commodity.cc @@ -40,6 +40,13 @@ namespace ledger { using namespace boost::python; +void py_add_price(commodity_t& commodity, + const datetime_t& date, + const amount_t& price) +{ + commodity.add_price(date, price); +} + void export_commodity() { scope().attr("COMMODITY_STYLE_DEFAULTS") = COMMODITY_STYLE_DEFAULTS; @@ -57,7 +64,9 @@ void export_commodity() .def("drop_flags", &commodity_t::drop_flags) - .add_property("precision", &commodity_t::precision) + .def("add_price", py_add_price) + + .def("precision", &commodity_t::precision) ; } diff --git a/python/py_times.cc b/python/py_times.cc index 0fc62941..23532cc2 100644 --- a/python/py_times.cc +++ b/python/py_times.cc @@ -32,12 +32,6 @@ #include "pyinterp.h" #include "pyutils.h" -#include -#include -#include -#include - -#include #include // jww (2007-05-04): Convert time duration objects to PyDelta @@ -121,12 +115,26 @@ struct datetime_from_python typedef register_python_conversion datetime_python_conversion; +datetime_t py_parse_datetime(const string& str) { + return parse_datetime(str); +} + +date_t py_parse_date(const string& str) { + return parse_date(str); +} + void export_times() { - date_python_conversion(); datetime_python_conversion(); + date_python_conversion(); register_optional_to_python(); + register_optional_to_python(); + + scope().attr("parse_datetime") = &py_parse_datetime; + scope().attr("parse_date") = &py_parse_date; + scope().attr("current_time") = current_time; + scope().attr("current_date") = current_date; } } // namespace ledger -- cgit v1.2.3