Skip to content
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

Py::as_ref and Py::into_ref (remove AsPyRef) #1098

Merged
merged 3 commits into from Aug 11, 2020

Conversation

davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Aug 9, 2020

I introduce Py::as_ref and Py::into_ref. We spoke about Py::into_ref in #1041.

I moved AsPyRef::as_ref to Py::as_ref because I think it's easier to document and for users to discover. Also means one more trait can be removed!

Closes #1041

Copy link
Member

@kngwyu kngwyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 💯

guide/src/migration.md Outdated Show resolved Hide resolved
@birkenfeld
Copy link
Member

I'm confused by into_ref. In what cases do you need to / should you use this instead of using as_ref and letting the Py be dropped naturally?

guide/src/types.md Outdated Show resolved Hide resolved
src/instance.rs Outdated Show resolved Hide resolved
src/instance.rs Outdated Show resolved Hide resolved
src/instance.rs Outdated Show resolved Hide resolved
@davidhewitt
Copy link
Member Author

I'm confused by into_ref. In what cases do you need to / should you use this instead of using as_ref and letting the Py be dropped naturally?

Great feedback. We've seen in rare cases APIs want to return a reference but have a Py value they have created (e.g. from into_py()). This has come up in #1041 as well as #1083.

I'll rework documentation later based on the comments here.

@birkenfeld
Copy link
Member

Ah, good point. I did not realize that as_ref had the self lifetime and the Python lifetime tied to the return's lifetime. (I'm sure there is a reason this is necessary?)

@davidhewitt
Copy link
Member Author

Yep - the returned reference for as_ref needs to be the shorter of the two lifetimes, which is why they're both set the same.

(It cannot outlive &self because that's the data it's borrowing, and it cannot outlive Python because it's GIL-bound.)

@davidhewitt davidhewitt force-pushed the py-as-into-ref branch 2 times, most recently from afcc149 to 22edfff Compare August 10, 2020 20:28
@davidhewitt
Copy link
Member Author

Thanks for the feedback; I've pushed the suggested improvements to the docs.

src/instance.rs Outdated Show resolved Hide resolved
Co-authored-by: Georg Brandl <georg@python.org>
@davidhewitt davidhewitt merged commit 565e36d into PyO3:master Aug 11, 2020
@davidhewitt davidhewitt deleted the py-as-into-ref branch December 24, 2021 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PyObject -> &'py PyAny
3 participants