From a7091ca88da3f9ee4fcd38ea033c2210c1e1dfb3 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 27 Apr 2019 00:27:47 +0900 Subject: [PATCH] Add range.__eq__ --- tests/snippets/builtin_range.py | 4 ++-- vm/src/obj/objrange.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/snippets/builtin_range.py b/tests/snippets/builtin_range.py index b087f7add..a8797f75a 100644 --- a/tests/snippets/builtin_range.py +++ b/tests/snippets/builtin_range.py @@ -58,5 +58,5 @@ assert range(i).stop is i # negative index assert range(10)[-1] == 9 assert_raises(IndexError, lambda: range(10)[-11], 'out of bound') -assert str(range(10)[-2:4]) == str(range(8, 4)) -assert str(range(10)[-6:-2]) == str(range(4, 8)) +assert range(10)[-2:4] == range(8, 4) +assert range(10)[-6:-2] == range(4, 8) diff --git a/vm/src/obj/objrange.rs b/vm/src/obj/objrange.rs index abe82ec3b..d98e7491f 100644 --- a/vm/src/obj/objrange.rs +++ b/vm/src/obj/objrange.rs @@ -13,7 +13,7 @@ use crate::vm::VirtualMachine; use super::objint::{PyInt, PyIntRef}; use super::objiter; use super::objslice::{PySlice, PySliceRef}; -use super::objtype::PyClassRef; +use super::objtype::{self, PyClassRef}; #[derive(Debug, Clone)] pub struct PyRange { @@ -110,6 +110,7 @@ pub fn init(context: &PyContext) { "__bool__" => context.new_rustfunc(PyRange::bool), "__contains__" => context.new_rustfunc(PyRange::contains), "__doc__" => context.new_str(range_doc.to_string()), + "__eq__" => context.new_rustfunc(PyRange::eq), "__getitem__" => context.new_rustfunc(PyRange::getitem), "__iter__" => context.new_rustfunc(PyRange::iter), "__len__" => context.new_rustfunc(PyRange::len), @@ -246,6 +247,17 @@ impl PyRange { } } + fn eq(&self, rhs: PyObjectRef, vm: &VirtualMachine) -> bool { + if objtype::isinstance(&rhs, &vm.ctx.range_type()) { + let rhs = get_value(&rhs); + self.start.as_bigint() == rhs.start.as_bigint() + && self.stop.as_bigint() == rhs.stop.as_bigint() + && self.step.as_bigint() == rhs.step.as_bigint() + } else { + false + } + } + fn index(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult { if let Ok(int) = needle.downcast::() { match self.index_of(int.as_bigint()) {