Skip to content

Commit 2cd634f

Browse files
committed
Implement array delitem
1 parent 3b2a16f commit 2cd634f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Lib/test/test_array.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,6 @@ def test_setitem(self):
704704
-len(self.example)-1, self.example[0]
705705
)
706706

707-
# TODO: RUSTPYTHON
708-
@unittest.expectedFailure
709707
def test_delitem(self):
710708
a = array.array(self.typecode, self.example)
711709
del a[0]

vm/src/stdlib/array.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ macro_rules! def_array_enum {
335335
Ok(())
336336
}
337337

338+
fn delitem_by_idx(&mut self, i: isize, vm: &VirtualMachine) -> PyResult<()> {
339+
let i = self.idx(i, "array assignment", vm)?;
340+
match self {
341+
$(ArrayContentType::$n(v) => { v.remove(i); },)*
342+
}
343+
Ok(())
344+
}
345+
346+
fn delitem_by_slice(&mut self, slice: PySliceRef, vm: &VirtualMachine) -> PyResult<()> {
347+
Err(vm.new_not_implemented_error("FIX NOW".to_owned()))
348+
}
349+
338350
fn repr(&self, _vm: &VirtualMachine) -> PyResult<String> {
339351
// we don't need ReprGuard here
340352
let s = match self {
@@ -580,6 +592,14 @@ impl PyArray {
580592
}
581593
}
582594

595+
#[pymethod(name = "__delitem__")]
596+
fn delitem(&self, needle: Either<isize, PySliceRef>, vm: &VirtualMachine) -> PyResult<()> {
597+
match needle {
598+
Either::A(i) => self.borrow_value_mut().delitem_by_idx(i, vm),
599+
Either::B(slice) => self.borrow_value_mut().delitem_by_slice(slice, vm)
600+
}
601+
}
602+
583603
#[pymethod(name = "__repr__")]
584604
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
585605
zelf.borrow_value().repr(vm)

0 commit comments

Comments
 (0)