diff options
-rw-r--r-- | src/py_amount.cc | 2 | ||||
-rw-r--r-- | src/py_utils.cc | 35 | ||||
-rw-r--r-- | src/pyutils.h | 5 |
3 files changed, 38 insertions, 4 deletions
diff --git a/src/py_amount.cc b/src/py_amount.cc index 8721f879..0bb5c26a 100644 --- a/src/py_amount.cc +++ b/src/py_amount.cc @@ -55,7 +55,6 @@ void export_amount() make_getter(&amount_t::current_pool, return_value_policy<reference_existing_object>())) -#if 0 .add_static_property("keep_base", &amount_t::keep_base) .add_static_property("keep_price", &amount_t::keep_price) @@ -63,7 +62,6 @@ void export_amount() .add_static_property("keep_tag", &amount_t::keep_tag) .add_static_property("stream_fullstrings", &amount_t::stream_fullstrings) -#endif .def(init<double>()) .def(init<long>()) diff --git a/src/py_utils.cc b/src/py_utils.cc index 0f82d683..4dfe8d7e 100644 --- a/src/py_utils.cc +++ b/src/py_utils.cc @@ -9,6 +9,40 @@ namespace ledger { using namespace boost::python; +struct bool_to_python +{ + static PyObject * convert(const bool truth) + { + if (truth) + Py_RETURN_TRUE; + else + Py_RETURN_FALSE; + } +}; + +struct bool_from_python +{ + static void* convertible(PyObject* obj_ptr) + { + if (!PyBool_Check(obj_ptr)) return 0; + return obj_ptr; + } + + static void construct(PyObject* obj_ptr, + converter::rvalue_from_python_stage1_data* data) + { + void* storage = ((converter::rvalue_from_python_storage<bool>*) data)->storage.bytes; + if (obj_ptr == Py_True) + new (storage) bool(true); + else + new (storage) bool(false); + data->convertible = storage; + } +}; + +typedef register_python_conversion<bool, bool_to_python, bool_from_python> + bool_python_conversion; + struct string_to_python { static PyObject* convert(const string& str) @@ -40,6 +74,7 @@ typedef register_python_conversion<string, string_to_python, string_from_python> void export_utils() { + bool_python_conversion(); string_python_conversion(); } diff --git a/src/pyutils.h b/src/pyutils.h index 42d5f1e0..51ca8734 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -27,8 +27,9 @@ struct register_optional_to_python : public boost::noncopyable { static PyObject * convert(const boost::optional<T>& value) { - return (value ? boost::python::to_python_value<T>()(*value) : - boost::python::detail::none()); + return boost::python::incref + (value ? boost::python::to_python_value<T>()(*value) : + boost::python::detail::none()); } }; |