-
Notifications
You must be signed in to change notification settings - Fork 970
fix type confusion when different #[pyclass] types returned from #[new]
#6062
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c456dcf
fix type confusion when different `#[pyclass]` types returned from `#…
davidhewitt 4d39ec5
fixup
davidhewitt d9354df
newsfragment
davidhewitt 66a7ac9
don't quote `unsafe` at user code
davidhewitt 1160174
review feedback
davidhewitt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix type confusion when returning a `#[pyclass]` from a different pyclass' `#[new]` method. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,8 @@ use crate::{ | |
| use crate::{quotes, utils}; | ||
| use proc_macro2::{Span, TokenStream}; | ||
| use quote::{format_ident, quote, quote_spanned, ToTokens}; | ||
| use syn::LitCStr; | ||
| use syn::{ext::IdentExt, spanned::Spanned, Field, Ident, Result}; | ||
| use syn::{parse_quote, LitCStr}; | ||
|
|
||
| /// Generated code for a single pymethod item. | ||
| pub struct MethodAndMethodDef { | ||
|
|
@@ -1473,25 +1473,25 @@ fn generate_method_body( | |
| } | ||
| }); | ||
|
|
||
| let output = if let syn::ReturnType::Type(_, ty) = &spec.output { | ||
| let mut ty = ty.clone(); | ||
| utils::elide_lifetimes(&mut ty); | ||
| ty | ||
| } else { | ||
| parse_quote!(()) | ||
| let py = syn::Ident::new("py", Span::call_site()); | ||
| let initializer = syn::Ident::new("initializer", Span::call_site()); | ||
| let slf = syn::Ident::new("_slf", Span::call_site()); | ||
|
|
||
| // Having just this call emitted at the span of the return value helps surface errors | ||
| // if the user passed an invalid return type. | ||
| let conversion = quote_spanned! { *output_span => | ||
| #pyo3_path::impl_::pymethods::tp_new_impl::<_, #cls>(#py, #initializer, #slf) | ||
| }; | ||
|
|
||
| let body = quote! { | ||
| #text_signature_impl | ||
|
|
||
| use #pyo3_path::impl_::pyclass::Probe as _; | ||
| #warnings | ||
| #arg_convert | ||
|
|
||
| let result = #call; | ||
| #pyo3_path::impl_::pymethods::tp_new_impl::< | ||
| _, | ||
| { #pyo3_path::impl_::pyclass::IsPyClass::<#output>::VALUE }, | ||
| { #pyo3_path::impl_::pyclass::IsInitializerTuple::<#output>::VALUE } | ||
|
Comment on lines
-1492
to
-1493
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the record, the original bug was that these specializations apply for all pyclasses, not just the |
||
| >(py, result, _slf) | ||
| let value = #pyo3_path::impl_::wrap::OkWrapper::new(&result).ok_wrap(result)?; | ||
| let #initializer = #pyo3_path::impl_::pymethods::tp_new_resolver::<#cls, _>(&value).resolve(value); | ||
| unsafe { #conversion } | ||
| }; | ||
| (arg_idents, arg_types, body) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,172 +1,2 @@ | ||
| //! Contains initialization utilities for `#[pyclass]`. | ||
| use crate::exceptions::PyTypeError; | ||
| use crate::ffi_ptr_ext::FfiPtrExt; | ||
| use crate::impl_::pyclass::PyClassBaseType; | ||
| use crate::internal::get_slot::TP_NEW; | ||
| use crate::types::{PyTuple, PyType}; | ||
| use crate::{ffi, PyClass, PyClassInitializer, PyErr, PyResult, Python}; | ||
| use crate::{ffi::PyTypeObject, sealed::Sealed, type_object::PyTypeInfo}; | ||
| use core::marker::PhantomData; | ||
|
|
||
| /// Initializer for Python types. | ||
| /// | ||
| /// This trait is intended to use internally for distinguishing `#[pyclass]` and | ||
| /// Python native types. | ||
| pub trait PyObjectInit<T>: Sized + Sealed { | ||
| /// # Safety | ||
| /// - `subtype` must be a valid pointer to a type object of T or a subclass. | ||
| unsafe fn into_new_object( | ||
| self, | ||
| py: Python<'_>, | ||
| subtype: *mut PyTypeObject, | ||
| ) -> PyResult<*mut ffi::PyObject>; | ||
| } | ||
|
|
||
| /// Initializer for Python native types, like `PyDict`. | ||
| pub struct PyNativeTypeInitializer<T: PyTypeInfo>(pub PhantomData<T>); | ||
|
|
||
| impl<T: PyTypeInfo> PyObjectInit<T> for PyNativeTypeInitializer<T> { | ||
| unsafe fn into_new_object( | ||
| self, | ||
| py: Python<'_>, | ||
| subtype: *mut PyTypeObject, | ||
| ) -> PyResult<*mut ffi::PyObject> { | ||
| unsafe fn inner( | ||
| py: Python<'_>, | ||
| type_ptr: *mut PyTypeObject, | ||
| subtype: *mut PyTypeObject, | ||
| ) -> PyResult<*mut ffi::PyObject> { | ||
| let tp_new = unsafe { | ||
| type_ptr | ||
| .cast::<ffi::PyObject>() | ||
| .assume_borrowed_unchecked(py) | ||
| .cast_unchecked::<PyType>() | ||
| .get_slot(TP_NEW) | ||
| .ok_or_else(|| PyTypeError::new_err("base type without tp_new"))? | ||
| }; | ||
|
|
||
| // TODO: make it possible to provide real arguments to the base tp_new | ||
| let obj = | ||
| unsafe { tp_new(subtype, PyTuple::empty(py).as_ptr(), core::ptr::null_mut()) }; | ||
| if obj.is_null() { | ||
| Err(PyErr::fetch(py)) | ||
| } else { | ||
| Ok(obj) | ||
| } | ||
| } | ||
| unsafe { inner(py, T::type_object_raw(py), subtype) } | ||
| } | ||
| } | ||
|
|
||
| pub trait PyClassInit<'py, const IS_PYCLASS: bool, const IS_INITIALIZER_TUPLE: bool>: | ||
| seal_pyclass_init::Sealed<'py, IS_PYCLASS, IS_INITIALIZER_TUPLE> | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>>; | ||
| } | ||
|
|
||
| mod seal_pyclass_init { | ||
| use crate::impl_::pyclass::{self, PyClassBaseType}; | ||
| use crate::impl_::pyclass_init::{PyClassInit, PyNativeTypeInitializer}; | ||
| use crate::{PyClass, PyClassInitializer, PyErr}; | ||
|
|
||
| pub trait Sealed<'py, const IS_PYCLASS: bool, const IS_INITIALIZER_TUPLE: bool> {} | ||
|
|
||
| impl<'py, T> Sealed<'py, false, false> for T where T: crate::IntoPyObject<'py> {} | ||
| impl<'py, T> Sealed<'py, true, false> for T | ||
| where | ||
| T: crate::PyClass, | ||
| T::BaseType: pyclass::PyClassBaseType<Initializer = PyNativeTypeInitializer<T::BaseType>>, | ||
| { | ||
| } | ||
| impl<'py, T, E, const IS_PYCLASS: bool, const IS_INITIALIZER_TUPLE: bool> | ||
| Sealed<'py, IS_PYCLASS, IS_INITIALIZER_TUPLE> for Result<T, E> | ||
| where | ||
| T: PyClassInit<'py, IS_PYCLASS, IS_INITIALIZER_TUPLE>, | ||
| E: Into<PyErr>, | ||
| { | ||
| } | ||
| impl<'py, T> Sealed<'py, false, false> for PyClassInitializer<T> where T: PyClass {} | ||
| impl<'py, S, B> Sealed<'py, false, true> for (S, B) | ||
| where | ||
| S: PyClass<BaseType = B>, | ||
| B: PyClass + PyClassBaseType<Initializer = PyClassInitializer<B>>, | ||
| B::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<B::BaseType>>, | ||
| { | ||
| } | ||
| } | ||
|
|
||
| impl<'py, T> PyClassInit<'py, false, false> for T | ||
| where | ||
| T: crate::IntoPyObject<'py>, | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>> { | ||
| self.into_pyobject(cls.py()) | ||
| .map(crate::BoundObject::into_any) | ||
| .map(crate::BoundObject::into_bound) | ||
| .map_err(Into::into) | ||
| } | ||
| } | ||
|
|
||
| impl<'py, T> PyClassInit<'py, true, false> for T | ||
| where | ||
| T: crate::PyClass, | ||
| T::BaseType: | ||
| super::pyclass::PyClassBaseType<Initializer = PyNativeTypeInitializer<T::BaseType>>, | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>> { | ||
| PyClassInitializer::from(self).init(cls) | ||
| } | ||
| } | ||
|
|
||
| impl<'py, T, E, const IS_PYCLASS: bool, const IS_INITIALIZER_TUPLE: bool> | ||
| PyClassInit<'py, IS_PYCLASS, IS_INITIALIZER_TUPLE> for Result<T, E> | ||
| where | ||
| T: PyClassInit<'py, IS_PYCLASS, IS_INITIALIZER_TUPLE>, | ||
| E: Into<PyErr>, | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>> { | ||
| self.map_err(Into::into)?.init(cls) | ||
| } | ||
| } | ||
|
|
||
| impl<'py, T> PyClassInit<'py, false, false> for PyClassInitializer<T> | ||
| where | ||
| T: PyClass, | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>> { | ||
| unsafe { | ||
| self.create_class_object_of_type(cls.py(), cls.as_ptr().cast()) | ||
| .map(crate::Bound::into_any) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'py, S, B> PyClassInit<'py, false, true> for (S, B) | ||
| where | ||
| S: PyClass<BaseType = B>, | ||
| B: PyClass + PyClassBaseType<Initializer = PyClassInitializer<B>>, | ||
| B::BaseType: PyClassBaseType<Initializer = PyNativeTypeInitializer<B::BaseType>>, | ||
| { | ||
| fn init( | ||
| self, | ||
| cls: crate::Borrowed<'_, 'py, crate::types::PyType>, | ||
| ) -> PyResult<crate::Bound<'py, crate::PyAny>> { | ||
| let (sub, base) = self; | ||
| PyClassInitializer::from(base).add_subclass(sub).init(cls) | ||
| } | ||
| } | ||
| /// Re-exported so that macros can name this internal type. | ||
| pub use crate::internal::pyclass_init::PyNativeTypeInitializer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| //! Holding place for code which is not intended to be reachable from outside of PyO3. | ||
|
|
||
| pub(crate) mod get_slot; | ||
| pub(crate) mod pyclass_init; | ||
| pub(crate) mod state; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.