Skip to content

Commit 13b5d96

Browse files
committed
Convert LazyTypeObject to use the Bound API
1 parent c33d330 commit 13b5d96

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

guide/src/class.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,8 @@ unsafe impl pyo3::type_object::PyTypeInfo for MyClass {
12391239
#[inline]
12401240
fn type_object_raw(py: pyo3::Python<'_>) -> *mut pyo3::ffi::PyTypeObject {
12411241
<Self as pyo3::impl_::pyclass::PyClassImpl>::lazy_type_object()
1242-
.get_or_init(py)
1243-
.as_type_ptr()
1242+
.get_or_init_bound(py)
1243+
.as_ptr() as *mut pyo3::ffi::PyTypeObject
12441244
}
12451245
}
12461246

pyo3-macros-backend/src/pyclass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,8 +1286,8 @@ fn impl_pytypeinfo(
12861286
#deprecations
12871287

12881288
<#cls as _pyo3::impl_::pyclass::PyClassImpl>::lazy_type_object()
1289-
.get_or_init(py)
1290-
.as_type_ptr()
1289+
.get_or_init_bound(py)
1290+
.as_ptr() as *mut _pyo3::ffi::PyTypeObject
12911291
}
12921292
}
12931293
}

src/impl_/pyclass/lazy_type_object.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
pyclass::{create_type_object, PyClassTypeObject},
1313
sync::{GILOnceCell, GILProtected},
1414
types::PyType,
15-
PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
15+
Bound, PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
1616
};
1717

1818
use super::PyClassItemsIter;
@@ -46,15 +46,30 @@ impl<T> LazyTypeObject<T> {
4646

4747
impl<T: PyClass> LazyTypeObject<T> {
4848
/// Gets the type object contained by this `LazyTypeObject`, initializing it if needed.
49+
#[cfg_attr(
50+
not(feature = "gil-refs"),
51+
deprecated(
52+
since = "0.21.0",
53+
note = "`LazyTypeObject::get_or_init` will be replaced by `LazyTypeObject::get_or_init_bound` in a future PyO3 version"
54+
)
55+
)]
4956
pub fn get_or_init<'py>(&'py self, py: Python<'py>) -> &'py PyType {
57+
self.get_or_init_bound(py).as_gil_ref()
58+
}
59+
60+
/// Gets the type object contained by this `LazyTypeObject`, initializing it if needed.
61+
pub fn get_or_init_bound<'py>(&'py self, py: Python<'py>) -> &Bound<'py, PyType> {
5062
self.get_or_try_init(py).unwrap_or_else(|err| {
5163
err.print(py);
5264
panic!("failed to create type object for {}", T::NAME)
5365
})
5466
}
5567

5668
/// Fallible version of the above.
57-
pub(crate) fn get_or_try_init<'py>(&'py self, py: Python<'py>) -> PyResult<&'py PyType> {
69+
pub(crate) fn get_or_try_init<'py>(
70+
&'py self,
71+
py: Python<'py>,
72+
) -> PyResult<&Bound<'py, PyType>> {
5873
self.0
5974
.get_or_try_init(py, create_type_object::<T>, T::NAME, T::items_iter())
6075
}
@@ -70,13 +85,13 @@ impl LazyTypeObjectInner {
7085
init: fn(Python<'py>) -> PyResult<PyClassTypeObject>,
7186
name: &str,
7287
items_iter: PyClassItemsIter,
73-
) -> PyResult<&'py PyType> {
88+
) -> PyResult<&Bound<'py, PyType>> {
7489
(|| -> PyResult<_> {
7590
let type_object = self
7691
.value
7792
.get_or_try_init(py, || init(py))?
7893
.type_object
79-
.as_ref(py);
94+
.bind(py);
8095
self.ensure_init(type_object, name, items_iter)?;
8196
Ok(type_object)
8297
})()
@@ -91,7 +106,7 @@ impl LazyTypeObjectInner {
91106

92107
fn ensure_init(
93108
&self,
94-
type_object: &PyType,
109+
type_object: &Bound<'_, PyType>,
95110
name: &str,
96111
items_iter: PyClassItemsIter,
97112
) -> PyResult<()> {

0 commit comments

Comments
 (0)