From d100f16d8ee9500a6ffc4943d17ab47f6c0304b4 Mon Sep 17 00:00:00 2001 From: Nathan Kent Date: Thu, 30 Nov 2023 13:27:57 -0800 Subject: [PATCH] Enable `GILProtected` access via `PyVisit` 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. --- src/sync.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sync.rs b/src/sync.rs index 8500413ef97..4a99c58b4db 100644 --- a/src/sync.rs +++ b/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. @@ -37,6 +37,11 @@ impl GILProtected { 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 Sync for GILProtected where T: Send {}