mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Allow None parameter for remove, index, count method of array
This commit is contained in:
1 parent
d193587184
commit
123eefd87f
2 files changed
+15
-15
No files matched your search
@@ -918,8 +918,6 @@ class BaseTest:
|
||||
del a[start:stop:step]
|
||||
self.assertEqual(a, array.array(self.typecode, L))
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_index(self):
|
||||
example = 2*self.example
|
||||
a = array.array(self.typecode, example)
|
||||
@@ -929,8 +927,6 @@ class BaseTest:
|
||||
self.assertRaises(ValueError, a.index, None)
|
||||
self.assertRaises(ValueError, a.index, self.outside)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_count(self):
|
||||
example = 2*self.example
|
||||
a = array.array(self.typecode, example)
|
||||
@@ -940,8 +936,6 @@ class BaseTest:
|
||||
self.assertEqual(a.count(self.outside), 0)
|
||||
self.assertEqual(a.count(None), 0)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_remove(self):
|
||||
for x in self.example:
|
||||
example = 2*self.example
|
||||
|
||||
+15
-9
@@ -99,8 +99,9 @@ macro_rules! def_array_enum {
|
||||
fn count(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
match self {
|
||||
$(ArrayContentType::$n(v) => {
|
||||
let val = $t::try_from_object(vm, obj)?;
|
||||
Ok(v.iter().filter(|&&a| a == val).count())
|
||||
Ok(<Option<$t>>::try_from_object(vm, obj)?.map_or(0, |val| {
|
||||
v.iter().filter(|&&a| a == val).count()
|
||||
}))
|
||||
})*
|
||||
}
|
||||
}
|
||||
@@ -108,11 +109,15 @@ macro_rules! def_array_enum {
|
||||
fn remove(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()>{
|
||||
match self {
|
||||
$(ArrayContentType::$n(v) => {
|
||||
let val = $t::try_from_object(vm, obj)?;
|
||||
if let Some(pos) = v.iter().position(|&a| a == val) {
|
||||
v.remove(pos);
|
||||
} else {
|
||||
return Err(vm.new_value_error("array.remove(x): x not in array".to_owned()));
|
||||
let pos = <Option<$t>>::try_from_object(vm, obj)?.map_or(None, |val| {
|
||||
v.iter().position(|&a| a == val)
|
||||
});
|
||||
|
||||
match pos {
|
||||
Some(x) => {
|
||||
v.remove(x);
|
||||
},
|
||||
None => return Err(vm.new_value_error("array.remove(x): x not in array".to_owned()))
|
||||
}
|
||||
})*
|
||||
}
|
||||
@@ -147,8 +152,9 @@ macro_rules! def_array_enum {
|
||||
fn index(&self, x: PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<usize>> {
|
||||
match self {
|
||||
$(ArrayContentType::$n(v) => {
|
||||
let val = $t::try_from_object(vm, x)?;
|
||||
Ok(v.iter().position(|&a| a == val))
|
||||
Ok(<Option<$t>>::try_from_object(vm, x)?.map_or(None, |val| {
|
||||
v.iter().position(|&a| a == val)
|
||||
}))
|
||||
})*
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user