Skip to content

Commit

Permalink
Enable GILProtected access via PyVisit
Browse files Browse the repository at this point in the history
Closes #3615

This simply adds a new method which uses the existence of a `PyVisit`
object as proof that the GIL is held instead of a `Python` object. This
allows `GILProtected` to be used in instances where contained Python
objects need to participate in garbage collection. Usage in this
situation should be valid since no Python calls are made and this does
not provide any additional mechanism for accessing a `Python` object.
  • Loading branch information
neachdainn committed Nov 30, 2023
1 parent 81ad2e8 commit 5de907c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/3616.added.md
@@ -0,0 +1 @@
Add `get_visit` method to `GILProtected`
7 changes: 6 additions & 1 deletion src/sync.rs
@@ -1,5 +1,5 @@
//! Synchronization mechanisms based on the Python GIL.
use crate::{types::PyString, types::PyType, Py, PyErr, Python};
use crate::{types::PyString, types::PyType, Py, PyErr, Python, PyVisit};
use std::cell::UnsafeCell;

/// Value with concurrent access protected by the GIL.
Expand Down Expand Up @@ -37,6 +37,11 @@ impl<T> GILProtected<T> {
pub fn get<'py>(&'py self, _py: Python<'py>) -> &'py T {
&self.value
}

/// Gain access to the inner value by giving proof that garbage collection is happening.
pub fn get_visit<'py>(&'py self, _visit: PyVisit<'py>) -> &'py T {
&self.value
}
}

unsafe impl<T> Sync for GILProtected<T> where T: Send {}
Expand Down

0 comments on commit 5de907c

Please sign in to comment.