diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index b4698d816..019c9340c 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -155,7 +155,7 @@ public: PYBIND11_NOINLINE bool load(handle src, bool convert) { if (!src || !typeinfo) return false; - if (src.ptr() == Py_None) { + if (src.is_none()) { value = nullptr; return true; } else if (PyType_IsSubtype(Py_TYPE(src.ptr()), typeinfo->type)) { @@ -180,7 +180,7 @@ public: const void *existing_holder = nullptr) { void *src = const_cast(_src); if (src == nullptr) - return handle(Py_None).inc_ref(); + return none(); auto &internals = get_internals(); @@ -408,7 +408,7 @@ template <> class type_caster { public: bool load(handle, bool) { return false; } static handle cast(void_type, return_value_policy /* policy */, handle /* parent */) { - return handle(Py_None).inc_ref(); + return none(); } PYBIND11_TYPE_CASTER(void_type, _("None")); }; @@ -420,7 +420,7 @@ public: bool load(handle h, bool) { if (!h) { return false; - } else if (h.ptr() == Py_None) { + } else if (h.is_none()) { value = nullptr; return true; } @@ -446,7 +446,7 @@ public: if (ptr) return capsule(ptr).release(); else - return handle(Py_None).inc_ref(); + return none(); } template using cast_op_type = void*&; @@ -558,12 +558,12 @@ protected: template <> class type_caster : public type_caster { public: bool load(handle src, bool convert) { - if (src.ptr() == Py_None) return true; + if (src.is_none()) return true; return type_caster::load(src, convert); } static handle cast(const char *src, return_value_policy /* policy */, handle /* parent */) { - if (src == nullptr) return handle(Py_None).inc_ref(); + if (src == nullptr) return none(); return PyUnicode_FromString(src); } @@ -581,12 +581,12 @@ public: template <> class type_caster : public type_caster { public: bool load(handle src, bool convert) { - if (src.ptr() == Py_None) return true; + if (src.is_none()) return true; return type_caster::load(src, convert); } static handle cast(const wchar_t *src, return_value_policy /* policy */, handle /* parent */) { - if (src == nullptr) return handle(Py_None).inc_ref(); + if (src == nullptr) return none(); return PyUnicode_FromWideChar(src, (ssize_t) wcslen(src)); } @@ -757,7 +757,7 @@ public: bool load(handle src, bool convert) { if (!src || !typeinfo) { return false; - } else if (src.ptr() == Py_None) { + } else if (src.is_none()) { value = nullptr; return true; } else if (PyType_IsSubtype(Py_TYPE(src.ptr()), typeinfo->type)) { diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 0df8129cb..7f032fc3a 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -20,7 +20,7 @@ template struct type_caster::value, void_type, Return>::type retval_type; public: bool load(handle src_, bool) { - if (src_.ptr() == Py_None) + if (src_.is_none()) return true; src_ = detail::get_function(src_); @@ -62,7 +62,7 @@ public: template static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { if (!f_) - return handle(Py_None).inc_ref(); + return none(); auto result = f_.template target(); if (result) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0d16c7a1f..dd04e9550 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1143,7 +1143,7 @@ inline void keep_alive_impl(handle nurse, handle patient) { if (!nurse || !patient) pybind11_fail("Could not activate keep_alive!"); - if (patient.ptr() == Py_None || nurse.ptr() == Py_None) + if (patient.is_none() || nurse.is_none()) return; /* Nothing to keep alive or nothing to be kept alive by */ cpp_function disable_lifesupport(