mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Add array.__getitem__
This commit is contained in:
@@ -124,6 +124,12 @@ macro_rules! def_array_enum {
|
||||
$(ArrayContentType::$n(v) => v.reverse(),)*
|
||||
}
|
||||
}
|
||||
|
||||
fn getitem(&self, i: usize, vm: &VirtualMachine) -> Option<PyResult> {
|
||||
match self {
|
||||
$(ArrayContentType::$n(v) => v.get(i).map(|x| x.into_pyobject(vm)),)*
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -280,6 +286,15 @@ impl PyArray {
|
||||
fn reverse(&self, _vm: &VirtualMachine) {
|
||||
self.array.borrow_mut().reverse()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
fn getitem(&self, i: isize, vm: &VirtualMachine) -> PyResult {
|
||||
let i = self.idx(i, vm)?;
|
||||
self.array
|
||||
.borrow()
|
||||
.getitem(i, vm)
|
||||
.unwrap_or_else(|| Err(vm.new_index_error("array index out of range".to_owned())))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
Reference in New Issue
Block a user