mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Implement method.__eq__
This commit is contained in:
3 files changed
+23
-12
No files matched your search
+2
-2
@@ -224,10 +224,10 @@ macro_rules! flame_guard {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! class_or_notimplemented {
|
||||
($vm:expr, $t:ty, $obj:expr) => {
|
||||
($t:ty, $obj:expr) => {
|
||||
match $crate::pyobject::PyObjectRef::downcast::<$t>($obj) {
|
||||
Ok(pyref) => pyref,
|
||||
Err(_) => return Ok(PyArithmaticValue::NotImplemented),
|
||||
Err(_) => return Ok($crate::pyobject::PyArithmaticValue::NotImplemented),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ use crate::obj::objgenerator::PyGenerator;
|
||||
#[cfg(feature = "jit")]
|
||||
use crate::pyobject::IntoPyObject;
|
||||
use crate::pyobject::{
|
||||
BorrowValue, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
TypeProtocol,
|
||||
BorrowValue, IdProtocol, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
|
||||
PyRef, PyResult, PyValue, TypeProtocol,
|
||||
};
|
||||
use crate::scope::Scope;
|
||||
use crate::slots::{SlotCall, SlotDescriptor};
|
||||
@@ -341,14 +341,12 @@ impl SlotCall for PyBoundMethod {
|
||||
}
|
||||
}
|
||||
|
||||
#[pyimpl(with(SlotCall), flags(HAS_DICT))]
|
||||
impl PyBoundMethod {
|
||||
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
|
||||
PyBoundMethod { object, function }
|
||||
}
|
||||
}
|
||||
|
||||
#[pyimpl(with(SlotCall), flags(HAS_DICT))]
|
||||
impl PyBoundMethod {
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
|
||||
Ok(format!(
|
||||
@@ -379,6 +377,19 @@ impl PyBoundMethod {
|
||||
fn module(&self, vm: &VirtualMachine) -> Option<PyObjectRef> {
|
||||
vm.get_attribute(self.function.clone(), "__module__").ok()
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn eq(&self, other: PyObjectRef) -> PyResult<PyComparisonValue> {
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
Ok(PyComparisonValue::Implemented(
|
||||
self.function.is(&other.function) && self.object.is(&other.object),
|
||||
))
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn ne(&self, other: PyObjectRef) -> PyResult<PyComparisonValue> {
|
||||
Ok(self.eq(other)?.map(|v| !v))
|
||||
}
|
||||
}
|
||||
|
||||
impl PyValue for PyBoundMethod {
|
||||
|
||||
@@ -770,7 +770,7 @@ impl PyArray {
|
||||
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
// we cannot use zelf.is(other) for shortcut because if we contenting a
|
||||
// float value NaN we always return False even they are the same object.
|
||||
let other = class_or_notimplemented!(vm, Self, other);
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
if self.len() != other.len() {
|
||||
return Ok(Implemented(false));
|
||||
}
|
||||
@@ -802,7 +802,7 @@ impl PyArray {
|
||||
|
||||
#[pymethod(name = "__lt__")]
|
||||
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let other = class_or_notimplemented!(vm, Self, other);
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
// fast path for same ArrayContentType type
|
||||
if let Ok(ord) = self.borrow_value().cmp(&*other.borrow_value()) {
|
||||
let r = match ord {
|
||||
@@ -816,7 +816,7 @@ impl PyArray {
|
||||
|
||||
#[pymethod(name = "__le__")]
|
||||
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let other = class_or_notimplemented!(vm, Self, other);
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
// fast path for same ArrayContentType type
|
||||
if let Ok(ord) = self.borrow_value().cmp(&*other.borrow_value()) {
|
||||
let r = match ord {
|
||||
@@ -830,7 +830,7 @@ impl PyArray {
|
||||
|
||||
#[pymethod(name = "__gt__")]
|
||||
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let other = class_or_notimplemented!(vm, Self, other);
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
// fast path for same ArrayContentType type
|
||||
if let Ok(ord) = self.borrow_value().cmp(&*other.borrow_value()) {
|
||||
let r = match ord {
|
||||
@@ -844,7 +844,7 @@ impl PyArray {
|
||||
|
||||
#[pymethod(name = "__ge__")]
|
||||
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyComparisonValue> {
|
||||
let other = class_or_notimplemented!(vm, Self, other);
|
||||
let other = class_or_notimplemented!(Self, other);
|
||||
// fast path for same ArrayContentType type
|
||||
if let Ok(ord) = self.borrow_value().cmp(&*other.borrow_value()) {
|
||||
let r = match ord {
|
||||
|
||||
Reference in new issue
Block a user