Skip to content

Commit ae1c1b3

Browse files
committed
Improved None <==> NULL correspondence
[SVN r13155]
1 parent 7407855 commit ae1c1b3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

include/boost/python/to_python_indirect.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ namespace detail
6767
return python::objects::class_object<T>::reference;
6868
}
6969
};
70+
71+
// null_pointer_to_none -- return none() for null pointers and 0 for all other types/values
72+
//
73+
// Uses simulated partial ordering
74+
template <class T>
75+
inline PyObject* null_pointer_to_none(T&, int)
76+
{
77+
return 0;
78+
}
79+
80+
// overload for pointers
81+
template <class T>
82+
inline PyObject* null_pointer_to_none(T* x, long)
83+
{
84+
return x == 0 ? python::detail::none() : 0;
85+
}
7086
}
7187

7288
template <class T, class MakeHolder>
@@ -79,6 +95,10 @@ inline bool to_python_indirect<T,MakeHolder>::convertible()
7995
template <class T, class MakeHolder>
8096
inline PyObject* to_python_indirect<T,MakeHolder>::operator()(T x) const
8197
{
98+
PyObject* const null_result = detail::null_pointer_to_none(x, 1L);
99+
if (null_result != 0)
100+
return null_result;
101+
82102
PyObject* raw_result = type()->tp_alloc(type(), 0);
83103

84104
if (raw_result == 0)

0 commit comments

Comments
 (0)