Skip to content

Commit a49895e

Browse files
committed
Cleanup slice_new and added more slice tests
1 parent 5625f7e commit a49895e

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

tests/snippets/builtin_slice.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
b = [1, 2]
1212

1313
assert b[:] == [1, 2]
14+
assert b[slice(None)] == [1, 2]
1415
assert b[:2**100] == [1, 2]
1516
assert b[-2**100:] == [1, 2]
1617
assert b[2**100:] == []
@@ -77,3 +78,15 @@ def __setitem__(self, key, value):
7778
ss = SubScript()
7879
_ = ss[:]
7980
ss[:1] = 1
81+
82+
83+
class CustomIndex:
84+
def __init__(self, x):
85+
self.x = x
86+
87+
def __index__(self):
88+
return self.x
89+
90+
91+
assert c[CustomIndex(1):CustomIndex(3)] == [1, 2]
92+
assert d[CustomIndex(1):CustomIndex(3)] == "23"

vm/src/function.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashMap;
2+
use std::mem;
23
use std::ops::RangeInclusive;
34

45
use crate::obj::objtype::{isinstance, PyClassRef};
@@ -46,6 +47,12 @@ impl From<(&Args, &KwArgs)> for PyFuncArgs {
4647
}
4748
}
4849

50+
impl FromArgs for PyFuncArgs {
51+
fn from_args(_vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> {
52+
Ok(mem::replace(args, Default::default()))
53+
}
54+
}
55+
4956
impl PyFuncArgs {
5057
pub fn new(mut args: Vec<PyObjectRef>, kwarg_names: Vec<String>) -> PyFuncArgs {
5158
let mut kwargs = vec![];

vm/src/obj/objslice.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@ impl PyValue for PySlice {
2424

2525
pub type PySliceRef = PyRef<PySlice>;
2626

27-
fn slice_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
28-
let (cls, slice): (PyClassRef, PySlice) = match args.args.len() {
29-
0 | 1 => {
27+
fn slice_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PySliceRef> {
28+
let slice: PySlice = match args.args.len() {
29+
0 => {
3030
return Err(vm.new_type_error("slice() must have at least one arguments.".to_owned()));
3131
}
32-
2 => {
33-
let (cls, stop) = args.bind(vm)?;
34-
(
35-
cls,
36-
PySlice {
37-
start: None,
38-
stop: Some(stop),
39-
step: None,
40-
},
41-
)
32+
1 => {
33+
let stop = args.bind(vm)?;
34+
PySlice {
35+
start: None,
36+
stop: Some(stop),
37+
step: None,
38+
}
4239
}
4340
_ => args.bind(vm)?,
4441
};
45-
slice.into_ref_with_type(vm, cls).map(|x| x.into_object())
42+
slice.into_ref_with_type(vm, cls)
4643
}
4744

4845
fn get_property_value(vm: &VirtualMachine, value: &Option<PyObjectRef>) -> PyObjectRef {

0 commit comments

Comments
 (0)