Skip to content

Commit

Permalink
use infallible tuple item lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
brownben committed Nov 8, 2024
1 parent 6d84eb3 commit 6df1a65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ pub fn get_executed_lines(tracer_object: &PyObject) -> Lines {
filename_line_pairs
.into_iter()
.map(|tuple| {
let filename = tuple.get_tuple_item(0).unwrap().to_string();
let filename = tuple.get_tuple_item(0).to_string();
let lines = tuple
.get_tuple_item(1)
.unwrap()
.into_iter()
.map(PyObject::get_long)
.collect();
Expand Down Expand Up @@ -185,7 +184,7 @@ fn get_line_numbers_from_code_object(
line_numbers.extend(
iterator_lines
.iter()
.map(|line_tuple| line_tuple.get_tuple_item(2).unwrap().get_long())
.map(|line_tuple| line_tuple.get_tuple_item(2).get_long())
.filter(|line_number| *line_number > 0),
);

Expand Down
8 changes: 6 additions & 2 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ impl PyObject {
}

/// Assume is a tuple, and get the item at the given index
pub fn get_tuple_item(&self, index: isize) -> Result<PyObject, Error> {
unsafe { Self::new(ffi::PyTuple_GET_ITEM(self.as_ptr(), index)) }
pub fn get_tuple_item(&self, index: isize) -> PyObject {
let result = unsafe { ffi::PyTuple_GetItem(self.as_ptr(), index) };
let pointer = unsafe { ptr::NonNull::new_unchecked(result) };

PyObject(pointer)
}
}

/// Assume is a Long, and get the value
Expand Down

0 comments on commit 6df1a65

Please sign in to comment.