Skip to content

Commit 044c473

Browse files
committed
py: Add %q format support to mp_[v]printf, and use it.
1 parent e72cda9 commit 044c473

File tree

16 files changed

+61
-51
lines changed

16 files changed

+61
-51
lines changed

py/argcheck.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_ui
9191
mp_arg_error_terse_mismatch();
9292
} else {
9393
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
94-
"'%s' argument required",
95-
qstr_str(allowed[i].qst)));
94+
"'%q' argument required", allowed[i].qst));
9695
}
9796
}
9897
out_vals[i] = allowed[i].defval;

py/bc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expecte
6161
"function takes %d positional arguments but %d were given", expected, given));
6262
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
6363
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
64-
"%s() takes %d positional arguments but %d were given",
65-
qstr_str(mp_obj_fun_get_name(f)), expected, given));
64+
"%q() takes %d positional arguments but %d were given",
65+
mp_obj_fun_get_name(f), expected, given));
6666
#endif
6767
}
6868

@@ -166,7 +166,7 @@ void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t
166166
if (wanted_arg_name == arg_names[j]) {
167167
if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) {
168168
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
169-
"function got multiple values for argument '%s'", qstr_str(MP_OBJ_QSTR_VALUE(wanted_arg_name))));
169+
"function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
170170
}
171171
code_state->state[n_state - 1 - j] = kwargs[2 * i + 1];
172172
goto continue2;
@@ -215,7 +215,7 @@ continue2:;
215215
code_state->state[n_state - 1 - self->n_pos_args - i] = elem->value;
216216
} else {
217217
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
218-
"function missing required keyword argument '%s'", qstr_str(MP_OBJ_QSTR_VALUE(arg_names[self->n_pos_args + i]))));
218+
"function missing required keyword argument '%q'", MP_OBJ_QSTR_VALUE(arg_names[self->n_pos_args + i])));
219219
}
220220
}
221221
}

py/builtinimport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
319319
nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
320320
} else {
321321
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
322-
"no module named '%s'", qstr_str(mod_name)));
322+
"no module named '%q'", mod_name));
323323
}
324324
}
325325
} else {

py/emitinlinethumb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_
296296
}
297297
// only need to have the labels on the last pass
298298
if (emit->pass == MP_PASS_EMIT) {
299-
emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%s' not defined", qstr_str(label_qstr)));
299+
emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%q' not defined", label_qstr));
300300
}
301301
return 0;
302302
}

py/emitnative.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t ar
571571
case MP_QSTR_ptr: type = VTYPE_PTR; break;
572572
case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
573573
case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
574-
default: mp_printf(&mp_plat_print, "ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
574+
default: mp_printf(&mp_plat_print, "ViperTypeError: unknown type %q\n", arg2); return;
575575
}
576576
if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
577577
emit->return_vtype = type;
@@ -1288,7 +1288,7 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
12881288
DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
12891289
vtype_kind_t vtype = emit->local_vtype[local_num];
12901290
if (vtype == VTYPE_UNBOUND) {
1291-
mp_printf(&mp_plat_print, "ViperTypeError: local %s used before type known\n", qstr_str(qst));
1291+
mp_printf(&mp_plat_print, "ViperTypeError: local %q used before type known\n", qst);
12921292
}
12931293
emit_native_pre(emit);
12941294
if (local_num == 0) {
@@ -1495,7 +1495,7 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
14951495
emit->local_vtype[local_num] = vtype;
14961496
} else if (emit->local_vtype[local_num] != vtype) {
14971497
// type of local is not the same as object stored in it
1498-
mp_printf(&mp_plat_print, "ViperTypeError: type mismatch, local %s has type %d but source object has type %d\n", qstr_str(qst), emit->local_vtype[local_num], vtype);
1498+
mp_printf(&mp_plat_print, "ViperTypeError: type mismatch, local %q has type %d but source object has type %d\n", qst, emit->local_vtype[local_num], vtype);
14991499
}
15001500
}
15011501

