Skip to content

Commit 12dd8df

Browse files
committed
py/objstr: Binary type of str/bytes for buffer protocol is 'B'.
The type is an unsigned 8-bit value, since bytes objects are exactly that. And it's also sensible for unicode strings to return unsigned values when accessed in a byte-wise manner (CPython does not allow this).
1 parent 2724bd4 commit 12dd8df

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u
18061806
GET_STR_DATA_LEN(self_in, str_data, str_len);
18071807
bufinfo->buf = (void*)str_data;
18081808
bufinfo->len = str_len;
1809-
bufinfo->typecode = 'b';
1809+
bufinfo->typecode = 'B'; // bytes should be unsigned, so should unicode byte-access
18101810
return 0;
18111811
} else {
18121812
// can't write to a string

tests/basics/memoryview2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# test memoryview accessing maximum values for signed/unsigned elements
2+
3+
from array import array
4+
5+
print(list(memoryview(b'\x7f\x80\x81\xff')))
6+
print(list(memoryview(array('b', [0x7f, -0x80]))))
7+
print(list(memoryview(array('B', [0x7f, 0x80, 0x81, 0xff]))))
8+
print(list(memoryview(array('h', [0x7f00, -0x8000]))))
9+
print(list(memoryview(array('H', [0x7f00, 0x8000, 0x8100, 0xffff]))))
10+
print(list(memoryview(array('i', [0x7f000000, -0x80000000]))))
11+
print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff]))))

0 commit comments

Comments
 (0)