Skip to content

Commit

Permalink
class_: Register reclaim_from_cpp with constructing type
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Apr 14, 2020
1 parent 6ab4acb commit 188026e
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1392,11 +1392,17 @@ class class_ : public detail::generic_type {
auto* tinfo = get_type_info();
// TODO(eric.cousineau): Should relax this to not require a holder be constructed,
// only that the holder itself be default (unique_ptr<>).
if (inst->owned || v_h.holder_constructed()) {
throw std::runtime_error("Derived Python object should live in C++");
if (inst->owned) {
throw std::runtime_error(
"reclaim_from_cpp: Python object already owned! Did you forget to explicitly use a "
"py::return_value_policy (e.g. reference or reference internal) when passing back "
"non-owned pointers of the C++ object?");
}
if (v_h.holder_constructed()) {
throw std::runtime_error("reclaim_from_cpp: Holder already exists - internal error?");
}
if (!external_holder_raw) {
throw std::runtime_error("Internal error - not external holder?");
throw std::runtime_error("reclaim_from_cpp: No external holder - internal error?");
}
// Is this valid?
handle h(reinterpret_cast<PyObject*>(inst));
Expand Down Expand Up @@ -1425,7 +1431,7 @@ class class_ : public detail::generic_type {
break;
}
default: {
throw std::runtime_error("Unsupported load type");
throw std::runtime_error("reclaim_from_cpp: Unsupported load type");
}
}
inst->owned = true;
Expand Down Expand Up @@ -1716,6 +1722,9 @@ class class_ : public detail::generic_type {
// method? (Rather than a class method?)
object del_orig = getattr(h_type, "__del__", none());

// Set reclaim method.
inst->reclaim_from_cpp = reclaim_from_cpp;

#if defined(PYPY_VERSION)
// PyPy will not execute an arbitrarily-added `__del__` method.
// Later version of PyPy throw an error if this happens.
Expand Down

0 comments on commit 188026e

Please sign in to comment.