type->print(): Distinguish str() and repr() variety by passing extra param.
This commit is contained in:
parent
24224d7c72
commit
76d982ef34
10
py/builtin.c
10
py/builtin.c
@ -64,7 +64,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___buil
|
|||||||
|
|
||||||
static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
|
static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
|
||||||
if (o != mp_const_none) {
|
if (o != mp_const_none) {
|
||||||
mp_obj_print(o);
|
mp_obj_print(o, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
@ -285,13 +285,7 @@ static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
if (MP_OBJ_IS_TYPE(args[i], &str_type)) {
|
mp_obj_print(args[i], PRINT_STR);
|
||||||
// special case, print string raw
|
|
||||||
printf("%s", qstr_str(mp_obj_str_get(args[i])));
|
|
||||||
} else {
|
|
||||||
// print the object Python style
|
|
||||||
mp_obj_print(args[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
|
8
py/obj.c
8
py/obj.c
@ -41,21 +41,21 @@ void printf_wrapper(void *env, const char *fmt, ...) {
|
|||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
if (MP_OBJ_IS_SMALL_INT(o_in)) {
|
||||||
print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
|
print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
|
||||||
} else {
|
} else {
|
||||||
mp_obj_base_t *o = o_in;
|
mp_obj_base_t *o = o_in;
|
||||||
if (o->type->print != NULL) {
|
if (o->type->print != NULL) {
|
||||||
o->type->print(print, env, o_in);
|
o->type->print(print, env, o_in, kind);
|
||||||
} else {
|
} else {
|
||||||
print(env, "<%s>", o->type->name);
|
print(env, "<%s>", o->type->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_obj_print(mp_obj_t o_in) {
|
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_print_helper(printf_wrapper, NULL, o_in);
|
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mp_obj_is_callable(mp_obj_t o_in) {
|
bool mp_obj_is_callable(mp_obj_t o_in) {
|
||||||
|
10
py/obj.h
10
py/obj.h
@ -85,7 +85,11 @@ typedef mp_obj_t (*mp_fun_t)(void);
|
|||||||
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
|
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
|
||||||
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t, struct _mp_map_t*);
|
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t, struct _mp_map_t*);
|
||||||
|
|
||||||
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
|
typedef enum {
|
||||||
|
PRINT_STR, PRINT_REPR
|
||||||
|
} mp_print_kind_t;
|
||||||
|
|
||||||
|
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
|
||||||
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
||||||
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
|
||||||
typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array
|
typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array
|
||||||
@ -230,8 +234,8 @@ mp_obj_t mp_obj_new_module(qstr module_name);
|
|||||||
mp_obj_t mp_obj_get_type(mp_obj_t o_in);
|
mp_obj_t mp_obj_get_type(mp_obj_t o_in);
|
||||||
const char *mp_obj_get_type_str(mp_obj_t o_in);
|
const char *mp_obj_get_type_str(mp_obj_t o_in);
|
||||||
|
|
||||||
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);
|
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
|
||||||
void mp_obj_print(mp_obj_t o);
|
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
|
||||||
|
|
||||||
bool mp_obj_is_callable(mp_obj_t o_in);
|
bool mp_obj_is_callable(mp_obj_t o_in);
|
||||||
machine_int_t mp_obj_hash(mp_obj_t o_in);
|
machine_int_t mp_obj_hash(mp_obj_t o_in);
|
||||||
|
@ -13,7 +13,7 @@ typedef struct _mp_obj_bool_t {
|
|||||||
bool value;
|
bool value;
|
||||||
} mp_obj_bool_t;
|
} mp_obj_bool_t;
|
||||||
|
|
||||||
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_bool_t *self = self_in;
|
mp_obj_bool_t *self = self_in;
|
||||||
if (self->value) {
|
if (self->value) {
|
||||||
print(env, "True");
|
print(env, "True");
|
||||||
|
@ -21,7 +21,7 @@ typedef struct _mp_obj_complex_t {
|
|||||||
|
|
||||||
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
|
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
|
||||||
|
|
||||||
void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_complex_t *o = o_in;
|
mp_obj_complex_t *o = o_in;
|
||||||
if (o->real == 0) {
|
if (o->real == 0) {
|
||||||
print(env, "%.8gj", o->imag);
|
print(env, "%.8gj", o->imag);
|
||||||
|
10
py/objdict.c
10
py/objdict.c
@ -20,7 +20,7 @@ typedef struct _mp_obj_dict_t {
|
|||||||
static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
|
static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
|
||||||
static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
|
static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
|
||||||
|
|
||||||
static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_dict_t *self = self_in;
|
mp_obj_dict_t *self = self_in;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
print(env, "{");
|
print(env, "{");
|
||||||
@ -31,9 +31,9 @@ static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env
|
|||||||
print(env, ", ");
|
print(env, ", ");
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
mp_obj_print_helper(print, env, next->key);
|
mp_obj_print_helper(print, env, next->key, PRINT_REPR);
|
||||||
print(env, ": ");
|
print(env, ": ");
|
||||||
mp_obj_print_helper(print, env, next->value);
|
mp_obj_print_helper(print, env, next->value, PRINT_REPR);
|
||||||
}
|
}
|
||||||
print(env, "}");
|
print(env, "}");
|
||||||
}
|
}
|
||||||
@ -350,7 +350,7 @@ static mp_obj_t dict_view_getiter(mp_obj_t view_in) {
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
|
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
|
||||||
mp_obj_dict_view_t *self = self_in;
|
mp_obj_dict_view_t *self = self_in;
|
||||||
bool first = true;
|
bool first = true;
|
||||||
@ -363,7 +363,7 @@ static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
|
|||||||
print(env, ", ");
|
print(env, ", ");
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
mp_obj_print_helper(print, env, next);
|
mp_obj_print_helper(print, env, next, PRINT_REPR);
|
||||||
}
|
}
|
||||||
print(env, "])");
|
print(env, "])");
|
||||||
}
|
}
|
||||||
|
@ -21,13 +21,25 @@ typedef struct mp_obj_exception_t {
|
|||||||
mp_obj_tuple_t args;
|
mp_obj_tuple_t args;
|
||||||
} mp_obj_exception_t;
|
} mp_obj_exception_t;
|
||||||
|
|
||||||
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_exception_t *o = o_in;
|
mp_obj_exception_t *o = o_in;
|
||||||
if (o->msg != 0) {
|
if (o->msg != 0) {
|
||||||
print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg));
|
print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg));
|
||||||
} else {
|
} else {
|
||||||
|
// Yes, that's how CPython has it
|
||||||
|
if (kind == PRINT_REPR) {
|
||||||
print(env, "%s", qstr_str(o->id));
|
print(env, "%s", qstr_str(o->id));
|
||||||
tuple_print(print, env, &o->args);
|
}
|
||||||
|
if (kind == PRINT_STR) {
|
||||||
|
if (o->args.len == 0) {
|
||||||
|
print(env, "");
|
||||||
|
return;
|
||||||
|
} else if (o->args.len == 1) {
|
||||||
|
mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tuple_print(print, env, &o->args, kind);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ typedef struct _mp_obj_float_t {
|
|||||||
|
|
||||||
mp_obj_t mp_obj_new_float(mp_float_t value);
|
mp_obj_t mp_obj_new_float(mp_float_t value);
|
||||||
|
|
||||||
static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_float_t *o = o_in;
|
mp_obj_float_t *o = o_in;
|
||||||
print(env, "%.8g", o->value);
|
print(env, "%.8g", o->value);
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ typedef struct _mp_obj_gen_instance_t {
|
|||||||
mp_obj_t state[];
|
mp_obj_t state[];
|
||||||
} mp_obj_gen_instance_t;
|
} mp_obj_gen_instance_t;
|
||||||
|
|
||||||
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
print(env, "<generator object 'fun-name' at %p>", self_in);
|
print(env, "<generator object 'fun-name' at %p>", self_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ const mp_obj_type_t int_type = {
|
|||||||
|
|
||||||
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
||||||
// This is called only for non-SMALL_INT
|
// This is called only for non-SMALL_INT
|
||||||
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is called only for non-SMALL_INT
|
// This is called only for non-SMALL_INT
|
||||||
|
@ -5,5 +5,5 @@ typedef struct _mp_obj_int_t {
|
|||||||
#endif
|
#endif
|
||||||
} mp_obj_int_t;
|
} mp_obj_int_t;
|
||||||
|
|
||||||
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
|
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
|
||||||
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
||||||
|
@ -26,14 +26,14 @@ static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* list */
|
/* list */
|
||||||
|
|
||||||
static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_list_t *o = o_in;
|
mp_obj_list_t *o = o_in;
|
||||||
print(env, "[");
|
print(env, "[");
|
||||||
for (int i = 0; i < o->len; i++) {
|
for (int i = 0; i < o->len; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
print(env, ", ");
|
print(env, ", ");
|
||||||
}
|
}
|
||||||
mp_obj_print_helper(print, env, o->items[i]);
|
mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
|
||||||
}
|
}
|
||||||
print(env, "]");
|
print(env, "]");
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ typedef struct _mp_obj_module_t {
|
|||||||
mp_map_t *globals;
|
mp_map_t *globals;
|
||||||
} mp_obj_module_t;
|
} mp_obj_module_t;
|
||||||
|
|
||||||
static void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_module_t *self = self_in;
|
mp_obj_module_t *self = self_in;
|
||||||
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
|
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ typedef struct _mp_obj_none_t {
|
|||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
} mp_obj_none_t;
|
} mp_obj_none_t;
|
||||||
|
|
||||||
void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
print(env, "None");
|
print(env, "None");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ typedef struct _mp_obj_set_it_t {
|
|||||||
|
|
||||||
static mp_obj_t set_it_iternext(mp_obj_t self_in);
|
static mp_obj_t set_it_iternext(mp_obj_t self_in);
|
||||||
|
|
||||||
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_set_t *self = self_in;
|
mp_obj_set_t *self = self_in;
|
||||||
if (self->set.used == 0) {
|
if (self->set.used == 0) {
|
||||||
print(env, "set()");
|
print(env, "set()");
|
||||||
@ -39,7 +39,7 @@ void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
|
|||||||
print(env, ", ");
|
print(env, ", ");
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
mp_obj_print_helper(print, env, self->set.table[i]);
|
mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print(env, "}");
|
print(env, "}");
|
||||||
|
@ -16,7 +16,7 @@ typedef struct _mp_obj_ellipsis_t {
|
|||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
} mp_obj_ellipsis_t;
|
} mp_obj_ellipsis_t;
|
||||||
|
|
||||||
void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
print(env, "Ellipsis");
|
print(env, "Ellipsis");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ typedef struct _mp_obj_slice_t {
|
|||||||
machine_int_t stop;
|
machine_int_t stop;
|
||||||
} mp_obj_slice_t;
|
} mp_obj_slice_t;
|
||||||
|
|
||||||
void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
void slice_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_slice_t *o = o_in;
|
mp_obj_slice_t *o = o_in;
|
||||||
print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop);
|
print(env, "slice(" INT_FMT ", " INT_FMT ")", o->start, o->stop);
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,15 @@ static mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur);
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* str */
|
/* str */
|
||||||
|
|
||||||
void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void str_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_str_t *self = self_in;
|
mp_obj_str_t *self = self_in;
|
||||||
|
if (kind == PRINT_STR) {
|
||||||
|
print(env, "%s", qstr_str(self->qstr));
|
||||||
|
} else {
|
||||||
// TODO need to escape chars etc
|
// TODO need to escape chars etc
|
||||||
print(env, "'%s'", qstr_str(self->qstr));
|
print(env, "'%s'", qstr_str(self->qstr));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||||
mp_obj_str_t *lhs = lhs_in;
|
mp_obj_str_t *lhs = lhs_in;
|
||||||
@ -277,7 +281,8 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
|
|||||||
if (arg_i >= n_args) {
|
if (arg_i >= n_args) {
|
||||||
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
|
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "tuple index out of range"));
|
||||||
}
|
}
|
||||||
mp_obj_print_helper(vstr_printf_wrapper, vstr, args[arg_i]);
|
// TODO: may be PRINT_REPR depending on formatting code
|
||||||
|
mp_obj_print_helper(vstr_printf_wrapper, vstr, args[arg_i], PRINT_STR);
|
||||||
arg_i++;
|
arg_i++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,14 +16,14 @@ static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur);
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* tuple */
|
/* tuple */
|
||||||
|
|
||||||
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
|
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_tuple_t *o = o_in;
|
mp_obj_tuple_t *o = o_in;
|
||||||
print(env, "(");
|
print(env, "(");
|
||||||
for (int i = 0; i < o->len; i++) {
|
for (int i = 0; i < o->len; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
print(env, ", ");
|
print(env, ", ");
|
||||||
}
|
}
|
||||||
mp_obj_print_helper(print, env, o->items[i]);
|
mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
|
||||||
}
|
}
|
||||||
if (o->len == 1) {
|
if (o->len == 1) {
|
||||||
print(env, ",");
|
print(env, ",");
|
||||||
|
@ -4,4 +4,4 @@ typedef struct _mp_obj_tuple_t {
|
|||||||
mp_obj_t items[];
|
mp_obj_t items[];
|
||||||
} mp_obj_tuple_t;
|
} mp_obj_tuple_t;
|
||||||
|
|
||||||
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);
|
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
|
||||||
|
@ -66,7 +66,7 @@ static mp_map_elem_t *mp_obj_class_lookup(const mp_obj_type_t *type, qstr attr,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
print(env, "<%s object at %p>", mp_obj_get_type_str(self_in), self_in);
|
print(env, "<%s object at %p>", mp_obj_get_type_str(self_in), self_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +148,7 @@ static bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
|
|||||||
// - there is a constant mp_obj_type_t (called mp_const_type) for the 'type' object
|
// - there is a constant mp_obj_type_t (called mp_const_type) for the 'type' object
|
||||||
// - creating a new class (a new type) creates a new mp_obj_type_t
|
// - creating a new class (a new type) creates a new mp_obj_type_t
|
||||||
|
|
||||||
static void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void type_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_type_t *self = self_in;
|
mp_obj_type_t *self = self_in;
|
||||||
print(env, "<class '%s'>", self->name);
|
print(env, "<class '%s'>", self->name);
|
||||||
}
|
}
|
||||||
|
10
stm/main.c
10
stm/main.c
@ -436,7 +436,7 @@ void do_repl(void) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -474,7 +474,7 @@ bool do_file(const char *filename) {
|
|||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -656,7 +656,7 @@ typedef struct _pyb_file_obj_t {
|
|||||||
FIL fp;
|
FIL fp;
|
||||||
} pyb_file_obj_t;
|
} pyb_file_obj_t;
|
||||||
|
|
||||||
void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
printf("<file %p>", self_in);
|
printf("<file %p>", self_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1097,13 +1097,13 @@ soft_reset:
|
|||||||
if (nlr_push(&nlr) == 0) {
|
if (nlr_push(&nlr) == 0) {
|
||||||
mp_obj_t ret = rt_call_function_0(module_fun);
|
mp_obj_t ret = rt_call_function_0(module_fun);
|
||||||
printf("done! got: ");
|
printf("done! got: ");
|
||||||
mp_obj_print(ret);
|
mp_obj_print(ret, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
nlr_pop();
|
nlr_pop();
|
||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
printf("exception: ");
|
printf("exception: ");
|
||||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ void timer_interrupt(void) {
|
|||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
printf("exception in timer interrupt\n");
|
printf("exception in timer interrupt\n");
|
||||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
# TODO: requires repr()
|
print(repr(IndexError()))
|
||||||
#a = IndexError(1, "test", [100, 200])
|
print(str(IndexError()))
|
||||||
#print(repr(a))
|
|
||||||
|
print(repr(IndexError("foo")))
|
||||||
|
print(str(IndexError("foo")))
|
||||||
|
|
||||||
|
a = IndexError(1, "test", [100, 200])
|
||||||
|
print(str(a))
|
||||||
|
print(repr(a))
|
||||||
|
@ -15,7 +15,7 @@ typedef struct _mp_obj_fdfile_t {
|
|||||||
int fd;
|
int fd;
|
||||||
} mp_obj_fdfile_t;
|
} mp_obj_fdfile_t;
|
||||||
|
|
||||||
static void fdfile_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void fdfile_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
mp_obj_fdfile_t *self = self_in;
|
mp_obj_fdfile_t *self = self_in;
|
||||||
print(env, "<io.FileIO %d>", self->fd);
|
print(env, "<io.FileIO %d>", self->fd);
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
|
|||||||
nlr_pop();
|
nlr_pop();
|
||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
mp_obj_print((mp_obj_t)nlr.ret_val);
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,7 +159,7 @@ typedef struct _test_obj_t {
|
|||||||
int value;
|
int value;
|
||||||
} test_obj_t;
|
} test_obj_t;
|
||||||
|
|
||||||
static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
|
static void test_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||||
test_obj_t *self = self_in;
|
test_obj_t *self = self_in;
|
||||||
print(env, "<test %d>", self->value);
|
print(env, "<test %d>", self->value);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user