From e2fb2baaa48ef6da317a533b5d168b1ab0fcefe8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 15 Jan 2014 21:40:48 +0000 Subject: [PATCH] Implement repr. --- py/builtin.c | 16 ++++++++++++---- py/builtin.h | 1 + py/mpqstrraw.h | 1 + py/runtime.c | 1 + tests/basics/tests/exception1.py | 5 ++--- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/py/builtin.c b/py/builtin.c index 8f93e843b5..837a9b2d09 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -140,10 +140,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable); static mp_obj_t mp_builtin_chr(mp_obj_t o_in) { int ord = mp_obj_get_int(o_in); if (0 <= ord && ord <= 0x10ffff) { - char *str = m_new(char, 2); - str[0] = ord; - str[1] = '\0'; - return mp_obj_new_str(qstr_from_str_take(str, 2)); + char str[2] = {ord, '\0'}; + return mp_obj_new_str(qstr_from_strn_copy(str, 1)); } else { nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "chr() arg not in range(0x110000)")); } @@ -310,6 +308,14 @@ static mp_obj_t mp_builtin_range(int n_args, const mp_obj_t *args) { MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range); +static mp_obj_t mp_builtin_repr(mp_obj_t o_in) { + vstr_t *vstr = vstr_new(); + mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in); + return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc)); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr); + static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { assert(1 <= n_args && n_args <= 2); mp_obj_t value; @@ -324,6 +330,7 @@ static mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } + MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum); static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { @@ -343,4 +350,5 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) { return self; } + MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted); diff --git a/py/builtin.h b/py/builtin.h index 3401528a68..cb8836c48b 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -21,5 +21,6 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj); diff --git a/py/mpqstrraw.h b/py/mpqstrraw.h index 84b24f2748..3ee569aec6 100644 --- a/py/mpqstrraw.h +++ b/py/mpqstrraw.h @@ -57,6 +57,7 @@ Q(ord) Q(pow) Q(print) Q(range) +Q(repr) Q(set) Q(sorted) Q(sum) diff --git a/py/runtime.c b/py/runtime.c index 3d8f0c9a7e..cbdee01dde 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -134,6 +134,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj); + mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj); diff --git a/tests/basics/tests/exception1.py b/tests/basics/tests/exception1.py index 95d933de76..14817fb931 100644 --- a/tests/basics/tests/exception1.py +++ b/tests/basics/tests/exception1.py @@ -1,3 +1,2 @@ -# TODO: requires repr() -#a = IndexError(1, "test", [100, 200]) -#print(repr(a)) +a = IndexError(1, "test", [100, 200]) +print(repr(a))