Skip to content

Commit

Permalink
Cleanup casters to release none() to avoid ref counting (pybind#4269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylion007 authored and henryiii committed Oct 21, 2022
1 parent a8f2110 commit 251516b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ struct void_caster {
return false;
}
static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
return none().inc_ref();
return none().release();
}
PYBIND11_TYPE_CASTER(T, const_name("None"));
};
Expand Down Expand Up @@ -291,7 +291,7 @@ class type_caster<void> : public type_caster<void_type> {
if (ptr) {
return capsule(ptr).release();
}
return none().inc_ref();
return none().release();
}

template <typename T>
Expand Down Expand Up @@ -537,7 +537,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {

static handle cast(const CharT *src, return_value_policy policy, handle parent) {
if (src == nullptr) {
return pybind11::none().inc_ref();
return pybind11::none().release();
}
return StringCaster::cast(StringType(src), policy, parent);
}
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct type_caster<std::function<Return(Args...)>> {
template <typename Func>
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
if (!f_) {
return none().inc_ref();
return none().release();
}

auto result = f_.template target<function_type>();
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ struct optional_caster {
template <typename T>
static handle cast(T &&src, return_value_policy policy, handle parent) {
if (!src) {
return none().inc_ref();
return none().release();
}
if (!std::is_lvalue_reference<T>::value) {
policy = return_value_policy_override<Value>::policy(policy);
Expand Down

0 comments on commit 251516b

Please sign in to comment.