diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index b30b5be6d..fae8d89cf 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -185,7 +185,7 @@ public: const void *existing_holder = nullptr) { void *src = const_cast(_src); if (src == nullptr) - return none(); + return none().inc_ref(); auto &internals = get_internals(); @@ -414,7 +414,7 @@ template <> class type_caster { public: bool load(handle, bool) { return false; } static handle cast(void_type, return_value_policy /* policy */, handle /* parent */) { - return none(); + return none().inc_ref(); } PYBIND11_TYPE_CASTER(void_type, _("None")); }; @@ -452,7 +452,7 @@ public: if (ptr) return capsule(ptr).release(); else - return none(); + return none().inc_ref(); } template using cast_op_type = void*&; @@ -569,7 +569,7 @@ public: } static handle cast(const char *src, return_value_policy /* policy */, handle /* parent */) { - if (src == nullptr) return none(); + if (src == nullptr) return none().inc_ref(); return PyUnicode_FromString(src); } @@ -592,7 +592,7 @@ public: } static handle cast(const wchar_t *src, return_value_policy /* policy */, handle /* parent */) { - if (src == nullptr) return none(); + if (src == nullptr) return none().inc_ref(); return PyUnicode_FromWideChar(src, (ssize_t) wcslen(src)); } diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 7f032fc3a..ed269b9bf 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -62,7 +62,7 @@ public: template static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { if (!f_) - return none(); + return none().inc_ref(); auto result = f_.template target(); if (result)