Skip to content

Commit

Permalink
PySlice: fix isize->long truncation in initialization
Browse files Browse the repository at this point in the history
Fixes #2768
  • Loading branch information
birkenfeld committed Nov 22, 2022
1 parent 8ca41be commit cbae47d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/2769.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix truncation of `isize` values to `c_long` in `PySlice::new`.
25 changes: 22 additions & 3 deletions src/types/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ impl PySlice {
pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice {
unsafe {
let ptr = ffi::PySlice_New(
ffi::PyLong_FromLong(start as c_long),
ffi::PyLong_FromLong(stop as c_long),
ffi::PyLong_FromLong(step as c_long),
ffi::PyLong_FromSsize_t(start),
ffi::PyLong_FromSsize_t(stop),
ffi::PyLong_FromSsize_t(step),
);
py.from_owned_ptr(ptr)
}
Expand Down Expand Up @@ -99,6 +99,25 @@ impl ToPyObject for PySliceIndices {
mod tests {
use super::*;

#[test]
fn test_py_slice_new() {
Python::with_gil(|py| {
let slice = PySlice::new(py, isize::MIN, isize::MAX, 1);
assert_eq!(
slice.getattr("start").unwrap().extract::<isize>().unwrap(),
isize::MIN
);
assert_eq!(
slice.getattr("stop").unwrap().extract::<isize>().unwrap(),
isize::MAX
);
assert_eq!(
slice.getattr("step").unwrap().extract::<isize>().unwrap(),
1
);
});
}

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

0 comments on commit cbae47d

Please sign in to comment.