Skip to content

Commit dfa563c

Browse files
committed
py/objstr: Make empty bytes object have a null-terminating byte.
Because a lot of string processing functions assume there is a null terminating byte, so they can work in an efficient way. Fixes issue adafruit#3334.
1 parent a3dc1b1 commit dfa563c

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,8 @@ const mp_obj_type_t mp_type_bytes = {
19731973
.locals_dict = (mp_obj_dict_t*)&str8_locals_dict,
19741974
};
19751975

1976-
// the zero-length bytes
1977-
const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL};
1976+
// The zero-length bytes object, with data that includes a null-terminating byte
1977+
const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, (const byte*)""};
19781978

19791979
// Create a str/bytes object using the given data. New memory is allocated and
19801980
// the data is copied across.

tests/basics/bytes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
print(bytes())
99
print(bytes(b'abc'))
1010

11+
# make sure empty bytes is converted correctly
12+
print(str(bytes(), 'utf-8'))
13+
1114
a = b"123"
1215
print(a)
1316
print(str(a))

0 commit comments

Comments
 (0)