Skip to content

Commit 123eefd

Browse files
committed
Allow None parameter for remove, index, count method of array
1 parent d193587 commit 123eefd

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

Lib/test/test_array.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,6 @@ def test_extended_set_del_slice(self):
918918
del a[start:stop:step]
919919
self.assertEqual(a, array.array(self.typecode, L))
920920

921-
# TODO: RUSTPYTHON
922-
@unittest.expectedFailure
923921
def test_index(self):
924922
example = 2*self.example
925923
a = array.array(self.typecode, example)
@@ -929,8 +927,6 @@ def test_index(self):
929927
self.assertRaises(ValueError, a.index, None)
930928
self.assertRaises(ValueError, a.index, self.outside)
931929

932-
# TODO: RUSTPYTHON
933-
@unittest.expectedFailure
934930
def test_count(self):
935931
example = 2*self.example
936932
a = array.array(self.typecode, example)
@@ -940,8 +936,6 @@ def test_count(self):
940936
self.assertEqual(a.count(self.outside), 0)
941937
self.assertEqual(a.count(None), 0)
942938

943-
# TODO: RUSTPYTHON
944-
@unittest.expectedFailure
945939
def test_remove(self):
946940
for x in self.example:
947941
example = 2*self.example

vm/src/stdlib/array.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,25 @@ macro_rules! def_array_enum {
9999
fn count(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
100100
match self {
101101
$(ArrayContentType::$n(v) => {
102-
let val = $t::try_from_object(vm, obj)?;
103-
Ok(v.iter().filter(|&&a| a == val).count())
102+
Ok(<Option<$t>>::try_from_object(vm, obj)?.map_or(0, |val| {
103+
v.iter().filter(|&&a| a == val).count()
104+
}))
104105
})*
105106
}
106107
}
107108

108109
fn remove(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()>{
109110
match self {
110111
$(ArrayContentType::$n(v) => {
111-
let val = $t::try_from_object(vm, obj)?;
112-
if let Some(pos) = v.iter().position(|&a| a == val) {
113-
v.remove(pos);
114-
} else {
115-
return Err(vm.new_value_error("array.remove(x): x not in array".to_owned()));
112+
let pos = <Option<$t>>::try_from_object(vm, obj)?.map_or(None, |val| {
113+
v.iter().position(|&a| a == val)
114+
});
115+
116+
match pos {
117+
Some(x) => {
118+
v.remove(x);
119+
},
120+
None => return Err(vm.new_value_error("array.remove(x): x not in array".to_owned()))
116121
}
117122
})*
118123
}
@@ -147,8 +152,9 @@ macro_rules! def_array_enum {
147152
fn index(&self, x: PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<usize>> {
148153
match self {
149154
$(ArrayContentType::$n(v) => {
150-
let val = $t::try_from_object(vm, x)?;
151-
Ok(v.iter().position(|&a| a == val))
155+
Ok(<Option<$t>>::try_from_object(vm, x)?.map_or(None, |val| {
156+
v.iter().position(|&a| a == val)
157+
}))
152158
})*
153159
}
154160
}

0 commit comments

Comments
 (0)