diff --git a/include/boost/python/to_python_indirect.hpp b/include/boost/python/to_python_indirect.hpp index fbbfc6f7..a636ff66 100644 --- a/include/boost/python/to_python_indirect.hpp +++ b/include/boost/python/to_python_indirect.hpp @@ -67,6 +67,22 @@ namespace detail return python::objects::class_object::reference; } }; + + // null_pointer_to_none -- return none() for null pointers and 0 for all other types/values + // + // Uses simulated partial ordering + template + inline PyObject* null_pointer_to_none(T&, int) + { + return 0; + } + + // overload for pointers + template + inline PyObject* null_pointer_to_none(T* x, long) + { + return x == 0 ? python::detail::none() : 0; + } } template @@ -79,6 +95,10 @@ inline bool to_python_indirect::convertible() template inline PyObject* to_python_indirect::operator()(T x) const { + PyObject* const null_result = detail::null_pointer_to_none(x, 1L); + if (null_result != 0) + return null_result; + PyObject* raw_result = type()->tp_alloc(type(), 0); if (raw_result == 0)