-
Notifications
You must be signed in to change notification settings - Fork 747
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
avoid creating PyRef
inside __traverse__
handler
#4479
Merged
Merged
Changes from all commits
Commits
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 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 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 @@ | ||
Remove illegal reference counting op inside implementation of `__traverse__` handlers. |
This file contains 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 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 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 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,48 +1,75 @@ | ||
use std::os::raw::{c_int, c_void}; | ||
use std::{ | ||
marker::PhantomData, | ||
os::raw::{c_int, c_void}, | ||
}; | ||
|
||
use crate::{ffi, AsPyPointer, Python}; | ||
use crate::{ffi, AsPyPointer}; | ||
|
||
/// Error returned by a `__traverse__` visitor implementation. | ||
#[repr(transparent)] | ||
pub struct PyTraverseError(pub(crate) c_int); | ||
pub struct PyTraverseError(NonZeroCInt); | ||
|
||
impl PyTraverseError { | ||
/// Returns the error code. | ||
pub(crate) fn into_inner(self) -> c_int { | ||
self.0.into() | ||
} | ||
} | ||
|
||
/// Object visitor for GC. | ||
#[derive(Clone)] | ||
pub struct PyVisit<'p> { | ||
pub struct PyVisit<'a> { | ||
pub(crate) visit: ffi::visitproc, | ||
pub(crate) arg: *mut c_void, | ||
/// VisitProc contains a Python instance to ensure that | ||
/// 1) it is cannot be moved out of the traverse() call | ||
/// 2) it cannot be sent to other threads | ||
pub(crate) _py: Python<'p>, | ||
/// Prevents the `PyVisit` from outliving the `__traverse__` call. | ||
pub(crate) _guard: PhantomData<&'a ()>, | ||
} | ||
|
||
impl<'p> PyVisit<'p> { | ||
impl<'a> PyVisit<'a> { | ||
/// Visit `obj`. | ||
pub fn call<T>(&self, obj: &T) -> Result<(), PyTraverseError> | ||
where | ||
T: AsPyPointer, | ||
{ | ||
let ptr = obj.as_ptr(); | ||
if !ptr.is_null() { | ||
let r = unsafe { (self.visit)(ptr, self.arg) }; | ||
if r == 0 { | ||
Ok(()) | ||
} else { | ||
Err(PyTraverseError(r)) | ||
match NonZeroCInt::new(unsafe { (self.visit)(ptr, self.arg) }) { | ||
None => Ok(()), | ||
Some(r) => Err(PyTraverseError(r)), | ||
} | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
/// Creates the PyVisit from the arguments to tp_traverse | ||
#[doc(hidden)] | ||
pub unsafe fn from_raw(visit: ffi::visitproc, arg: *mut c_void, py: Python<'p>) -> Self { | ||
Self { | ||
visit, | ||
arg, | ||
_py: py, | ||
} | ||
/// Workaround for `NonZero<c_int>` not being available until MSRV 1.79 | ||
mod get_nonzero_c_int { | ||
pub struct GetNonZeroCInt<const WIDTH: usize>(); | ||
|
||
pub trait NonZeroCIntType { | ||
type Type; | ||
} | ||
impl NonZeroCIntType for GetNonZeroCInt<16> { | ||
type Type = std::num::NonZeroI16; | ||
} | ||
impl NonZeroCIntType for GetNonZeroCInt<32> { | ||
type Type = std::num::NonZeroI32; | ||
} | ||
|
||
pub type Type = | ||
<GetNonZeroCInt<{ std::mem::size_of::<std::os::raw::c_int>() * 8 }> as NonZeroCIntType>::Type; | ||
} | ||
|
||
use get_nonzero_c_int::Type as NonZeroCInt; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::PyVisit; | ||
use static_assertions::assert_not_impl_any; | ||
|
||
#[test] | ||
fn py_visit_not_send_sync() { | ||
assert_not_impl_any!(PyVisit<'_>: Send, Sync); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely removed the
Python::assume_gil_acquired
call from_call_traverse
, so I needed to place a different reference back in for the lifetime. This is sufficient because the*mut c_void
already preventsSend
/Sync
.