change Py
constructors from pointer to produce Py<PyAny>
#5495
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.
The constructors such as
Py::from_owned_ptr
are implemented onPy<T>
. This is different toBound
andBorrowed
where the implementation is only onBound<'py, PyAny>
andBorrowed<'a, 'py, PyAny>
.I would argue that implementing only on the
PyAny
type is better because it forces the caller to decide what casting logic to do - e.g..cast()
,.cast_exact()
, or.cast_unchecked()
. ThePy<T>
constructors basically all do.cast_unchecked()
.Implementing this diff I found places in the codebase where the unchecked cast was probably not good (return value of
PyNumber_Index
), and I'd bet user code more likely makes the same mistake.If we like moving forward with this, I think a better path than actually changing the constructors immediately would be to introduce new temporary names (
Py::any_from_owned_ptr
etc. maybe?) and deprecate the existing, and rename the new forms once the deprecated ones are removed.We might want to also introduce proper
.cast()
methods toPy<T>
similar toBound::cast
etc, to make the handling of thePy<PyAny>
return values easier.