From 36c4499d36a36ab3e1a68545e613bce61fb15f3c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 13 Jan 2014 19:20:46 +0200 Subject: Implement str() and repr() builtin functions. --- py/builtin.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'py/builtin.c') diff --git a/py/builtin.c b/py/builtin.c index a45b1463d..bc10f29d7 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -338,3 +338,19 @@ 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); + +mp_obj_t mp_builtin_str(mp_obj_t o) { + vstr_t *vstr = vstr_new(); + mp_obj_print_helper(vstr_printf_wrapper, vstr, o, PRINT_STR); + return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc)); +} + +MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str); + +mp_obj_t mp_builtin_repr(mp_obj_t o) { + vstr_t *vstr = vstr_new(); + mp_obj_print_helper(vstr_printf_wrapper, vstr, o, PRINT_REPR); + 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); -- cgit v1.2.3