diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 582b2f749..37588ddb2 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -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), } }; } diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index 752ec4846..3ea41da7a 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -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 { Ok(format!( @@ -379,6 +377,19 @@ impl PyBoundMethod { fn module(&self, vm: &VirtualMachine) -> Option { vm.get_attribute(self.function.clone(), "__module__").ok() } + + #[pymethod(magic)] + fn eq(&self, other: PyObjectRef) -> PyResult { + 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 { + Ok(self.eq(other)?.map(|v| !v)) + } } impl PyValue for PyBoundMethod { diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index f5091e004..a0485eb7b 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -770,7 +770,7 @@ impl PyArray { fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { // 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 { - 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 { - 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 { - 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 { - 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 {