py: Fix builtin ord so that it can handle bytes values >= 0x80.
Addresses issue #1188.
This commit is contained in:
parent
404b68da88
commit
d8cbbcaa9d
@ -351,9 +351,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
|
|||||||
mp_uint_t len;
|
mp_uint_t len;
|
||||||
const char *str = mp_obj_str_get_data(o_in, &len);
|
const char *str = mp_obj_str_get_data(o_in, &len);
|
||||||
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
||||||
len = unichar_charlen(str, len);
|
if (MP_OBJ_IS_STR(o_in)) {
|
||||||
if (len == 1) {
|
len = unichar_charlen(str, len);
|
||||||
if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
|
if (len == 1) {
|
||||||
|
if (!UTF8_IS_NONASCII(*str)) {
|
||||||
|
goto return_first_byte;
|
||||||
|
}
|
||||||
mp_int_t ord = *str++ & 0x7F;
|
mp_int_t ord = *str++ & 0x7F;
|
||||||
for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
|
for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
|
||||||
ord &= ~mask;
|
ord &= ~mask;
|
||||||
@ -362,8 +365,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
|
|||||||
ord = (ord << 6) | (*str++ & 0x3F);
|
ord = (ord << 6) | (*str++ & 0x3F);
|
||||||
}
|
}
|
||||||
return mp_obj_new_int(ord);
|
return mp_obj_new_int(ord);
|
||||||
} else {
|
}
|
||||||
return mp_obj_new_int(((const byte*)str)[0]);
|
} else {
|
||||||
|
// a bytes object
|
||||||
|
if (len == 1) {
|
||||||
|
return_first_byte:
|
||||||
|
return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
@ -7,3 +7,16 @@ try:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
print("TypeError")
|
print("TypeError")
|
||||||
|
|
||||||
|
# bytes also work in ord
|
||||||
|
|
||||||
|
print(ord(b'a'))
|
||||||
|
print(ord(b'\x00'))
|
||||||
|
print(ord(b'\x01'))
|
||||||
|
print(ord(b'\x7f'))
|
||||||
|
print(ord(b'\x80'))
|
||||||
|
print(ord(b'\xff'))
|
||||||
|
|
||||||
|
try:
|
||||||
|
ord(b'')
|
||||||
|
except TypeError:
|
||||||
|
print("TypeError")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user