py/mpprint.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
489489
chrs += mp_print_strn(print, &str, 1, flags, fill, width);
490490
break;
491491
}
492+
case 'q':
493+
{
494+
qstr qst = va_arg(args, qstr);
495+
mp_uint_t len;
496+
const char *str = (const char*)qstr_data(qst, &len);
497+
if (prec < 0) {
498+
prec = len;
499+
}
500+
chrs += mp_print_strn(print, str, prec, flags, fill, width);
501+
break;
502+
}
492503
case 's':
493504
{
494505
const char *str = va_arg(args, const char*);

py/obj.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "py/runtime0.h"
3737
#include "py/runtime.h"
3838
#include "py/stackctrl.h"
39-
//#include "py/pfenv.h"
4039
#include "py/stream.h" // for mp_obj_print
4140

4241
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
@@ -67,7 +66,7 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
6766
if (type->print != NULL) {
6867
type->print((mp_print_t*)print, o_in, kind);
6968
} else {
70-
mp_printf(print, "<%s>", qstr_str(type->name));
69+
mp_printf(print, "<%q>", type->name);
7170
}
7271
}
7372

@@ -89,16 +88,16 @@ void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
8988
mp_print_str(print, "Traceback (most recent call last):\n");
9089
for (int i = n - 3; i >= 0; i -= 3) {
9190
#if MICROPY_ENABLE_SOURCE_LINE
92-
mp_printf(print, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
91+
mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
9392
#else
94-
mp_printf(print, " File \"%s\"", qstr_str(values[i]));
93+
mp_printf(print, " File \"%q\"", values[i]);
9594
#endif
9695
// the block name can be NULL if it's unknown
9796
qstr block = values[i + 2];
9897
if (block == MP_QSTR_NULL) {
9998
mp_print_str(print, "\n");
10099
} else {
101-
mp_printf(print, ", in %s\n", qstr_str(block));
100+
mp_printf(print, ", in %q\n", block);
102101
}
103102
}
104103
}
@@ -387,8 +386,8 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
387386
"indices must be integers"));
388387
} else {
389388
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
390-
"%s indices must be integers, not %s",
391-
qstr_str(type->name), mp_obj_get_type_str(index)));
389+
"%q indices must be integers, not %s",
390+
type->name, mp_obj_get_type_str(index)));
392391
}
393392
}
394393

@@ -407,7 +406,7 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
407406
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
408407
} else {
409408
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
410-
"%s index out of range", qstr_str(type->name)));
409+
"%q index out of range", type->name));
411410
}
412411
}
413412
}

py/objdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
6262
kind = PRINT_REPR;
6363
}
6464
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
65-
mp_printf(print, "%s(", qstr_str(self->base.type->name));
65+
mp_printf(print, "%q(", self->base.type->name);
6666
}
6767
mp_print_str(print, "{");
6868
mp_uint_t cur = 0;

py/objexcept.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_pr
9595
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
9696
bool is_subclass = kind & PRINT_EXC_SUBCLASS;
9797
if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
98-
mp_printf(print, "%s", qstr_str(o->base.type->name));
98+
mp_print_str(print, qstr_str(o->base.type->name));
9999
}
100100

101101
if (k == PRINT_EXC) {
@@ -321,13 +321,17 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
321321
uint max_len = MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
322322
- str_data;
323323

324+
vstr_t vstr;
325+
vstr_init_fixed_buf(&vstr, max_len, (char *)str_data);
326+
324327
va_list ap;
325328
va_start(ap, fmt);
326-
str->len = vsnprintf((char *)str_data, max_len, fmt, ap);
329+
vstr_vprintf(&vstr, fmt, ap);
327330
va_end(ap);
328331

329332
str->base.type = &mp_type_str;
330333
str->hash = qstr_compute_hash(str_data, str->len);
334+
str->len = vstr.len;
331335
str->data = str_data;
332336

333337
o->args = tuple;

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
128128
STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
129129
(void)kind;
130130
mp_obj_fun_bc_t *o = o_in;
131-
mp_printf(print, "<function %s at 0x%x>", qstr_str(mp_obj_fun_get_name(o)), o);
131+
mp_printf(print, "<function %q at 0x%x>", mp_obj_fun_get_name(o), o);
132132
}
133133
#endif
134134

0 commit comments

Comments
 (0)