Skip to content

Commit 6073151

Browse files
authored
Implement array getitem by slice
1 parent b58bd86 commit 6073151

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Lib/test/test_array.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,6 @@ def test_delitem(self):
739739
self.assertRaises(IndexError, a.__delitem__, len(self.example))
740740
self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1)
741741

742-
# TODO: RUSTPYTHON
743-
@unittest.expectedFailure
744742
def test_getslice(self):
745743
a = array.array(self.typecode, self.example)
746744
self.assertEqual(a[:], a)
@@ -791,8 +789,6 @@ def test_getslice(self):
791789
array.array(self.typecode)
792790
)
793791

794-
# TODO: RUSTPYTHON
795-
@unittest.expectedFailure
796792
def test_extended_getslice(self):
797793
# Test extended slicing by comparing with list slicing
798794
# (Assumes list conversion works correctly, too)
@@ -1206,8 +1202,6 @@ def test_issue17223(self):
12061202

12071203
class NumberTest(BaseTest):
12081204

1209-
# TODO: RUSTPYTHON
1210-
@unittest.expectedFailure
12111205
def test_extslice(self):
12121206
a = array.array(self.typecode, range(5))
12131207
self.assertEqual(a[::], a)

vm/src/stdlib/array.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use crate::common::cell::{
44
};
55
use crate::function::OptionalArg;
66
use crate::obj::objbytes::PyBytesRef;
7+
use crate::obj::objsequence::PySliceableSequence;
78
use crate::obj::objslice::PySliceRef;
89
use crate::obj::objstr::PyStringRef;
910
use crate::obj::objtype::PyClassRef;
1011
use crate::obj::{objbool, objiter};
1112
use crate::pyobject::{
1213
BorrowValue, Either, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue,
13-
PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
14+
PyIterable, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
1415
};
1516
use crate::VirtualMachine;
1617
use crossbeam_utils::atomic::AtomicCell;
@@ -194,14 +195,31 @@ macro_rules! def_array_enum {
194195
}
195196
}
196197

198+
fn getitem_by_slice(&self, slice: PySliceRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
199+
match self {
200+
$(ArrayContentType::$n(v) => {
201+
let elements = v.get_slice_items(vm, &slice)?;
202+
let sliced = ArrayContentType::$n(elements);
203+
let obj = PyObject::new(
204+
PyArray {
205+
array: PyRwLock::new(sliced)
206+
},
207+
PyArray::class(vm),
208+
None
209+
);
210+
Ok(obj)
211+
})*
212+
}
213+
}
214+
197215
fn getitem(&self, needle: Either<isize, PySliceRef>, vm: &VirtualMachine) -> PyResult {
198216
match needle {
199217
Either::A(i) => {
200218
self.idx(i, "array", vm).map(|i| {
201219
self.getitem_by_idx(i, vm).unwrap()
202220
})
203221
}
204-
Either::B(_slice) => Err(vm.new_not_implemented_error("array slice is not implemented".to_owned())),
222+
Either::B(slice) => self.getitem_by_slice(slice, vm),
205223
}
206224
}
207225

0 commit comments

Comments
 (0)