Skip to content

Commit

Permalink
Use PyList_GetItemRef in list.get_item()
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Aug 7, 2024
1 parent 6b249e9 commit 4b00a10
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/types/list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::iter::FusedIterator;

use crate::err::{self, PyResult};
use crate::err::{self, PyErr, PyResult};
use crate::ffi::{self, Py_ssize_t};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::instance::Borrowed;
use crate::internal_tricks::get_ssize_index;
use crate::types::{PySequence, PyTuple};
use crate::{Bound, PyAny, PyObject, Python, ToPyObject};
Expand Down Expand Up @@ -288,12 +287,12 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> {
/// });
/// ```
fn get_item(&self, index: usize) -> PyResult<Bound<'py, PyAny>> {
unsafe {
// PyList_GetItem return borrowed ptr; must make owned for safety (see #890).
ffi::PyList_GetItem(self.as_ptr(), index as Py_ssize_t)
.assume_borrowed_or_err(self.py())
.map(Borrowed::to_owned)
let py = self.py();
let result = unsafe { ffi::compat::PyList_GetItemRef(self.as_ptr(), index as Py_ssize_t) };
if !result.is_null() {
return Ok(unsafe { result.assume_owned(py) });
}
Err(PyErr::fetch(py))
}

/// Gets the list item at the specified index. Undefined behavior on bad index. Use with caution.
Expand Down

0 comments on commit 4b00a10

Please sign in to comment.