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

Convert LazyTypeObject to use the Bound API #3855

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ fn impl_pytypeinfo(

#[inline]
fn type_object_raw(py: _pyo3::Python<'_>) -> *mut _pyo3::ffi::PyTypeObject {
use _pyo3::prelude::PyTypeMethods;
#deprecations

<#cls as _pyo3::impl_::pyclass::PyClassImpl>::lazy_type_object()
Expand Down
14 changes: 7 additions & 7 deletions src/impl_/pyclass/lazy_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
pyclass::{create_type_object, PyClassTypeObject},
sync::{GILOnceCell, GILProtected},
types::PyType,
PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
Bound, PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
};

use super::PyClassItemsIter;
Expand Down Expand Up @@ -46,15 +46,15 @@ impl<T> LazyTypeObject<T> {

impl<T: PyClass> LazyTypeObject<T> {
/// Gets the type object contained by this `LazyTypeObject`, initializing it if needed.
pub fn get_or_init<'py>(&'py self, py: Python<'py>) -> &'py PyType {
pub fn get_or_init<'py>(&self, py: Python<'py>) -> &Bound<'py, PyType> {
self.get_or_try_init(py).unwrap_or_else(|err| {
err.print(py);
panic!("failed to create type object for {}", T::NAME)
})
}

/// Fallible version of the above.
pub(crate) fn get_or_try_init<'py>(&'py self, py: Python<'py>) -> PyResult<&'py PyType> {
pub(crate) fn get_or_try_init<'py>(&self, py: Python<'py>) -> PyResult<&Bound<'py, PyType>> {
self.0
.get_or_try_init(py, create_type_object::<T>, T::NAME, T::items_iter())
}
Expand All @@ -65,18 +65,18 @@ impl LazyTypeObjectInner {
// so that this code is only instantiated once, instead of for every T
// like the generic LazyTypeObject<T> methods above.
fn get_or_try_init<'py>(
&'py self,
&self,
py: Python<'py>,
init: fn(Python<'py>) -> PyResult<PyClassTypeObject>,
name: &str,
items_iter: PyClassItemsIter,
) -> PyResult<&'py PyType> {
) -> PyResult<&Bound<'py, PyType>> {
(|| -> PyResult<_> {
let type_object = self
.value
.get_or_try_init(py, || init(py))?
.type_object
.as_ref(py);
.bind(py);
self.ensure_init(type_object, name, items_iter)?;
Ok(type_object)
})()
Expand All @@ -91,7 +91,7 @@ impl LazyTypeObjectInner {

fn ensure_init(
&self,
type_object: &PyType,
type_object: &Bound<'_, PyType>,
name: &str,
items_iter: PyClassItemsIter,
) -> PyResult<()> {
Expand Down
Loading