Skip to content
Snippets Groups Projects
Commit dfa563c7 authored by Damien George's avatar Damien George
Browse files

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 #3334.
parent a3dc1b19
Branches
No related tags found
No related merge requests found
...@@ -1973,8 +1973,8 @@ const mp_obj_type_t mp_type_bytes = { ...@@ -1973,8 +1973,8 @@ const mp_obj_type_t mp_type_bytes = {
.locals_dict = (mp_obj_dict_t*)&str8_locals_dict, .locals_dict = (mp_obj_dict_t*)&str8_locals_dict,
}; };
// the zero-length bytes // The zero-length bytes object, with data that includes a null-terminating byte
const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, NULL}; const mp_obj_str_t mp_const_empty_bytes_obj = {{&mp_type_bytes}, 0, 0, (const byte*)""};
// Create a str/bytes object using the given data. New memory is allocated and // Create a str/bytes object using the given data. New memory is allocated and
// the data is copied across. // the data is copied across.
......
...@@ -8,6 +8,9 @@ print(b'\u1234') ...@@ -8,6 +8,9 @@ print(b'\u1234')
print(bytes()) print(bytes())
print(bytes(b'abc')) print(bytes(b'abc'))
# make sure empty bytes is converted correctly
print(str(bytes(), 'utf-8'))
a = b"123" a = b"123"
print(a) print(a)
print(str(a)) print(str(a))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment