From cbae47d1715ccce826e2b159644785967201adf1 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 22 Nov 2022 11:57:38 +0100 Subject: [PATCH] PySlice: fix isize->long truncation in initialization Fixes #2768 --- newsfragments/2769.fixed.md | 1 + src/types/slice.rs | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 newsfragments/2769.fixed.md diff --git a/newsfragments/2769.fixed.md b/newsfragments/2769.fixed.md new file mode 100644 index 00000000..d575dd2a --- /dev/null +++ b/newsfragments/2769.fixed.md @@ -0,0 +1 @@ +Fix truncation of `isize` values to `c_long` in `PySlice::new`. diff --git a/src/types/slice.rs b/src/types/slice.rs index 460383b1..61574f64 100644 --- a/src/types/slice.rs +++ b/src/types/slice.rs @@ -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) } @@ -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::().unwrap(), + isize::MIN + ); + assert_eq!( + slice.getattr("stop").unwrap().extract::().unwrap(), + isize::MAX + ); + assert_eq!( + slice.getattr("step").unwrap().extract::().unwrap(), + 1 + ); + }); + } + #[test] fn test_py_slice_indices_new() { let start = 0;