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 #18807.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2026-02-12 12:39:29 +11:00
parent 65ddc2346c
commit 06bfefeaee
2 changed files with 2 additions and 1 deletions

View File

@ -1995,7 +1995,7 @@ mp_obj_t mp_obj_bytes_hex(size_t n_args, const mp_obj_t *args, const mp_obj_type
// Code below assumes non-zero buffer length when computing size with
// separator, so handle the zero-length case here.
if (bufinfo.len == 0) {
return mp_const_empty_bytes;
return make_empty_str_of_type(type);
}
vstr_t vstr;

View File

@ -3,6 +3,7 @@ if not hasattr(bytes, "fromhex"):
raise SystemExit
for x in (
b"",
b"\x00\x01\x02\x03\x04\x05\x06\x07",
b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
b"\x7f\x80\xff",