Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use PyList.get_item_unchecked() on free-threaded build #4539

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions newsfragments/4539.removed.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 5 additions & 5 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)))]
ngoldbaum marked this conversation as resolved.
Show resolved Hide resolved
let item = self.list.get_item_unchecked(index);
item
}
Expand Down Expand Up @@ -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| {
Expand Down
Loading