Skip to content

Commit

Permalink
Add a PySlice::full() constructor for ::
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr authored and davidhewitt committed Jul 30, 2023
1 parent 7090e6b commit 9282f31
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/types/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ impl PySlice {
}
}

/// Constructs a new full slice that is equivalent to `::`.
pub fn full(py: Python<'_>) -> &PySlice {
unsafe {
let ptr = ffi::PySlice_New(ffi::Py_None(), ffi::Py_None(), ffi::Py_None());
py.from_owned_ptr(ptr)
}
}

/// Retrieves the start, stop, and step indices from the slice object,
/// assuming a sequence of length `length`, and stores the length of the
/// slice in its `slicelength` member.
Expand Down Expand Up @@ -116,6 +124,34 @@ mod tests {
});
}

#[test]
fn test_py_slice_full() {
Python::with_gil(|py| {
let slice = PySlice::full(py);
assert!(slice.getattr("start").unwrap().is_none(),);
assert!(slice.getattr("stop").unwrap().is_none(),);
assert!(slice.getattr("step").unwrap().is_none(),);
assert_eq!(
slice.indices(0).unwrap(),
PySliceIndices {
start: 0,
stop: 0,
step: 1,
slicelength: 0,
},
);
assert_eq!(
slice.indices(42).unwrap(),
PySliceIndices {
start: 0,
stop: 42,
step: 1,
slicelength: 42,
},
);
});
}

#[test]
fn test_py_slice_indices_new() {
let start = 0;
Expand Down

0 comments on commit 9282f31

Please sign in to comment.