diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 6ca49a89eb..1514faba1d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -602,7 +602,11 @@ struct type_caster::value && !is_std_char_t template struct void_caster { public: - bool load(handle, bool) { return false; } + bool load(handle src, bool) { + if (src && src.is_none()) + return true; + return false; + } static handle cast(T, return_value_policy /* policy */, handle /* parent */) { return none().inc_ref(); } @@ -653,7 +657,7 @@ template <> class type_caster : public type_caster { void *value = nullptr; }; -template <> class type_caster : public type_caster { }; +template <> class type_caster : public void_caster { }; template <> class type_caster { public: diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index 776c4ce389..a82145be5e 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -359,9 +359,10 @@ test_initializer python_types([](py::module &m) { const char *operator()(int) { return "int"; } const char *operator()(std::string) { return "std::string"; } const char *operator()(double) { return "double"; } + const char *operator()(std::nullptr_t) { return "std::nullptr_t"; } }; - m.def("load_variant", [](std::variant v) { + m.def("load_variant", [](std::variant v) { return std::visit(visitor(), v); }); @@ -516,6 +517,9 @@ test_initializer python_types([](py::module &m) { }); } ); + + m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile + m.def("cast_nullptr_t", []() { return std::nullptr_t{}; }); }); #if defined(_MSC_VER) diff --git a/tests/test_python_types.py b/tests/test_python_types.py index 45cf3e887e..3e337d4d5b 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -378,9 +378,10 @@ def test_variant(doc): assert load_variant(1) == "int" assert load_variant("1") == "std::string" assert load_variant(1.0) == "double" + assert load_variant(None) == "std::nullptr_t" assert cast_variant() == (5, "Hello") - assert doc(load_variant) == "load_variant(arg0: Union[int, str, float]) -> str" + assert doc(load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str" def test_constructors(): @@ -568,3 +569,10 @@ def test_capsule_with_destructor(capture): creating capsule destructing capsule: 1234 """ + + +def test_void_caster(): + import pybind11_tests as m + + assert m.load_nullptr_t(None) is None + assert m.cast_nullptr_t() is None