Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builtin converter for None <-> std::nullptr_t #406

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/boost/python/converter/builtin_converters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ namespace detail
: ::PyInt_FromLong(x), &PyInt_Type)
#endif

#ifndef BOOST_NO_CXX11_NULLPTR
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::nullptr_t, boost::python::detail::none(), Py_TYPE(Py_None))
#endif

// Bool is not signed.
#if PY_VERSION_HEX >= 0x02030000
BOOST_PYTHON_TO_PYTHON_BY_VALUE(bool, ::PyBool_FromLong(x), &PyBool_Type)
Expand Down
24 changes: 24 additions & 0 deletions src/converter/builtin_converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ namespace
};
#endif

#ifndef BOOST_NO_CXX11_NULLPTR
struct nullptr_rvalue_from_python
{
static unaryfunc* get_slot(PyObject* obj)
{
return obj == Py_None ? &py_object_identity : 0;
}

static std::nullptr_t extract(PyObject*)
{
return nullptr;
}

static PyTypeObject const* get_pytype()
{
return Py_TYPE(Py_None);
}
};
#endif

// A SlotPolicy for extracting bool from a Python object
struct bool_rvalue_from_python
{
Expand Down Expand Up @@ -549,6 +569,10 @@ BOOST_PYTHON_DECL PyObject* do_arg_to_python(PyObject* x)

void initialize_builtin_converters()
{
#ifndef BOOST_NO_CXX11_NULLPTR
slot_rvalue_from_python<std::nullptr_t,nullptr_rvalue_from_python>();
#endif

// booleans
slot_rvalue_from_python<bool,bool_rvalue_from_python>();

Expand Down
6 changes: 6 additions & 0 deletions test/builtin_converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ BOOST_PYTHON_MODULE(builtin_converters_ext)
def("long_long_size", by_value<BOOST_PYTHON_LONG_LONG>::size);
#endif

#ifndef BOOST_NO_CXX11_NULLPTR
def("rewrap_value_nullptr_t", by_value<std::nullptr_t>::rewrap);
#endif
def("rewrap_value_bool", by_value<bool>::rewrap);
def("rewrap_value_char", by_value<char>::rewrap);
def("rewrap_value_signed_char", by_value<signed char>::rewrap);
Expand Down Expand Up @@ -121,6 +124,9 @@ BOOST_PYTHON_MODULE(builtin_converters_ext)
def("rewrap_value_mutable_cstring", rewrap_value_mutable_cstring);


#ifndef BOOST_NO_CXX11_NULLPTR
def("rewrap_const_reference_nullptr_t", by_const_reference<std::nullptr_t>::rewrap);
#endif
def("rewrap_const_reference_bool", by_const_reference<bool>::rewrap);
def("rewrap_const_reference_char", by_const_reference<char>::rewrap);
def("rewrap_const_reference_signed_char", by_const_reference<signed char>::rewrap);
Expand Down
9 changes: 9 additions & 0 deletions test/test_builtin_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
... def rewrap_const_reference_unsigned_long_long(x): return long(x)
>>> if not 'long_long_size' in dir():
... def long_long_size(): return long_size()
>>> if not 'rewrap_value_nullptr_t' in dir():
... def rewrap_value_nullptr_t(x): return x
... def rewrap_const_reference_nullptr_t(x): return x

>>> rewrap_value_nullptr_t(None) is None
True

>>> try: bool_exists = bool
... except: pass
Expand Down Expand Up @@ -156,6 +162,9 @@
>>> rewrap_value_mutable_cstring('hello, world')
'hello, world'

>>> rewrap_const_reference_nullptr_t(None) is None
True

>>> rewrap_const_reference_bool(None)
0
>>> rewrap_const_reference_bool(0)
Expand Down