From 06bfefeaeee0922da4a507c297d45ffe58b7b88b Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Feb 2026 12:39:29 +1100 Subject: [PATCH] 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 --- py/objstr.c | 2 +- tests/basics/builtin_str_hex.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/py/objstr.c b/py/objstr.c index 006deee095..e8b2512a24 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -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; diff --git a/tests/basics/builtin_str_hex.py b/tests/basics/builtin_str_hex.py index 544406cfc5..f77fe7c4fa 100644 --- a/tests/basics/builtin_str_hex.py +++ b/tests/basics/builtin_str_hex.py @@ -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",