Fix py::cast from pytype rvalue to pytype #3949
Merged
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.
Previously, py::cast blindly assumed that the destination type was a C++ type rather than a python type when the source type was an rvalue.
Description
The following piece of code:
... results in the following bizarre runtime error:
Whereas this works fine:
The problem seems to be that in the first case, the
py::float_
is an rvalue, which causes pybind to use a different implementation ofpy::cast
which blindly assumes that the destination type is always a C++ type and attempts to use thetype_caster
mechanism - unlike the normalpy::cast
implementation which checks the destination type to determine whether to usetype_caster
or just call the constructor of the destination type.This commit does three things:
load_type
such that if it gets invoked with a python type rather than a C++ type, it will produce a meaningful compile-time error rather than the bizarre runtime error shown above.py::cast(object &&)
such that it actually checks whether the destination type is a python object, and if so, invokes the move constructor instead of usingtype_caster
.Suggested changelog entry: