-
Notifications
You must be signed in to change notification settings - Fork 997
FIx method resolution in number slots when a single method is a subclass #6228
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
Open
MatthieuDartiailh
wants to merge
7
commits into
PyO3:main
Choose a base branch
from
MatthieuDartiailh:binary-op-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
775dded
rework binop slot dispatching to be type based and dispatch to parent…
MatthieuDartiailh 8dc3388
tests: add tests demonstrating proper resolution of add/radd when sub…
MatthieuDartiailh 9f19128
add newfragment
MatthieuDartiailh d84ec67
use SelfConversionPolicy::trusted now that we do not call blindly fra…
MatthieuDartiailh 205d1b9
add safety comment
MatthieuDartiailh dbcd614
fix macro namespacing
MatthieuDartiailh 268e604
do not return early if method lookup fails on LHS since RHS may work
MatthieuDartiailh 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 reversible arithmetic operators don't respect subclassing if only one arm implemented |
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ use core::{ | |
| ffi::{c_int, c_void}, | ||
| marker::PhantomData, | ||
| ptr::{self, NonNull}, | ||
| result::Result, | ||
| }; | ||
| use std::{sync::Mutex, thread}; | ||
|
|
||
|
|
@@ -517,7 +518,6 @@ define_pyclass_setattr_slot! { | |
| objobjargproc, | ||
| } | ||
|
|
||
| /// Macro which expands to three items | ||
| /// - Trait for a lhs dunder e.g. __add__ | ||
| /// - Trait for the corresponding rhs e.g. __radd__ | ||
| /// - A macro which will use dtolnay specialisation to generate the shared slot for the two dunders | ||
|
|
@@ -563,21 +563,129 @@ macro_rules! define_pyclass_binary_operator_slot { | |
| #[doc(hidden)] | ||
| #[macro_export] | ||
| macro_rules! $generate_macro { | ||
| ($cls:ty) => {{ | ||
| // Both forward and reflected available | ||
| ($cls:ty, true, true) => {{ | ||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| let lhs_result = unsafe { collector.$lhs(py, _slf, _other) }?; | ||
| if lhs_result == unsafe { $crate::ffi::Py_NotImplemented() } { | ||
| unsafe { $crate::ffi::Py_DECREF(lhs_result) }; | ||
| unsafe { collector.$rhs(py, _other, _slf) } | ||
| } else { | ||
| ::core::result::Result::Ok(lhs_result) | ||
|
|
||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| return unsafe { collector.$lhs(py, _slf, _other) }; | ||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| if rhs_obj.is_instance_of::<$cls>() { | ||
| return unsafe { collector.$rhs(py, _other, _slf) }; | ||
| } | ||
|
|
||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
| slot: $crate::ffi::$slot, | ||
| pfunc: $crate::impl_::trampoline::get_trampoline_function!( | ||
| binaryfunc, slot_impl | ||
| ) as $crate::ffi::binaryfunc as _, | ||
| } | ||
| }}; | ||
|
|
||
| // Only forward available | ||
| ($cls:ty, true, false) => {{ | ||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
|
|
||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| return unsafe { collector.$lhs(py, _slf, _other) }; | ||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| if rhs_obj.is_instance_of::<$cls>() { | ||
| // RHS is our class, try parent's reflected method via super() | ||
| // This ensures parent knows it's being called for reflection | ||
| let py_type = <$cls as $crate::PyTypeInfo>::type_object(py); | ||
| let reflected_name = $crate::intern!(py, concat!(stringify!($rhs))); | ||
| let super_obj = match $crate::types::PySuper::new(&py_type, &rhs_obj) { | ||
| ::core::result::Result::Ok(s) => s, | ||
| ::core::result::Result::Err(e) => return ::core::result::Result::Err(e), | ||
| }; | ||
| match super_obj.call_method1(reflected_name, (&lhs_obj,)) { | ||
| ::core::result::Result::Ok(result) => return ::core::result::Result::Ok(result.into_ptr()), | ||
| ::core::result::Result::Err(e) => { | ||
| // If parent doesn't implement the method, return NotImplemented instead of AttributeError | ||
| if e.is_instance_of::<$crate::exceptions::PyAttributeError>(py) { | ||
| return ::core::result::Result::Ok(py.NotImplemented().into_ptr()); | ||
| } else { | ||
| return ::core::result::Result::Err(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
| slot: $crate::ffi::$slot, | ||
| pfunc: $crate::impl_::trampoline::get_trampoline_function!( | ||
| binaryfunc, slot_impl | ||
| ) as $crate::ffi::binaryfunc as _, | ||
| } | ||
| }}; | ||
|
|
||
| // Only reflected available | ||
| ($cls:ty, false, true) => {{ | ||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
|
|
||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| // LHS is our class, try parent's forward method via super() | ||
| // This ensures parent knows it's being called for forward dispatch | ||
| let py_type = <$cls as $crate::PyTypeInfo>::type_object(py); | ||
| let forward_name = $crate::intern!(py, concat!(stringify!($lhs))); | ||
| let other_bound = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| let super_obj = match $crate::types::PySuper::new(&py_type, &lhs_obj) { | ||
| ::core::result::Result::Ok(s) => s, | ||
| ::core::result::Result::Err(e) => return ::core::result::Result::Err(e), | ||
| }; | ||
| match super_obj.call_method1(forward_name, (&other_bound,)) { | ||
| ::core::result::Result::Ok(result) => return ::core::result::Result::Ok(result.into_ptr()), | ||
| ::core::result::Result::Err(e) => { | ||
| // If parent doesn't implement the method, | ||
| // try the reflected method on our class instead | ||
| // of returning NotImplemented for AttributeError | ||
| if !e.is_instance_of::<$crate::exceptions::PyAttributeError>(py) { | ||
| return ::core::result::Result::Err(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| let rhs_is_instance = rhs_obj.is_instance_of::<$cls>(); | ||
| if rhs_is_instance { | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| return unsafe { collector.$rhs(py, _other, _slf) }; | ||
| } | ||
|
|
||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
|
|
@@ -744,22 +852,138 @@ slot_fragment_trait! { | |
| #[doc(hidden)] | ||
| #[macro_export] | ||
| macro_rules! generate_pyclass_pow_slot { | ||
| ($cls:ty) => {{ | ||
| fn slot_impl( | ||
| // Both forward and reflected available | ||
| ($cls:ty, true, true) => {{ | ||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| _mod: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| let lhs_result = unsafe { collector.__pow__(py, _slf, _other, _mod) }?; | ||
| if lhs_result == unsafe { $crate::ffi::Py_NotImplemented() } { | ||
| unsafe { $crate::ffi::Py_DECREF(lhs_result) }; | ||
| unsafe { collector.__rpow__(py, _other, _slf, _mod) } | ||
| } else { | ||
| ::core::result::Result::Ok(lhs_result) | ||
|
|
||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| return unsafe { collector.__pow__(py, _slf, _other, _mod) }; | ||
|
Contributor
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. Here, we can add a fast path so that direct type object comparisons avoid the creation of a let lhs_type = unsafe { (*_slf).ob_type };
let type_ = <$cls as $crate::PyTypeInfo>::type_object_raw(py);
if lhs_type == type_ {
return unsafe { collector.$lhs(py, _slf, _other) };
} |
||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| if rhs_obj.is_instance_of::<$cls>() { | ||
| return unsafe { collector.__rpow__(py, _other, _slf, _mod) }; | ||
| } | ||
|
|
||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
| slot: $crate::ffi::Py_nb_power, | ||
| pfunc: $crate::impl_::trampoline::get_trampoline_function!(ternaryfunc, slot_impl) | ||
| as $crate::ffi::ternaryfunc as _, | ||
| } | ||
| }}; | ||
|
|
||
| // Only forward available | ||
| ($cls:ty, true, false) => {{ | ||
|
Contributor
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. Same type optimization as above: let collector = PyClassImplCollector::<$cls>::new();
- let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) };
- if lhs_obj.is_instance_of::<$cls>() {
+ let lhs_type = unsafe { (*_slf).ob_type };
+ let type_ = <$cls as $crate::PyTypeInfo>::type_object_raw(py);
+ if lhs_type == type_ || unsafe {
+ $crate::Bound::from_borrowed_ptr(py, _slf).is_instance_of::<$cls>()
+ } {
return unsafe { collector.$lhs(py, _slf, _other) };
}
- let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) };
- if rhs_obj.is_instance_of::<$cls>() {
+ let rhs_type = unsafe { (*_other).ob_type };
+ if rhs_type == type_ || unsafe {
+ $crate::Bound::from_borrowed_ptr(py, _other).is_instance_of::<$cls>()
+ } {
return unsafe { collector.$rhs(py, _other, _slf) };
} |
||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| _mod: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
|
|
||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| return unsafe { collector.__pow__(py, _slf, _other, _mod) }; | ||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| if rhs_obj.is_instance_of::<$cls>() { | ||
| // RHS is our class, try parent's reflected method via super() | ||
| // This ensures parent knows it's being called for reflection | ||
| let py_type = <$cls as $crate::PyTypeInfo>::type_object(py); | ||
| let mod_obj = if _mod.is_null() { | ||
| py.None().into_bound(py) | ||
| } else { | ||
| unsafe { $crate::Bound::from_borrowed_ptr(py, _mod) } | ||
| }; | ||
| let super_obj = match $crate::types::PySuper::new(&py_type, &rhs_obj) { | ||
| ::core::result::Result::Ok(s) => s, | ||
| ::core::result::Result::Err(e) => return ::core::result::Result::Err(e), | ||
| }; | ||
| match super_obj.call_method1($crate::intern!(py, "__rpow__"), (&lhs_obj, mod_obj)) { | ||
| ::core::result::Result::Ok(result) => return ::core::result::Result::Ok(result.into_ptr()), | ||
| ::core::result::Result::Err(e) => { | ||
| // If parent doesn't implement the method, return NotImplemented instead of AttributeError | ||
| if e.is_instance_of::<$crate::exceptions::PyAttributeError>(py) { | ||
| return ::core::result::Result::Ok(py.NotImplemented().into_ptr()); | ||
| } else { | ||
| return ::core::result::Result::Err(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
| slot: $crate::ffi::Py_nb_power, | ||
| pfunc: $crate::impl_::trampoline::get_trampoline_function!(ternaryfunc, slot_impl) | ||
| as $crate::ffi::ternaryfunc as _, | ||
| } | ||
| }}; | ||
|
|
||
| // Only reflected available | ||
| ($cls:ty, false, true) => {{ | ||
| unsafe fn slot_impl( | ||
| py: $crate::Python<'_>, | ||
| _slf: *mut $crate::ffi::PyObject, | ||
| _other: *mut $crate::ffi::PyObject, | ||
| _mod: *mut $crate::ffi::PyObject, | ||
| ) -> $crate::PyResult<*mut $crate::ffi::PyObject> { | ||
| use $crate::impl_::pyclass::*; | ||
| use $crate::types::PyAnyMethods; | ||
|
|
||
| let lhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _slf) }; | ||
| if lhs_obj.is_instance_of::<$cls>() { | ||
| // LHS is our class, try parent's forward method via super() | ||
| // This ensures parent knows it's being called for forward dispatch | ||
| let py_type = <$cls as $crate::PyTypeInfo>::type_object(py); | ||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| let mod_obj = if _mod.is_null() { | ||
| py.None().into_bound(py) | ||
| } else { | ||
| unsafe { $crate::Bound::from_borrowed_ptr(py, _mod) } | ||
| }; | ||
| let super_obj = match $crate::types::PySuper::new(&py_type, &lhs_obj) { | ||
| Ok(s) => s, | ||
| Err(e) => return Err(e), | ||
| }; | ||
| match super_obj.call_method1($crate::intern!(py, "__pow__"), (&rhs_obj, mod_obj)) { | ||
| ::core::result::Result::Ok(result) => return ::core::result::Result::Ok(result.into_ptr()), | ||
| ::core::result::Result::Err(e) => { | ||
| // If parent doesn't implement the method, | ||
| // try the reflected method on our class instead | ||
| // of returning NotImplemented for AttributeError | ||
| if !e.is_instance_of::<$crate::exceptions::PyAttributeError>(py) { | ||
| return ::core::result::Result::Err(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let rhs_obj = unsafe { $crate::Bound::from_borrowed_ptr(py, _other) }; | ||
| let rhs_is_instance = rhs_obj.is_instance_of::<$cls>(); | ||
| if rhs_is_instance { | ||
| let collector = PyClassImplCollector::<$cls>::new(); | ||
| return unsafe { collector.__rpow__(py, _other, _slf, _mod) }; | ||
| } | ||
|
|
||
| ::core::result::Result::Ok(py.NotImplemented().into_ptr()) | ||
| } | ||
|
|
||
| $crate::ffi::PyType_Slot { | ||
|
|
||
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.