From d8a228cd85a696650af4401703019290c0053b1b Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Mon, 9 Sep 2024 14:05:56 -0600 Subject: [PATCH 1/2] disable list.get_item_unchecked() on the free-threaded build --- src/types/list.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/types/list.rs b/src/types/list.rs index 0fa33d41a44..8e2c50ea397 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -175,7 +175,7 @@ pub trait PyListMethods<'py>: crate::sealed::Sealed { /// # Safety /// /// Caller must verify that the index is within the bounds of the list. - #[cfg(not(Py_LIMITED_API))] + #[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; /// Takes the slice `self[low:high]` and returns it as a new list. @@ -298,7 +298,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> { /// # Safety /// /// Caller must verify that the index is within the bounds of the list. - #[cfg(not(Py_LIMITED_API))] + #[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny> { // PyList_GET_ITEM return borrowed ptr; must make owned for safety (see #890). ffi::PyList_GET_ITEM(self.as_ptr(), index as Py_ssize_t) @@ -465,9 +465,9 @@ impl<'py> BoundListIterator<'py> { } unsafe fn get_item(&self, index: usize) -> Bound<'py, PyAny> { - #[cfg(any(Py_LIMITED_API, PyPy))] + #[cfg(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED))] let item = self.list.get_item(index).expect("list.get failed"); - #[cfg(not(any(Py_LIMITED_API, PyPy)))] + #[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))] let item = self.list.get_item_unchecked(index); item } @@ -863,7 +863,7 @@ mod tests { }); } - #[cfg(not(any(Py_LIMITED_API, PyPy)))] + #[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))] #[test] fn test_list_get_item_unchecked_sanity() { Python::with_gil(|py| { From 763a2417e522fd80781e982be7352b7c241ca1e8 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Thu, 26 Sep 2024 10:46:18 -0600 Subject: [PATCH 2/2] add changelog entry --- newsfragments/4539.removed.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 newsfragments/4539.removed.md diff --git a/newsfragments/4539.removed.md b/newsfragments/4539.removed.md new file mode 100644 index 00000000000..dd1da0169b6 --- /dev/null +++ b/newsfragments/4539.removed.md @@ -0,0 +1,3 @@ +* `PyListMethods::get_item_unchecked` is disabled on the free-threaded build. + It relies on accessing list internals without any locking and is not + thread-safe without the GIL to synchronize access.