forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceobject.rs
More file actions
106 lines (99 loc) · 2.81 KB
/
Copy pathsliceobject.rs
File metadata and controls
106 lines (99 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::PyObject;
use crate::pystate::with_vm;
use core::ffi::c_int;
use rustpython_vm::PyPayload;
use rustpython_vm::builtins::PySlice;
use rustpython_vm::sliceable::SaturatedSlice;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PySlice_New(
start: *mut PyObject,
stop: *mut PyObject,
step: *mut PyObject,
) -> *mut PyObject {
with_vm(|vm| {
let start = if start.is_null() {
None
} else {
Some(unsafe { &*start }.to_owned())
};
let stop = if stop.is_null() {
vm.ctx.none()
} else {
unsafe { &*stop }.to_owned()
};
let step = if step.is_null() {
None
} else {
Some(unsafe { &*step }.to_owned())
};
Ok(PySlice { start, stop, step }.into_ref(&vm.ctx))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PySlice_Unpack(
slice: *mut PyObject,
start: *mut isize,
stop: *mut isize,
step: *mut isize,
) -> c_int {
with_vm(|vm| {
let slice = unsafe { &*slice }.try_downcast_ref::<PySlice>(vm)?;
let saturated = slice.to_saturated(vm)?;
unsafe {
*start = saturated.start();
*stop = saturated.stop();
*step = saturated.step();
}
Ok(())
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PySlice_AdjustIndices(
length: isize,
start: *mut isize,
stop: *mut isize,
step: isize,
) -> isize {
let length = length.max(0) as usize;
let saturated = SaturatedSlice::from_parts(unsafe { *start }, unsafe { *stop }, step);
let (range, _, slice_len) = saturated.adjust_indices(length);
unsafe {
if step.is_negative() {
*start = range.end as isize - 1;
*stop = range.start as isize - 1;
} else {
*start = range.start as isize;
*stop = range.end as isize;
}
}
slice_len as isize
}
#[cfg(false)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::{PySlice, PySliceMethods};
#[test]
fn slice_new_indices() {
Python::attach(|py| {
let slice = PySlice::new(py, 1, 10, 3);
let indices = slice.indices(100).unwrap();
assert_eq!((indices.start, indices.stop, indices.step), (1, 10, 3));
})
}
#[test]
fn slice_full_defaults() {
Python::attach(|py| {
let slice = PySlice::full(py);
let indices = slice.indices(5).unwrap();
assert_eq!((indices.start, indices.stop, indices.step), (0, 5, 1));
})
}
#[test]
fn slice_new_negative_step() {
Python::attach(|py| {
let slice = PySlice::new(py, 10, 1, -2);
let indices = slice.indices(100).unwrap();
assert_eq!((indices.start, indices.stop, indices.step), (10, 1, -2));
})
}
}