From 58051117322bc7cd55c0d35168c0563a880c7689 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 15 Apr 2014 12:42:52 +0100 Subject: [PATCH] py: Add hex builtin function. A one-liner, added especially for @pfalcon :) --- py/builtin.c | 6 ++++++ py/builtin.h | 1 + py/builtintables.c | 1 + py/makeqstrdata.py | 1 + py/qstrdefs.h | 2 ++ 5 files changed, 11 insertions(+) diff --git a/py/builtin.c b/py/builtin.c index de1a8fc6a..b02608b40 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -207,6 +207,12 @@ STATIC mp_obj_t mp_builtin_hash(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hash_obj, mp_builtin_hash); +STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) { + return mp_binary_op(MP_BINARY_OP_MODULO, MP_OBJ_NEW_QSTR(MP_QSTR__percent_x), o_in); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); + STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) { return mp_getiter(o_in); } diff --git a/py/builtin.h b/py/builtin.h index caca71b52..51a74a310 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -15,6 +15,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_exec_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_getattr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hex_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_id_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_isinstance_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_issubclass_obj); diff --git a/py/builtintables.c b/py/builtintables.c index cbf885844..6cf12dffa 100644 --- a/py/builtintables.c +++ b/py/builtintables.c @@ -60,6 +60,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_hex), (mp_obj_t)&mp_builtin_hex_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_id), (mp_obj_t)&mp_builtin_id_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_isinstance), (mp_obj_t)&mp_builtin_isinstance_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_issubclass), (mp_obj_t)&mp_builtin_issubclass_obj }, diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index eaff11bd0..f8605ae09 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -16,6 +16,7 @@ codepoint2name[ord('-')] = 'hyphen'; codepoint2name[ord('.')] = 'dot' codepoint2name[ord(':')] = 'colon' codepoint2name[ord('/')] = 'slash' +codepoint2name[ord('%')] = 'percent' # this must match the equivalent function in qstr.c def compute_hash(qstr): diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 80cf681ae..e9f2d8d9a 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -106,6 +106,8 @@ Q(from_bytes) Q(getattr) Q(globals) Q(hash) +Q(hex) +Q(%x) Q(id) Q(io) Q(int)