You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In src/types/slice.rs, a PySlice is constructed like this:
impl PySlice {
/// Constructs a new slice with the given elements.
pub fn new(py: Python<'_>, start: isizbe, 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),
);
py.from_owned_ptr(ptr)
}
}
...
The isize arguments are cast to std::os::raw::c_long. The issue is that, surprisingly, on Windows c_long is only 4 bytes. This causes slices to indices greater than i32::MAX to return incorrect results (see repro below).
To be clear, slices above i32::MAX work in vanilla Python on Windows:
import numpy
array = numpy.arange(3_000_000_000)
array[2_999_999_997:9_223_372_036_854_775_807] # Slice goes to i64::MAX. This works in Python but fails in PyO3
array([2999999997, 2999999998, 2999999999], dtype=int64)
Steps to Reproduce
use pyo3::{types::PySlice, Python};
fn main() {
Python::with_gil(|py| {
let slice = PySlice::new(py, 0, isize::MAX, 1);
println!("{}", slice.call_method0("__str__").unwrap().extract::<String>().unwrap());
// "slice(0, 9223372036854775807, 1)" on Linux
// "slice(0, -1, 1)" on Windows
});
}
Backtrace
No response
Your operating system and version
Windows
Your Python version (python --version)
3.10.5
Your Rust version (rustc --version)
rustc 1.65.0 (897e37553 2022-11-02)
Your PyO3 version
0.16.5
How did you install python? Did you use a virtualenv?
python.org installer
Additional Info
No response
The text was updated successfully, but these errors were encountered:
Bug Description
In src/types/slice.rs, a
PySlice
is constructed like this:The
isize
arguments are cast tostd::os::raw::c_long
. The issue is that, surprisingly, on Windows c_long is only 4 bytes. This causes slices to indices greater than i32::MAX to return incorrect results (see repro below).To be clear, slices above i32::MAX work in vanilla Python on Windows:
Steps to Reproduce
Backtrace
No response
Your operating system and version
Windows
Your Python version (
python --version
)3.10.5
Your Rust version (
rustc --version
)rustc 1.65.0 (897e37553 2022-11-02)
Your PyO3 version
0.16.5
How did you install python? Did you use a virtualenv?
python.org installer
Additional Info
No response
The text was updated successfully, but these errors were encountered: