Skip to content

Commit 4899ff9

Browse files
committed
Merge branch 'str-repr' of github.com:pfalcon/micropython into pfalcon-str-repr
Conflicts: tests/basics/tests/exception1.py
2 parents 7a9d0c4 + 36c4499 commit 4899ff9

28 files changed

Lines changed: 89 additions & 63 deletions

py/builtin.c

Lines changed: 11 additions & 9 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;
@@ -283,13 +283,7 @@ static mp_obj_t mp_builtin_print(int n_args, const mp_obj_t *args) {
283283
if (i > 0) {
284284
printf(" ");
285285
}
286-
if (MP_OBJ_IS_TYPE(args[i], &str_type)) {
287-
// special case, print string raw
288-
printf("%s", qstr_str(mp_obj_str_get(args[i])));
289-
} else {
290-
// print the object Python style
291-
mp_obj_print(args[i]);
292-
}
286+
mp_obj_print(args[i], PRINT_STR);
293287
}
294288
printf("\n");
295289
return mp_const_none;
@@ -310,7 +304,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_range_obj, 1, 3, mp_builtin_range
310304

311305
static mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
312306
vstr_t *vstr = vstr_new();
313-
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in);
307+
mp_obj_print_helper((void (*)(void *env, const char *fmt, ...))vstr_printf, vstr, o_in, PRINT_REPR);
314308
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
315309
}
316310

@@ -352,3 +346,11 @@ static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
352346
}
353347

354348
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
349+
350+
static mp_obj_t mp_builtin_str(mp_obj_t o_in) {
351+
vstr_t *vstr = vstr_new();
352+
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, vstr, o_in, PRINT_STR);
353+
return mp_obj_new_str(qstr_from_str_take(vstr->buf, vstr->alloc));
354+
}
355+
356+
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_str_obj, mp_builtin_str);

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_range_obj);
2525
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
2626
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
2727
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
28+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_str_obj);

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Q(repr)
6565
Q(set)
6666
Q(sorted)
6767
Q(sum)
68+
Q(str)
6869
Q(tuple)
6970
Q(type)
7071
Q(zip)

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
}

0 commit comments

Comments
 (0)