Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_dictviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ def test_copy(self):
self.assertRaises(TypeError, copy.copy, d.values())
self.assertRaises(TypeError, copy.copy, d.items())

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_compare_error(self):
class Exc(Exception):
pass
Expand Down
11 changes: 7 additions & 4 deletions crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
class::PyClassImpl,
common::{hash, lock::LazyLock},
convert::ToPyObject,
function::{ArgMapping, OptionalArg, PyComparisonValue},
function::{ArgMapping, OptionalArg, PyArithmeticValue, PyComparisonValue},
object::{Traverse, TraverseFn},
protocol::{PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::{
Expand Down Expand Up @@ -211,9 +211,12 @@ impl Comparable for PyMappingProxy {
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
let obj = zelf.to_object(vm)?;
Ok(PyComparisonValue::Implemented(
obj.rich_compare_bool(other, op, vm)?,
))
// CPython parity (Objects/descrobject.c::mappingproxy_richcompare):
// delegate to PyObject_RichCompare on the underlying mapping.
let res = obj.rich_compare(other.to_owned(), op, vm)?;
PyArithmeticValue::from_object(vm, res)
.map(|o| o.try_to_bool(vm))
.transpose()
}
}

Expand Down
11 changes: 7 additions & 4 deletions crates/vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
class::PyClassImpl,
common::hash::PyHash,
function::{OptionalArg, PyComparisonValue, PySetterValue},
function::{OptionalArg, PyArithmeticValue, PyComparisonValue, PySetterValue},
protocol::{PyIter, PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
stdlib::builtins::reversed,
types::{
Expand Down Expand Up @@ -301,9 +301,12 @@ impl Comparable for PyWeakProxy {
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
let obj = zelf.try_upgrade(vm)?;
Ok(PyComparisonValue::Implemented(
obj.rich_compare_bool(other, op, vm)?,
))
// CPython parity (Objects/weakref.c::proxy_richcompare): delegate to
// PyObject_RichCompare on the referent, not the bool variant.
let res = obj.rich_compare(other.to_owned(), op, vm)?;
PyArithmeticValue::from_object(vm, res)
.map(|o| o.try_to_bool(vm))
.transpose()
}
}

Expand Down
21 changes: 14 additions & 7 deletions crates/vm/src/builtins/weakref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::common::{
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
function::{FuncArgs, OptionalArg},
function::{FuncArgs, OptionalArg, PyArithmeticValue, PyComparisonValue},
types::{
Callable, Comparable, Constructor, Hashable, Initializer, PyComparisonOp, Representable,
},
Expand Down Expand Up @@ -127,15 +127,22 @@ impl Comparable for PyWeak {
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<crate::function::PyComparisonValue> {
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
let other = class_or_notimplemented!(Self, other);
let both = zelf.upgrade().and_then(|s| other.upgrade().map(|o| (s, o)));
let eq = match both {
Some((a, b)) => vm.bool_eq(&a, &b)?,
None => zelf.is(other),
};
Ok(eq.into())
match both {
// CPython parity (Objects/weakref.c::weakref_richcompare): use
// PyObject_RichCompare on the referents, not the bool variant,
// so referent __eq__ runs even when referents share identity.
Some((a, b)) => {
let res = a.rich_compare(b, PyComparisonOp::Eq, vm)?;
PyArithmeticValue::from_object(vm, res)
.map(|obj| obj.try_to_bool(vm))
.transpose()
}
None => Ok(zelf.is(other).into()),
}
})
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ impl PyObject {
op_id: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<bool> {
// CPython parity: PyObject_RichCompareBool guarantees identity implies
// equality (and inequality is false on identity), short-circuiting
// before dispatch. Collection membership / equality (e.g. `x in [x]`,
// `[nan] == [nan]`) depend on this even when `__eq__` would raise
// or return False. Only Eq/Ne are decidable from identity; ordering
// ops fall through to `_cmp` because Python does not guarantee
// reflexivity for `<`/`<=`/`>`/`>=`.
if self.is(other) {
match op_id {
PyComparisonOp::Eq => return Ok(true),
PyComparisonOp::Ne => return Ok(false),
_ => {}
}
}
match self._cmp(other, op_id, vm)? {
Either::A(obj) => obj.try_to_bool(vm),
Either::B(other) => Ok(other),
Expand Down
Loading