From 187afa4e4a7efc08c07e60ccdb5ebc1a229776e4 Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sun, 30 Aug 2020 19:28:26 +0200 Subject: [PATCH 1/3] Implement array __repr__ --- vm/src/stdlib/array.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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(); From 4d888e15c107964cac3dfb07d94ec367aa7aec2e Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sun, 30 Aug 2020 21:27:37 +0200 Subject: [PATCH 2/3] Remove expectedFailure from array test_repr --- Lib/test/test_array.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 9a121d5f1..a50d1d516 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -497,8 +497,6 @@ class BaseTest: b = array.array(self.typecode, a) self.assertEqual(a, b) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_repr(self): a = array.array(self.typecode, 2*self.example) self.assertEqual(a, eval(repr(a), {"array": array.array})) From 6068ab13142f6a1c98221228e111f5ad1e573ed8 Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sun, 30 Aug 2020 21:32:58 +0200 Subject: [PATCH 3/3] Replace map->join with Itertools::format --- vm/src/stdlib/array.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index 0ff310d40..6d0985776 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -243,7 +243,7 @@ macro_rules! def_array_enum { if v.is_empty() { format!("array('{}')", $c) } else { - format!("array('{}', [{}])", $c, v.iter().map(|x| x.to_string()).join(", ")) + format!("array('{}', [{}])", $c, v.iter().format(", ")) } })* };