Skip to content

Add type casters for nullopt_t, fix none refcount #499

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

Merged
merged 3 commits into from
Nov 15, 2016
Merged
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
8 changes: 5 additions & 3 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,17 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value>> {
PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
};

template <> class type_caster<void_type> {
template<typename T> struct void_caster {
public:
bool load(handle, bool) { return false; }
static handle cast(void_type, return_value_policy /* policy */, handle /* parent */) {
static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
return none().inc_ref();
}
PYBIND11_TYPE_CASTER(void_type, _("None"));
PYBIND11_TYPE_CASTER(T, _("None"));
};

template <> class type_caster<void_type> : public void_caster<void_type> {};

template <> class type_caster<void> : public type_caster<void_type> {
public:
using type_caster<void_type>::cast;
Expand Down
8 changes: 7 additions & 1 deletion include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ template<typename T> struct optional_caster {

static handle cast(const T& src, return_value_policy policy, handle parent) {
if (!src)
return none();
return none().inc_ref();
return caster_type::cast(*src, policy, parent);
}

Expand All @@ -232,11 +232,17 @@ template<typename T> struct optional_caster {
#if PYBIND11_HAS_OPTIONAL
template<typename T> struct type_caster<std::optional<T>>
: public optional_caster<std::optional<T>> {};

template<> struct type_caster<std::nullopt_t>
: public void_caster<std::nullopt_t> {};
#endif

#if PYBIND11_HAS_EXP_OPTIONAL
template<typename T> struct type_caster<std::experimental::optional<T>>
: public optional_caster<std::experimental::optional<T>> {};

template<> struct type_caster<std::experimental::nullopt_t>
: public void_caster<std::experimental::nullopt_t> {};
#endif

NAMESPACE_END(detail)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_python_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ test_initializer python_types([](py::module &m) {
m.def("half_or_none", [](int x) -> opt_int {
return x ? opt_int(x / 2) : opt_int();
});
m.def("test_nullopt", [](opt_int x) {
return x.value_or(42);
}, py::arg_v("x", std::experimental::nullopt, "None"));
#endif
m.attr("has_optional") = py::cast(has_optional);
});
7 changes: 6 additions & 1 deletion tests/test_python_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def func(self, x, *args):

@pytest.mark.skipif(not has_optional, reason='no <experimental/optional>')
def test_optional():
from pybind11_tests import double_or_zero, half_or_none
from pybind11_tests import double_or_zero, half_or_none, test_nullopt

assert double_or_zero(None) == 0
assert double_or_zero(42) == 84
Expand All @@ -308,3 +308,8 @@ def test_optional():
assert half_or_none(0) is None
assert half_or_none(42) == 21
pytest.raises(TypeError, half_or_none, 'foo')

assert test_nullopt() == 42
assert test_nullopt(None) == 42
assert test_nullopt(42) == 42
assert test_nullopt(43) == 43