-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[smart_holder] Fix handling of const unique_ptr<T, D> &
(do not disown).
#5332
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…`rtrn_unique_ptr_cref()` to make the current behavior obvious.
…th MSVC, NVHPC, ICC
rwgk
pushed a commit
to rwgk/pybind11
that referenced
this pull request
Aug 25, 2024
PREPARATION for: PR pybind#5332 — Fix handling of const unique_ptr<T, D> & (do not disown). Splitting out so that the functional changes under PR pybind#5332 will be more obvious.
rwgk
pushed a commit
to rwgk/pybind11
that referenced
this pull request
Aug 25, 2024
PREPARATION for: PR pybind#5332 — Fix handling of const unique_ptr<T, D> & (do not disown). Splitting out so that the functional changes under PR pybind#5332 will be more obvious. The only functional change under this PR is that ``` assert(custom_deleter_ptr != nullptr); ``` is replaced with: ``` if (custom_deleter_ptr == nullptr) { throw std::runtime_error( std::string("smart_holder::extract_deleter() precondition failure (") + context + ")."); } ```
rwgk
pushed a commit
that referenced
this pull request
Aug 25, 2024
rwgk
pushed a commit
to rwgk/pybind11
that referenced
this pull request
Aug 25, 2024
PREPARATION for: PR pybind#5332 — Fix handling of const unique_ptr<T, D> & (do not disown). Splitting out so that the functional changes under PR pybind#5332 will be more obvious. The only functional change under this PR is that ``` assert(custom_deleter_ptr != nullptr); ``` is replaced with: ``` if (custom_deleter_ptr == nullptr) { throw std::runtime_error( std::string("smart_holder::extract_deleter() precondition failure (") + context + ")."); } ```
rwgk
pushed a commit
that referenced
this pull request
Aug 25, 2024
PREPARATION for: PR #5332 — Fix handling of const unique_ptr<T, D> & (do not disown). Splitting out so that the functional changes under PR #5332 will be more obvious. The only functional change under this PR is that ``` assert(custom_deleter_ptr != nullptr); ``` is replaced with: ``` if (custom_deleter_ptr == nullptr) { throw std::runtime_error( std::string("smart_holder::extract_deleter() precondition failure (") + context + ")."); } ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Before this PR, passing a Python object as a C++
const unique_ptr<T, D> &
involved building a temporary (non-const)unique_ptr<T, D>
, which was then passed as aconst unique_ptr<T, D> &
. The Python object was disowned when the temporaryunique_ptr<T, D>
was built. This was unintentional and counter-intuitive (although bug-compatible with PyCLIF; probably also unintentional).This PR introduces a different mechanism, specifically for
const unique_ptr<T, D> &
: a temporary (still non-const)unique_ptr<T, D>
is built, but the Python object is not disowned, and the temporaryunique_ptr
is meant to never expire (this would only be possible if the passed-to C++ code applied aconst_cast
, to then disown the non-constunique_ptr
). When thetype_caster
goes out of scope, the new implementation checks if the temporaryunique_ptr
did in fact not expire (otherwise aFATAL
exception is thrown), then.release()
s it before deleting theunique_ptr
object. Conveniently, this can be achieved by managing the temporaryunique_ptr
with ashared_ptr
and a custom deleter.I (rwgk) strongly considered the alternative of using a
static_assert()
to make it impossible to pass a Python object as aconst unique_ptr<T, D> &
, but the new implementation is sufficiently small, it's less trouble than backing out all existing test code that would break. There could also be existing client code in the wild that may need workarounds. Having the revised feature is convenient and not very costly (amount of code; complexity).@rhaschke FYI
Suggested changelog entry: