Skip to content
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
1 change: 1 addition & 0 deletions newsfragments/3353.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `PySlice::full()` to construct a full slice (`::`).
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