Add range.__eq__

This commit is contained in:
Jeong YunWon
2019-04-27 00:27:47 +09:00
parent 16353f4f54
commit a7091ca88d
2 changed files with 15 additions and 3 deletions

View File

@@ -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<PyInt> {
if let Ok(int) = needle.downcast::<PyInt>() {
match self.index_of(int.as_bigint()) {