Skip to content

Commit

Permalink
Change the type of PySliceIndices::slicelength and `PySlice::indice…
Browse files Browse the repository at this point in the history
…s()`
  • Loading branch information
cmpute committed Jan 26, 2024
1 parent f449fc0 commit 3d8cd6b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions newsfragments/3761.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the type of `PySliceIndices::slicelength` and the `length` parameter of `PySlice::indices()`.
15 changes: 9 additions & 6 deletions src/types/slice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::err::{PyErr, PyResult};
use crate::ffi::{self, Py_ssize_t};
use crate::ffi;
use crate::{PyAny, PyObject, Python, ToPyObject};
use std::os::raw::c_long;

/// Represents a Python `slice`.
///
Expand All @@ -20,13 +19,17 @@ pyobject_native_type!(
#[derive(Debug, Eq, PartialEq)]
pub struct PySliceIndices {
/// Start of the slice
///
/// It can be -1 when the step is negative, otherwise it's non-negative.
pub start: isize,
/// End of the slice
///
/// It can be -1 when the step is negative, otherwise it's non-negative.
pub stop: isize,
/// Increment to use when iterating the slice from `start` to `stop`.
pub step: isize,
/// The length of the slice calculated from the original input sequence.
pub slicelength: isize,
pub slicelength: usize,
}

impl PySliceIndices {
Expand Down Expand Up @@ -66,7 +69,7 @@ impl PySlice {
/// assuming a sequence of length `length`, and stores the length of the
/// slice in its `slicelength` member.
#[inline]
pub fn indices(&self, length: c_long) -> PyResult<PySliceIndices> {
pub fn indices(&self, length: isize) -> PyResult<PySliceIndices> {
// non-negative Py_ssize_t should always fit into Rust usize
unsafe {
let mut slicelength: isize = 0;
Expand All @@ -75,7 +78,7 @@ impl PySlice {
let mut step: isize = 0;
let r = ffi::PySlice_GetIndicesEx(
self.as_ptr(),
length as Py_ssize_t,
length,
&mut start,
&mut stop,
&mut step,
Expand All @@ -86,7 +89,7 @@ impl PySlice {
start,
stop,
step,
slicelength,
slicelength: slicelength as _,
})
} else {
Err(PyErr::fetch(self.py()))
Expand Down

0 comments on commit 3d8cd6b

Please # to comment.