-
Notifications
You must be signed in to change notification settings - Fork 291
fix python GC traversal for validators and serializers #787
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
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
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,70 @@ | ||
use ahash::AHashMap; | ||
use enum_dispatch::enum_dispatch; | ||
use pyo3::{AsPyPointer, Py, PyTraverseError, PyVisit}; | ||
|
||
/// Trait implemented by types which can be traversed by the Python GC. | ||
#[enum_dispatch] | ||
pub trait PyGcTraverse { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError>; | ||
} | ||
|
||
impl<T> PyGcTraverse for Py<T> | ||
where | ||
Py<T>: AsPyPointer, | ||
{ | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
visit.call(self) | ||
} | ||
} | ||
|
||
impl<T: PyGcTraverse> PyGcTraverse for Vec<T> { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
for item in self { | ||
item.py_gc_traverse(visit)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: PyGcTraverse> PyGcTraverse for AHashMap<String, T> { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
for item in self.values() { | ||
item.py_gc_traverse(visit)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: PyGcTraverse> PyGcTraverse for Box<T> { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
T::py_gc_traverse(self, visit) | ||
} | ||
} | ||
|
||
impl<T: PyGcTraverse> PyGcTraverse for Option<T> { | ||
fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { | ||
match self { | ||
Some(item) => T::py_gc_traverse(item, visit), | ||
None => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
/// A crude alternative to a "derive" macro to help with building PyGcTraverse implementations | ||
macro_rules! impl_py_gc_traverse { | ||
($name:ty { }) => { | ||
impl crate::py_gc::PyGcTraverse for $name { | ||
fn py_gc_traverse(&self, _visit: &pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> { | ||
Ok(()) | ||
} | ||
} | ||
}; | ||
($name:ty { $($fields:ident),* }) => { | ||
impl crate::py_gc::PyGcTraverse for $name { | ||
fn py_gc_traverse(&self, visit: &pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> { | ||
$(self.$fields.py_gc_traverse(visit)?;)* | ||
Ok(()) | ||
} | ||
} | ||
}; | ||
} |
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
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
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
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
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
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
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.
Seems like these might even make sense in PyO3 at some point, right?
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.
Yes, though probably not in this form. In PyO3 I'd like (if it's possible) to do it automatically with a
#[derive]
macro, however the challenge is how to make most types noops and enable gc traversal for just the ones that matter. I'm sure there's an issue about this in the PyO3 backlog but I can't find it right now.