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

[WIP] Fix complex enum Py::new vs .into_py inconsistency #3752

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,19 @@ fn impl_complex_enum(
}
};

let enum_pyinitializable_impl = quote! {
impl _pyo3::PyInitializable for #cls {
fn initialize(py: Python<'_>, value: impl Into<PyClassInitializer<Self>>) -> PyResult<Py<Self>> {
todo!()
}
}
};

let pyclass_impls: TokenStream = vec![
impl_builder.impl_pyclass(),
impl_builder.impl_extractext(),
enum_into_py_impl,
enum_pyinitializable_impl,
impl_builder.impl_pyclassimpl()?,
impl_builder.impl_freelist(),
]
Expand Down Expand Up @@ -1336,6 +1345,7 @@ impl<'a> PyClassImplsBuilder<'a> {
let tokens = vec![
self.impl_pyclass(),
self.impl_extractext(),
self.impl_pyinitializable(),
self.impl_into_py(),
self.impl_pyclassimpl()?,
self.impl_freelist(),
Expand Down Expand Up @@ -1399,6 +1409,13 @@ impl<'a> PyClassImplsBuilder<'a> {
}
}

fn impl_pyinitializable(&self) -> TokenStream {
let cls = self.cls;
quote! {
impl _pyo3::PyInitializable for #cls {}
}
}

fn impl_into_py(&self) -> TokenStream {
let cls = self.cls;
let attr = self.attr;
Expand Down
16 changes: 15 additions & 1 deletion src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
pycell::PyCellLayout,
pyclass_init::PyObjectInit,
types::PyBool,
Py, PyAny, PyCell, PyClass, PyErr, PyMethodDefType, PyNativeType, PyResult, PyTypeInfo, Python,
Py, PyAny, PyCell, PyClass, PyClassInitializer, PyErr, PyMethodDefType, PyNativeType, PyResult,
PyTypeInfo, Python,
};
use std::{
borrow::Cow,
Expand Down Expand Up @@ -136,6 +137,19 @@ pub struct PyClassItems {
// Allow PyClassItems in statics
unsafe impl Sync for PyClassItems {}

/// Allows customizing how a Py<T> is created from a value, while also providing a default implementation.
pub trait PyInitializable: PyClass {
fn initialize(
py: Python<'_>,
value: impl Into<PyClassInitializer<Self>>,
) -> PyResult<Py<Self>> {
let initializer: PyClassInitializer<Self> = value.into();
let obj = initializer.create_cell(py)?;
let ob = unsafe { Py::from_owned_ptr(py, obj as _) };
Ok(ob)
}
}

/// Implements the underlying functionality of `#[pyclass]`, assembled by various proc macros.
///
/// Users are discouraged from implementing this trait manually; it is a PyO3 implementation detail
Expand Down
11 changes: 4 additions & 7 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::types::any::PyAnyMethods;
use crate::types::string::PyStringMethods;
use crate::types::{PyDict, PyString, PyTuple};
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, PyRef, PyRefMut,
PyTypeInfo, Python, ToPyObject,
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyClass, PyClassInitializer, PyInitializable,
PyRef, PyRefMut, PyTypeInfo, Python, ToPyObject,
};
use crate::{gil, PyTypeCheck};
use std::marker::PhantomData;
Expand Down Expand Up @@ -548,7 +548,7 @@ unsafe impl<T> Sync for Py<T> {}

impl<T> Py<T>
where
T: PyClass,
T: PyInitializable,
{
/// Creates a new instance `Py<T>` of a `#[pyclass]` on the Python heap.
///
Expand All @@ -569,10 +569,7 @@ where
/// # }
/// ```
pub fn new(py: Python<'_>, value: impl Into<PyClassInitializer<T>>) -> PyResult<Py<T>> {
let initializer = value.into();
let obj = initializer.create_cell(py)?;
let ob = unsafe { Py::from_owned_ptr(py, obj as _) };
Ok(ob)
<T as PyInitializable>::initialize(py, value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub use crate::gil::{prepare_freethreaded_python, with_embedded_python_interpret
pub use crate::instance::{Borrowed, Bound, Py, PyNativeType, PyObject};
pub use crate::marker::Python;
pub use crate::pycell::{PyCell, PyRef, PyRefMut};
pub use crate::pyclass::PyClass;
pub use crate::pyclass::{PyClass, PyInitializable};
pub use crate::pyclass_init::PyClassInitializer;
pub use crate::type_object::{PyTypeCheck, PyTypeInfo};
pub use crate::types::PyAny;
Expand Down
1 change: 1 addition & 0 deletions src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod gc;

pub(crate) use self::create_type_object::{create_type_object, PyClassTypeObject};
pub use self::gc::{PyTraverseError, PyVisit};
pub use crate::impl_::pyclass::PyInitializable;

/// Types that can be used as Python classes.
///
Expand Down
15 changes: 15 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,18 @@ fn test_renaming_all_enum_variants() {
);
});
}

#[test]
fn test_complex_enum_py_new_into_py() {
#[pyclass]
enum MyEnum {
Variant { i: i32 },
}

Python::with_gil(|py| {
let x = Py::new(py, MyEnum::Variant { i: 42 }).unwrap();
let cls = py.get_type::<MyEnum>();
py_assert!(py, x cls, "isinstance(x, cls)");
py_assert!(py, x cls, "isinstance(x, cls.Variant)");
});
}
Loading