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
43 changes: 22 additions & 21 deletions crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
class::PyClassImpl,
common::{hash, lock::LazyLock},
convert::ToPyObject,
function::{ArgMapping, OptionalArg, PyArithmeticValue, PyComparisonValue},
function::{ArgMapping, Either, OptionalArg, PyComparisonValue},
object::{Traverse, TraverseFn},
protocol::{PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::{
AsMapping, AsNumber, AsSequence, Comparable, Constructor, Hashable, Iterable,
PyComparisonOp, Representable,
AsMapping, AsNumber, AsSequence, Constructor, Hashable, Iterable, PyComparisonOp,
Representable,
},
};
use rustpython_common::wtf8::{Wtf8Buf, wtf8_concat};
Expand Down Expand Up @@ -175,12 +175,30 @@ impl PyMappingProxy {
Iterable,
Constructor,
AsSequence,
Comparable,
Hashable,
AsNumber,
Representable
))]
impl Py<PyMappingProxy> {
#[pyslot]
fn slot_richcompare(
zelf: &PyObject,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<Either<PyObjectRef, PyComparisonValue>> {
let zelf = zelf.downcast_ref::<PyMappingProxy>().ok_or_else(|| {
vm.new_type_error(format!(
"unexpected payload for {}",
op.method_name(&vm.ctx).as_str()
))
})?;
let obj = zelf.to_object(vm)?;
// CPython parity (Objects/descrobject.c::mappingproxy_richcompare):
// delegate to PyObject_RichCompare on the underlying mapping.
obj.rich_compare(other.to_owned(), op, vm).map(Either::A)
}

#[pymethod]
fn get(
&self,
Expand Down Expand Up @@ -238,23 +256,6 @@ impl Py<PyMappingProxy> {
}
}

impl Comparable for PyMappingProxy {
fn cmp(
zelf: &Py<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
let obj = zelf.to_object(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()
}
}

impl Hashable for PyMappingProxy {
#[inline]
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<hash::PyHash> {
Expand Down
49 changes: 49 additions & 0 deletions extra_tests/snippets/builtin_mappingproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,52 @@ def b():
assert A.__dict__.get("not here", "default") == "default"
assert A.__dict__.get("a", "default") is A.a
assert A.__dict__.get("not here") is None


# A mappingproxy must preserve arbitrary rich-comparison results unchanged.
def check_mappingproxy_comparison_results():
import operator
from types import MappingProxyType

class Result:
def __bool__(self):
raise AssertionError("comparison result must not be coerced to bool")

result = Result()
calls = []

def compare(name):
def method(self, other):
calls.append(name)
return result

return method

class Compared:
__eq__ = compare("eq")
__ne__ = compare("ne")
__lt__ = compare("lt")
__le__ = compare("le")
__gt__ = compare("gt")
__ge__ = compare("ge")

class CustomMapping(Compared, dict):
pass

for operation, reflected in (
(operator.eq, "eq"),
(operator.ne, "ne"),
(operator.lt, "gt"),
(operator.le, "ge"),
(operator.gt, "lt"),
(operator.ge, "le"),
):
calls.clear()
assert operation(MappingProxyType(CustomMapping()), {}) is result
assert calls == [operation.__name__], calls
calls.clear()
assert operation(MappingProxyType({}), Compared()) is result
assert calls == [reflected], calls


check_mappingproxy_comparison_results()
Loading