Skip to content

Commit 06bfefe

Browse files
committed
py/objstr: Fix b"".hex() so it returns an empty str object.
As per CPython behaviour, `b"".hex()` should return an empty str object (not an empty bytes object). Fixes issue micropython#18807. Signed-off-by: Damien George <damien@micropython.org>
1 parent 65ddc23 commit 06bfefe

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type
19951995
// Code below assumes non-zero buffer length when computing size with
19961996
// separator, so handle the zero-length case here.
19971997
if (bufinfo.len == 0) {
1998-
return mp_const_empty_bytes;
1998+
return make_empty_str_of_type(type);
19991999
}
20002000

20012001
vstr_t vstr;

tests/basics/builtin_str_hex.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
raise SystemExit
44

55
for x in (
6+
b"",
67
b"\x00\x01\x02\x03\x04\x05\x06\x07",
78
b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
89
b"\x7f\x80\xff",

0 commit comments

Comments
 (0)