GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add PyList_GetItem to C API by kairosci · Pull Request #9669 · RustPython/RustPython

Add PyList_GetItem to C API #9669

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.

Sign up for GitHub

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

Open
kairosci wants to merge 3 commits into RustPython:main
base: main
Loading
Loading
from kairosci:feature/issue-8156-list-getitem

Uh oh!

There was an error while loading. Please reload this page.

Open

Add PyList_GetItem to C API #9669

Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
607179d
Add PyList_GetItem to C API
kairosci Nov 3, 2026
b39fc72
Address PyList_GetItem review feedback
kairosci Nov 3, 2026
4d0fe6a
fix(capi): preserve PyList_GetItem cast errors
kairosci Nov 11, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

62 changes: 60 additions & 2 deletions 62 crates/capi/src/listobject.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ pub unsafe extern "C" fn PyList_GetItemRef(obj: *mut PyObject, index: isize) ->
})
}

#[unsafe(no_mangle)]
Comment thread
kairosci marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

pub unsafe extern "C" fn PyList_GetItem(obj: *mut PyObject, index: isize) -> *mut PyObject {
with_vm(|vm| {
let list = unsafe { obj.assume_borrowed_and_cast::<PyList>(vm) }?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

let vec = list.borrow_vec();
let item = index
.try_into()
.ok()
.and_then(|index: usize| vec.get(index))
.ok_or_else(|| vm.new_index_error("list index out of range"))?;
Ok(item.as_object().as_raw().cast_mut())
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyList_SetItem(
list: *mut PyObject,
Expand Down Expand Up @@ -167,9 +181,53 @@ pub unsafe extern "C" fn PyList_Sort(list: *mut PyObject) -> c_int {

#[cfg(test)]
mod tests {
use pyo3::exceptions::PyIndexError;
use pyo3::exceptions::{PyIndexError, PyTypeError};
use pyo3::prelude::*;
use pyo3::types::{PyList, PyListMethods};
use pyo3::types::{PyDict, PyList, PyListMethods, PyStringMethods, PyTuple};

#[test]
fn get_item_borrowed() {
Python::attach(|py| {
let list = PyList::empty(py);
let first = PyDict::new(py);
let last = PyDict::new(py);
list.append(&first).unwrap();
list.append(&last).unwrap();

let got_first = unsafe {
Borrowed::from_ptr(py, super::PyList_GetItem(list.as_ptr().cast(), 0).cast())
};
let got_last = unsafe {
Borrowed::from_ptr(py, super::PyList_GetItem(list.as_ptr().cast(), 1).cast())
};
assert!(got_first.is(&first));
assert!(got_last.is(&last));

assert!(unsafe { super::PyList_GetItem(list.as_ptr().cast(), -1) }.is_null());
let error = PyErr::fetch(py);
assert!(error.is_instance_of::<PyIndexError>(py));
assert_eq!(
error.value(py).str().unwrap().to_str().unwrap(),
"list index out of range"
);

assert!(unsafe { super::PyList_GetItem(list.as_ptr().cast(), 2) }.is_null());
let error = PyErr::fetch(py);
assert!(error.is_instance_of::<PyIndexError>(py));
assert_eq!(
error.value(py).str().unwrap().to_str().unwrap(),
"list index out of range"
);

let empty = PyList::empty(py);
assert!(unsafe { super::PyList_GetItem(empty.as_ptr().cast(), 0) }.is_null());
assert!(PyErr::fetch(py).is_instance_of::<PyIndexError>(py));

let tuple = PyTuple::new(py, [10]).unwrap();
assert!(unsafe { super::PyList_GetItem(tuple.as_ptr().cast(), 0) }.is_null());
assert!(PyErr::fetch(py).is_instance_of::<PyTypeError>(py));
});
}

#[test]
fn create_list() {
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.