From 723dd41df17d3c4730ebbe8d1b10e4cb577870c7 Mon Sep 17 00:00:00 2001 From: David Sklar <177495+davidsklar@users.noreply.github.com> Date: Sat, 14 Jan 2023 14:28:09 -0500 Subject: Fix python/c++ conversion for boost::optional Setters for types wrapped in boost::optional, such as item_t::note were broken, e.g. setting a note on a transaction resulted in garbled data that would cause Python to throw utf-8 errors when retrieving the note. (But setters that accessed strings directly, e.g. "payee" on a transaction worked fine.) This change alters the from-python conversion for optional-wrapped types based on the example at https://stackoverflow.com/questions/36485840/wrap-boostoptional-using-boostpython and a test case to verify the behavior. --- src/pyutils.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/pyutils.h') diff --git a/src/pyutils.h b/src/pyutils.h index 6c9675b7..7bc0d0af 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -88,13 +88,14 @@ struct register_optional_to_python : public boost::noncopyable { using namespace boost::python::converter; - void * const storage = - reinterpret_cast *>(data)->storage.bytes; + const T value = typename boost::python::extract(source); - if (data->convertible == source) // == None + void * storage = ((rvalue_from_python_storage>*) data)->storage.bytes; + + if (source == Py_None) // == None new (storage) boost::optional(); // A Boost uninitialized value else - new (storage) boost::optional(*reinterpret_cast(data->convertible)); + new (storage) boost::optional(value); data->convertible = storage; } -- cgit v1.2.3