Skip to content

Commit 70328e4

Browse files
committed
py: Implement more complete bytes comparison handling.
1 parent ad3baec commit 70328e4

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,17 @@ STATIC mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
328328
GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len);
329329
return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, rhs_data, rhs_len));
330330
}
331+
if (lhs_type == &mp_type_bytes) {
332+
mp_buffer_info_t bufinfo;
333+
if (!mp_get_buffer(rhs_in, &bufinfo, MP_BUFFER_READ)) {
334+
goto uncomparable;
335+
}
336+
return MP_BOOL(mp_seq_cmp_bytes(op, lhs_data, lhs_len, bufinfo.buf, bufinfo.len));
337+
}
338+
uncomparable:
339+
if (op == MP_BINARY_OP_EQUAL) {
340+
return mp_const_false;
341+
}
331342
}
332343

333344
return MP_OBJ_NOT_SUPPORTED;

tests/basics/bytes_compare2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import array
2+
3+
print(b"1" == 1)
4+
print(b"123" == bytearray(b"123"))
5+
print(b"123" == "123")
6+
# CPyhon gives False here
7+
#print(b"\x01\x02\x03" == array.array("B", [1, 2, 3]))

0 commit comments

Comments
 (0)