PySlice: fix isize->long truncation in initialization

Fixes #2768
This commit is contained in:
Georg Brandl 2022-11-22 11:57:38 +01:00
parent 8ca41be87b
commit cbae47d171
2 changed files with 23 additions and 3 deletions

View File

@ -0,0 +1 @@
Fix truncation of `isize` values to `c_long` in `PySlice::new`.

View File

@ -48,9 +48,9 @@ impl PySlice {
pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice { pub fn new(py: Python<'_>, start: isize, stop: isize, step: isize) -> &PySlice {
unsafe { unsafe {
let ptr = ffi::PySlice_New( let ptr = ffi::PySlice_New(
ffi::PyLong_FromLong(start as c_long), ffi::PyLong_FromSsize_t(start),
ffi::PyLong_FromLong(stop as c_long), ffi::PyLong_FromSsize_t(stop),
ffi::PyLong_FromLong(step as c_long), ffi::PyLong_FromSsize_t(step),
); );
py.from_owned_ptr(ptr) py.from_owned_ptr(ptr)
} }
@ -99,6 +99,25 @@ impl ToPyObject for PySliceIndices {
mod tests { mod tests {
use super::*; 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] #[test]
fn test_py_slice_indices_new() { fn test_py_slice_indices_new() {
let start = 0; let start = 0;