#include #include #include #include template struct object_from_python { object_from_python() { boost::python::converter::registry::insert (&TfromPy::convertible, &TfromPy::construct, boost::python::type_id()); } }; template struct register_python_conversion { register_python_conversion() { boost::python::to_python_converter(); object_from_python(); } }; template struct register_optional_to_python : public boost::noncopyable { struct optional_to_python { static PyObject * convert(const boost::optional& value) { return boost::python::incref (value ? boost::python::to_python_value()(*value) : boost::python::detail::none()); } }; struct optional_from_python { static void * convertible(PyObject * source) { using namespace boost::python::converter; if (source == Py_None) return source; const registration& converters(registered::converters); if (implicit_rvalue_convertible_from_python(source, converters)) { rvalue_from_python_stage1_data data = rvalue_from_python_stage1(source, converters); return rvalue_from_python_stage2(source, data, converters); } return NULL; } static void construct(PyObject * source, boost::python::converter::rvalue_from_python_stage1_data * data) { using namespace boost::python::converter; void * const storage = reinterpret_cast *>(data)->storage.bytes; if (data->convertible == source) // == None new (storage) boost::optional(); // A Boost uninitialized value else new (storage) boost::optional(*reinterpret_cast(data->convertible)); data->convertible = storage; } }; explicit register_optional_to_python() { register_python_conversion, optional_to_python, optional_from_python>(); } }; class amount_t { public: std::string hello() { return "Hello, world!"; } }; class annotation_t { public: boost::optional price; }; boost::optional py_price(annotation_t& ann) { return ann.price; } BOOST_PYTHON_MODULE(bug) { using namespace boost::python; class_< annotation_t > ("Annotation") .add_property("price", make_getter(&annotation_t::price)) .add_property("price2", py_price) ; register_optional_to_python(); }