Skip to content

Commit 9b76dc3

Browse files
authored
Merge pull request RustPython#2357 from hyperbora/fix-memoryview-compare
fix memoryview compare
2 parents c9a332c + 928fbcd commit 9b76dc3

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Lib/test/test_memoryview.py

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ def test_tolist(self):
150150
l = m.tolist()
151151
self.assertEqual(l, list(b"abcdef"))
152152

153-
# TODO: RUSTPYTHON
154-
@unittest.expectedFailure
155153
def test_compare(self):
156154
# memoryviews can compare for equality with other objects
157155
# having the buffer interface.

vm/src/builtins/bytes.rs

+10
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ impl Comparable for PyBytes {
531531
) -> PyResult<PyComparisonValue> {
532532
Ok(if let Some(res) = op.identical_optimization(zelf, other) {
533533
res.into()
534+
} else if other.isinstance(&vm.ctx.types.memoryview_type)
535+
&& op != PyComparisonOp::Eq
536+
&& op != PyComparisonOp::Ne
537+
{
538+
return Err(vm.new_type_error(format!(
539+
"'{}' not supported between instances of '{}' and '{}'",
540+
op.operator_token(),
541+
zelf.class().name,
542+
other.class().name
543+
)));
534544
} else {
535545
zelf.inner.cmp(other, op, vm)
536546
})

vm/src/builtins/memory.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,10 @@ impl PyMemoryView {
711711
return Ok(false);
712712
}
713713

714-
let other = try_buffer_from_object(vm, other)?;
714+
let other = match try_buffer_from_object(vm, other) {
715+
Ok(buf) => buf,
716+
Err(_) => return Ok(false),
717+
};
715718

716719
let a_options = &zelf.options;
717720
let b_options = other.get_options();

0 commit comments

Comments
 (0)