From ab4aeece96bd7e30a06653c17049c97ed3918204 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Sun, 18 Aug 2019 22:56:41 -0500 Subject: [PATCH] Add array.__getitem__ --- vm/src/stdlib/array.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index 99a92a036..170405887 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -124,6 +124,12 @@ macro_rules! def_array_enum { $(ArrayContentType::$n(v) => v.reverse(),)* } } + + fn getitem(&self, i: usize, vm: &VirtualMachine) -> Option { + 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 {