diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index d01773e9d..0ff310d40 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -236,6 +236,20 @@ macro_rules! def_array_enum { } } + fn repr(&self, _vm: &VirtualMachine) -> PyResult { + // we don't need ReprGuard here + let s = match self { + $(ArrayContentType::$n(v) => { + if v.is_empty() { + format!("array('{}')", $c) + } else { + format!("array('{}', [{}])", $c, v.iter().map(|x| x.to_string()).join(", ")) + } + })* + }; + Ok(s) + } + fn iter<'a>(&'a self, vm: &'a VirtualMachine) -> impl Iterator + 'a { let mut i = 0; std::iter::from_fn(move || { @@ -443,6 +457,11 @@ impl PyArray { self.borrow_value_mut().setitem(needle, obj, vm) } + #[pymethod(name = "__repr__")] + fn repr(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + zelf.borrow_value().repr(vm) + } + #[pymethod(name = "__eq__")] fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { let lhs = self.borrow_value();