Skip to content

Commit 76d982e

Browse files
committed
type->print(): Distinguish str() and repr() variety by passing extra param.
1 parent 24224d7 commit 76d982e

25 files changed

+79
-58
lines changed

py/builtin.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___buil
6464

6565
static mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
6666
if (o != mp_const_none) {
67-
mp_obj_print(o);
67+
mp_obj_print(o, PRINT_REPR);
6868
printf("\n");
6969
}
7070
return mp_const_none;
@@ -285,13 +285,7 @@ static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
285285
if (i > 0) {
286286
printf(" ");
287287
}
288-
if (MP_OBJ_IS_TYPE(args[i], &str_type)) {
289-
// special case, print string raw
290-
printf("%s", qstr_str(mp_obj_str_get(args[i])));
291-
} else {
292-
// print the object Python style
293-
mp_obj_print(args[i]);
294-
}
288+
mp_obj_print(args[i], PRINT_STR);
295289
}
296290
printf("\n");
297291
return mp_const_none;

py/obj.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ void printf_wrapper(void *env, const char *fmt, ...) {
4141
va_end(args);
4242
}
4343

44-
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
44+
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
4545
if (MP_OBJ_IS_SMALL_INT(o_in)) {
4646
print(env, "%d", (int)MP_OBJ_SMALL_INT_VALUE(o_in));
4747
} else {
4848
mp_obj_base_t *o = o_in;
4949
if (o->type->print != NULL) {
50-
o->type->print(print, env, o_in);
50+
o->type->print(print, env, o_in, kind);
5151
} else {
5252
print(env, "<%s>", o->type->name);
5353
}
5454
}
5555
}
5656

57-
void mp_obj_print(mp_obj_t o_in) {
58-
mp_obj_print_helper(printf_wrapper, NULL, o_in);
57+
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
58+
mp_obj_print_helper(printf_wrapper, NULL, o_in, kind);
5959
}
6060

6161
bool mp_obj_is_callable(mp_obj_t o_in) {

py/obj.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ typedef mp_obj_t (*mp_fun_t)(void);
8585
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
8686
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t, struct _mp_map_t*);
8787

88-
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
88+
typedef enum {
89+
PRINT_STR, PRINT_REPR
90+
} mp_print_kind_t;
91+
92+
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
8993
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
9094
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
9195
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);
230234
mp_obj_t mp_obj_get_type(mp_obj_t o_in);
231235
const char *mp_obj_get_type_str(mp_obj_t o_in);
232236

233-
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);
234-
void mp_obj_print(mp_obj_t o);
237+
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
238+
void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
235239

236240
bool mp_obj_is_callable(mp_obj_t o_in);
237241
machine_int_t mp_obj_hash(mp_obj_t o_in);

py/objbool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ typedef struct _mp_obj_bool_t {
1313
bool value;
1414
} mp_obj_bool_t;
1515

16-
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
16+
static void bool_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
1717
mp_obj_bool_t *self = self_in;
1818
if (self->value) {
1919
print(env, "True");

py/objcomplex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct _mp_obj_complex_t {
2121

2222
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
2323

24-
void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
24+
void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2525
mp_obj_complex_t *o = o_in;
2626
if (o->real == 0) {
2727
print(env, "%.8gj", o->imag);

py/objdict.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef struct _mp_obj_dict_t {
2020
static mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
2121
static mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
2222

23-
static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
23+
static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
2424
mp_obj_dict_t *self = self_in;
2525
bool first = true;
2626
print(env, "{");
@@ -31,9 +31,9 @@ static void dict_print(void (*print)(void *env, const char *fmt, ...), void *env
3131
print(env, ", ");
3232
}
3333
first = false;
34-
mp_obj_print_helper(print, env, next->key);
34+
mp_obj_print_helper(print, env, next->key, PRINT_REPR);
3535
print(env, ": ");
36-
mp_obj_print_helper(print, env, next->value);
36+
mp_obj_print_helper(print, env, next->value, PRINT_REPR);
3737
}
3838
print(env, "}");
3939
}
@@ -350,7 +350,7 @@ static mp_obj_t dict_view_getiter(mp_obj_t view_in) {
350350
return o;
351351
}
352352

353-
static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
353+
static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
354354
assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
355355
mp_obj_dict_view_t *self = self_in;
356356
bool first = true;
@@ -363,7 +363,7 @@ static void dict_view_print(void (*print)(void *env, const char *fmt, ...), void
363363
print(env, ", ");
364364
}
365365
first = false;
366-
mp_obj_print_helper(print, env, next);
366+
mp_obj_print_helper(print, env, next, PRINT_REPR);
367367
}
368368
print(env, "])");
369369
}

py/objexcept.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,25 @@ typedef struct mp_obj_exception_t {
2121
mp_obj_tuple_t args;
2222
} mp_obj_exception_t;
2323

24-
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
24+
void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2525
mp_obj_exception_t *o = o_in;
2626
if (o->msg != 0) {
2727
print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg));
2828
} else {
29-
print(env, "%s", qstr_str(o->id));
30-
tuple_print(print, env, &o->args);
29+
// Yes, that's how CPython has it
30+
if (kind == PRINT_REPR) {
31+
print(env, "%s", qstr_str(o->id));
32+
}
33+
if (kind == PRINT_STR) {
34+
if (o->args.len == 0) {
35+
print(env, "");
36+
return;
37+
} else if (o->args.len == 1) {
38+
mp_obj_print_helper(print, env, o->args.items[0], PRINT_STR);
39+
return;
40+
}
41+
}
42+
tuple_print(print, env, &o->args, kind);
3143
}
3244
}
3345

py/objfloat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct _mp_obj_float_t {
1919

2020
mp_obj_t mp_obj_new_float(mp_float_t value);
2121

22-
static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
22+
static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2323
mp_obj_float_t *o = o_in;
2424
print(env, "%.8g", o->value);
2525
}

py/objgenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ typedef struct _mp_obj_gen_instance_t {
6161
mp_obj_t state[];
6262
} mp_obj_gen_instance_t;
6363

64-
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
64+
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
6565
print(env, "<generator object 'fun-name' at %p>", self_in);
6666
}
6767

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const mp_obj_type_t int_type = {
3939

4040
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
4141
// This is called only for non-SMALL_INT
42-
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
42+
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
4343
}
4444

4545
// This is called only for non-SMALL_INT

0 commit comments

Comments
 (0)