summaryrefslogtreecommitdiffstatshomepage
path: root/py/modbuiltins.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-04-19 12:26:46 +0100
committerDamien George <damien.p.george@gmail.com>2015-04-19 12:26:46 +0100
commitd8cbbcaa9d057f210b192bde68fde551972e426c (patch)
treeea3e53359518a1e35539f0c073b89507f92677ca /py/modbuiltins.c
parent404b68da88f9df5f16cce1ff8b778c2b1dc09e56 (diff)
downloadmicropython-d8cbbcaa9d057f210b192bde68fde551972e426c.tar.gz
micropython-d8cbbcaa9d057f210b192bde68fde551972e426c.zip
py: Fix builtin ord so that it can handle bytes values >= 0x80.
Addresses issue #1188.
Diffstat (limited to 'py/modbuiltins.c')
-rw-r--r--py/modbuiltins.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index 877c655d6c..bec47787c8 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -351,9 +351,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
mp_uint_t len;
const char *str = mp_obj_str_get_data(o_in, &len);
#if MICROPY_PY_BUILTINS_STR_UNICODE
- len = unichar_charlen(str, len);
- if (len == 1) {
- if (MP_OBJ_IS_STR(o_in) && UTF8_IS_NONASCII(*str)) {
+ if (MP_OBJ_IS_STR(o_in)) {
+ len = unichar_charlen(str, len);
+ if (len == 1) {
+ if (!UTF8_IS_NONASCII(*str)) {
+ goto return_first_byte;
+ }
mp_int_t ord = *str++ & 0x7F;
for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
ord &= ~mask;
@@ -362,8 +365,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
ord = (ord << 6) | (*str++ & 0x3F);
}
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