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).
pull/2047/head
Damien George 2016-05-07 21:18:17 +01:00
rodzic 2724bd4a94
commit 12dd8df375
2 zmienionych plików z 12 dodań i 1 usunięć

Wyświetl plik

@ -1806,7 +1806,7 @@ mp_int_t mp_obj_str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_u
GET_STR_DATA_LEN(self_in, str_data, str_len);
bufinfo->buf = (void*)str_data;
bufinfo->len = str_len;
bufinfo->typecode = 'b';
bufinfo->typecode = 'B'; // bytes should be unsigned, so should unicode byte-access
return 0;
} else {
// can't write to a string

Wyświetl plik

@ -0,0 +1,11 @@
# test memoryview accessing maximum values for signed/unsigned elements
from array import array
print(list(memoryview(b'\x7f\x80\x81\xff')))
print(list(memoryview(array('b', [0x7f, -0x80]))))
print(list(memoryview(array('B', [0x7f, 0x80, 0x81, 0xff]))))
print(list(memoryview(array('h', [0x7f00, -0x8000]))))
print(list(memoryview(array('H', [0x7f00, 0x8000, 0x8100, 0xffff]))))
print(list(memoryview(array('i', [0x7f000000, -0x80000000]))))
print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff]))))