Skip to content

Commit ce18f79

Browse files
authored
Don't use PyList.get_item_unchecked() on free-threaded build (#4539)
* disable list.get_item_unchecked() on the free-threaded build * add changelog entry
1 parent 116170a commit ce18f79

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

newsfragments/4539.removed.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `PyListMethods::get_item_unchecked` is disabled on the free-threaded build.
2+
It relies on accessing list internals without any locking and is not
3+
thread-safe without the GIL to synchronize access.

src/types/list.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub trait PyListMethods<'py>: crate::sealed::Sealed {
182182
/// # Safety
183183
///
184184
/// Caller must verify that the index is within the bounds of the list.
185-
#[cfg(not(Py_LIMITED_API))]
185+
#[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))]
186186
unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>;
187187

188188
/// Takes the slice `self[low:high]` and returns it as a new list.
@@ -305,7 +305,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> {
305305
/// # Safety
306306
///
307307
/// Caller must verify that the index is within the bounds of the list.
308-
#[cfg(not(Py_LIMITED_API))]
308+
#[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))]
309309
unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny> {
310310
// PyList_GET_ITEM return borrowed ptr; must make owned for safety (see #890).
311311
ffi::PyList_GET_ITEM(self.as_ptr(), index as Py_ssize_t)
@@ -496,9 +496,9 @@ impl<'py> BoundListIterator<'py> {
496496
}
497497

498498
unsafe fn get_item(&self, index: usize) -> Bound<'py, PyAny> {
499-
#[cfg(any(Py_LIMITED_API, PyPy))]
499+
#[cfg(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED))]
500500
let item = self.list.get_item(index).expect("list.get failed");
501-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
501+
#[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))]
502502
let item = self.list.get_item_unchecked(index);
503503
item
504504
}
@@ -893,7 +893,7 @@ mod tests {
893893
});
894894
}
895895

896-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
896+
#[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))]
897897
#[test]
898898
fn test_list_get_item_unchecked_sanity() {
899899
Python::with_gil(|py| {

0 commit comments

Comments
 (0)