From fee005494024a2bd13aa026b2331348146fb6c54 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 26 Sep 2021 03:17:38 +0900 Subject: [PATCH] Fix array repr --- stdlib/src/array.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/stdlib/src/array.rs b/stdlib/src/array.rs index 262ea06f5..ef9d46880 100644 --- a/stdlib/src/array.rs +++ b/stdlib/src/array.rs @@ -424,14 +424,14 @@ mod array { } } - fn repr(&self, _vm: &VirtualMachine) -> PyResult { + fn repr(&self, class_name: &str, _vm: &VirtualMachine) -> PyResult { // we don't need ReprGuard here let s = match self { $(ArrayContentType::$n(v) => { if v.is_empty() { - format!("array('{}')", $c) + format!("{}('{}')", class_name, $c) } else { - format!("array('{}', [{}])", $c, v.iter().format(", ")) + format!("{}('{}', [{}])", class_name, $c, v.iter().format(", ")) } })* }; @@ -1002,46 +1002,45 @@ mod array { ) -> PyResult> { if zelf.is(&other) { zelf.try_resizable(vm)?.imul(2); - Ok(zelf) } else if let Some(other) = other.payload::() { - let result = zelf.try_resizable(vm)?.iadd(&*other.read(), vm); - result.map(|_| zelf) + zelf.try_resizable(vm)?.iadd(&*other.read(), vm)?; } else { - Err(vm.new_type_error(format!( + return Err(vm.new_type_error(format!( "can only extend array with array (not \"{}\")", other.class().name() - ))) + ))); } + Ok(zelf) } #[pymethod(name = "__rmul__")] #[pymethod(magic)] fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult> { - vm.check_repeat_or_memory_error(self.len(), value) - .map(|value| PyArray::from(self.read().mul(value)).into_ref(vm)) + let value = vm.check_repeat_or_memory_error(self.len(), value)?; + Ok(Self::from(self.read().mul(value)).into_ref(vm)) } #[pymethod(magic)] fn imul(zelf: PyRef, value: isize, vm: &VirtualMachine) -> PyResult> { - vm.check_repeat_or_memory_error(zelf.len(), value) - .and_then(|value| { - zelf.try_resizable(vm)?.imul(value); - Ok(zelf) - }) + let value = vm.check_repeat_or_memory_error(zelf.len(), value)?; + zelf.try_resizable(vm)?.imul(value); + Ok(zelf) } #[pymethod(magic)] fn repr(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + let class_name = zelf.class().name(); if zelf.read().typecode() == 'u' { if zelf.len() == 0 { - return Ok("array('u')".into()); + return Ok(format!("{}('u')", class_name)); } return Ok(format!( - "array('u', {})", + "{}('u', {})", + class_name, PyStr::from(zelf.tounicode(vm)?).repr(vm)? )); } - zelf.read().repr(vm) + zelf.read().repr(&class_name, vm) } #[pymethod(magic)]