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

deprecate from_borrowed_ptr methods #3915

Merged
merged 2 commits into from
Mar 1, 2024

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Feb 28, 2024

Part of #3684

This deprecates the remaining from_borrowed_ptr methods on Python and FromPyPointer, as well as that trait itself (since all methods are deprecated now)

Comment on lines 437 to 438
fn set(mut self_: PyRefMut<'_, Self>, key: String, value: &PyAny) -> PyResult<()> {
self_.counter.entry(key.clone()).or_insert(0);
let py = self_.py();
# #[allow(deprecated)]
let dict: &PyDict = unsafe { py.from_borrowed_ptr_or_err(self_.as_ptr())? };
dict.set_item(key, value)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updates this example in #3914

not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think technically the equivalent method would be on Borrowed, but I thought we might want to nudge user here to the safer option.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed I think Borrowed::from_ptr is hard to get right, as we keep finding by not using it in our own macro code!

@Icxolu Icxolu added the CI-skip-changelog Skip checking changelog entry label Feb 28, 2024
@@ -513,15 +513,15 @@ impl<'a> FnSpec<'a> {
holders.pop().unwrap(); // does not actually use holder created by `self_arg`

quote! {{
let __guard = _pyo3::impl_::coroutine::RefGuard::<#cls>::new(py.from_borrowed_ptr::<_pyo3::types::PyAny>(_slf))?;
let __guard = _pyo3::impl_::coroutine::RefGuard::<#cls>::new(_pyo3::Bound::from_borrowed_ptr(py, _slf))?;
Copy link
Contributor Author

@Icxolu Icxolu Feb 29, 2024

Choose a reason for hiding this comment

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

Today I had the thought that we could also use BoundRef here, to defer the ref count bump until after the downcast check in RefGuard::new. This would prevent touching the ref count in the error case.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks yet again, and sorry for the slower review here. I think I've spotted something (not really part of this PR) which I just want to check in the morning, I'll complete this review first thing too.

Comment on lines 43 to 44
let owned = obj.downcast::<T>()?;
mem::forget(owned.try_borrow()?);
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, now that I see these men::forget calls I suspect that the changes I made to make PyRef and PyRefMut contain Bound mean that we're leaking a reference count here.

Probably not related to this PR. I'll pause reviewing here just so I remember to reread this in the morning.

Copy link
Member

Choose a reason for hiding this comment

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

#3917 can fixup later.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks again, after separately resolving my worry about the RefGuard[Mut] leak issue this looks great!

not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
Copy link
Member

Choose a reason for hiding this comment

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

Agreed I think Borrowed::from_ptr is hard to get right, as we keep finding by not using it in our own macro code!

@davidhewitt
Copy link
Member

Ah, there's a merge conflict now :)

@Icxolu Icxolu force-pushed the deprecate-from-borrowed-ptrs branch from 3c532d1 to f315b7d Compare March 1, 2024 18:38
@Icxolu
Copy link
Contributor Author

Icxolu commented Mar 1, 2024

Rebased 😇

Edit: Seems like some autoformatter messed with the markdown on resolving the conflict, I'll remove that

This deprecates the methods on the `Python`
marker, aswell as `FromPyPointer`
@Icxolu Icxolu force-pushed the deprecate-from-borrowed-ptrs branch from f315b7d to 6ff042b Compare March 1, 2024 18:46
@davidhewitt davidhewitt added this pull request to the merge queue Mar 1, 2024
Merged via the queue into PyO3:main with commit 1c5265e Mar 1, 2024
41 of 42 checks passed
@Icxolu Icxolu deleted the deprecate-from-borrowed-ptrs branch March 1, 2024 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants