From 2940e19a6f3d023e9b629c5bb7fd1016398a7136 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. --- newsfragments/3616.added.md | 1 + src/sync.rs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 newsfragments/3616.added.md diff --git a/newsfragments/3616.added.md b/newsfragments/3616.added.md new file mode 100644 index 00000000000..3f8c3a9ff08 --- /dev/null +++ b/newsfragments/3616.added.md @@ -0,0 +1 @@ +Add `get_visit` method to `GILProtected` diff --git a/src/sync.rs b/src/sync.rs index 8500413ef97..6fa70b583f6 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, PyVisit, Python}; 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 {}