Skip to content

Commit 09e3180

Browse files
committed
from_type_ptr_bound instead of from_type_ptr_borrowed
1 parent d07256e commit 09e3180

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/types/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
21082108
}
21092109

21102110
fn get_type(&self) -> Bound<'py, PyType> {
2111-
unsafe { PyType::from_type_ptr_borrowed(self.py(), ffi::Py_TYPE(self.as_ptr())) }.to_owned()
2111+
unsafe { PyType::from_type_ptr_bound(self.py(), &ffi::Py_TYPE(self.as_ptr())) }.to_owned()
21122112
}
21132113

21142114
#[inline]

src/types/typeobject.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,36 @@ impl PyType {
3939
self.as_borrowed().as_type_ptr()
4040
}
4141

42-
/// Deprecated form of [`PyType::from_type_ptr_borrowed`].
42+
/// Deprecated form of [`PyType::from_type_ptr_bound`].
4343
///
4444
/// # Safety
4545
///
46-
/// See [`PyType::from_type_ptr_borrowed`].
46+
/// - The pointer must a valid non-null reference to a `PyTypeObject`.
4747
#[inline]
4848
#[cfg_attr(
4949
not(feature = "gil-refs"),
5050
deprecated(
5151
since = "0.21.0",
52-
note = "`from_type_ptr` will be replaced by `from_type_ptr_borrowed` in a future PyO3 version"
52+
note = "`from_type_ptr` will be replaced by `from_type_ptr_bound` in a future PyO3 version"
5353
)
5454
)]
5555
pub unsafe fn from_type_ptr(py: Python<'_>, p: *mut ffi::PyTypeObject) -> &PyType {
56-
Self::from_type_ptr_borrowed(py, p).into_gil_ref()
56+
Self::from_type_ptr_bound(py, &p).to_owned().into_gil_ref()
5757
}
5858

59-
/// Retrieves the `PyType` instance for the given FFI pointer.
59+
/// Converts the given FFI pointer into `&Bound<PyType>`, to use in safe code.
6060
///
6161
/// # Safety
6262
/// - The pointer must be non-null.
6363
/// - The pointer must be valid for the entire of the lifetime 'a for which the reference is used,
6464
/// as with `std::slice::from_raw_parts`.
6565
#[inline]
66-
pub unsafe fn from_type_ptr_borrowed<'a>(
67-
py: Python<'_>,
68-
p: *mut ffi::PyTypeObject,
69-
) -> Borrowed<'a, '_, PyType> {
70-
(p as *mut ffi::PyObject)
71-
.assume_borrowed_unchecked(py)
72-
.downcast_unchecked()
66+
pub unsafe fn from_type_ptr_bound<'a, 'py>(
67+
py: Python<'py>,
68+
p: &'a *mut ffi::PyTypeObject,
69+
) -> &'a Bound<'py, PyType> {
70+
let object_ptr = &*(p as *const *mut ffi::PyTypeObject as *const *mut ffi::PyObject);
71+
Bound::ref_from_ptr(py, object_ptr).downcast_unchecked()
7372
}
7473

7574
/// Gets the [qualified name](https://docs.python.org/3/glossary.html#term-qualified-name) of the `PyType`.

0 commit comments

Comments
 (0)