Skip to content

Commit

Permalink
Merge pull request #3353 from juntyr/full-slice
Browse files Browse the repository at this point in the history
Add a `PySlice::full()` constructor for `::`
  • Loading branch information
davidhewitt committed Jul 29, 2023
2 parents eb88596 + ab78323 commit 096552e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/3353.added.md
@@ -0,0 +1 @@
Add `PySlice::full()` to construct a full slice (`::`).
36 changes: 36 additions & 0 deletions src/types/slice.rs
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 096552e

Please sign in to comment.