Commit 044c473d authored by Damien George's avatar Damien George
Browse files

py: Add %q format support to mp_[v]printf, and use it.

parent e72cda99
......@@ -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
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"'%s' argument required",
qstr_str(allowed[i].qst)));
"'%q' argument required", allowed[i].qst));
}
}
out_vals[i] = allowed[i].defval;
......
......@@ -61,8 +61,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expecte
"function takes %d positional arguments but %d were given", expected, given));
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%s() takes %d positional arguments but %d were given",
qstr_str(mp_obj_fun_get_name(f)), expected, given));
"%q() takes %d positional arguments but %d were given",
mp_obj_fun_get_name(f), expected, given));
#endif
}
......@@ -166,7 +166,7 @@ void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t
if (wanted_arg_name == arg_names[j]) {
if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function got multiple values for argument '%s'", qstr_str(MP_OBJ_QSTR_VALUE(wanted_arg_name))));
"function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
}
code_state->state[n_state - 1 - j] = kwargs[2 * i + 1];
goto continue2;
......@@ -215,7 +215,7 @@ continue2:;
code_state->state[n_state - 1 - self->n_pos_args - i] = elem->value;
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing required keyword argument '%s'", qstr_str(MP_OBJ_QSTR_VALUE(arg_names[self->n_pos_args + i]))));
"function missing required keyword argument '%q'", MP_OBJ_QSTR_VALUE(arg_names[self->n_pos_args + i])));
}
}
}
......
......@@ -319,7 +319,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ImportError, "module not found"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
"no module named '%s'", qstr_str(mod_name)));
"no module named '%q'", mod_name));
}
}
} else {
......
......@@ -296,7 +296,7 @@ STATIC int get_arg_label(emit_inline_asm_t *emit, const char *op, mp_parse_node_
}
// only need to have the labels on the last pass
if (emit->pass == MP_PASS_EMIT) {
emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%s' not defined", qstr_str(label_qstr)));
emit_inline_thumb_error_exc(emit, mp_obj_new_exception_msg_varg(&mp_type_SyntaxError, "label '%q' not defined", label_qstr));
}
return 0;
}
......
......@@ -571,7 +571,7 @@ STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t ar
case MP_QSTR_ptr: type = VTYPE_PTR; break;
case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
default: mp_printf(&mp_plat_print, "ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
default: mp_printf(&mp_plat_print, "ViperTypeError: unknown type %q\n", arg2); return;
}
if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
emit->return_vtype = type;
......@@ -1288,7 +1288,7 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
vtype_kind_t vtype = emit->local_vtype[local_num];
if (vtype == VTYPE_UNBOUND) {
mp_printf(&mp_plat_print, "ViperTypeError: local %s used before type known\n", qstr_str(qst));
mp_printf(&mp_plat_print, "ViperTypeError: local %q used before type known\n", qst);
}
emit_native_pre(emit);
if (local_num == 0) {
......@@ -1495,7 +1495,7 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num)
emit->local_vtype[local_num] = vtype;
} else if (emit->local_vtype[local_num] != vtype) {
// type of local is not the same as object stored in it
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);
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);
}
}
......
......@@ -489,6 +489,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
chrs += mp_print_strn(print, &str, 1, flags, fill, width);
break;
}
case 'q':
{
qstr qst = va_arg(args, qstr);
mp_uint_t len;
const char *str = (const char*)qstr_data(qst, &len);
if (prec < 0) {
prec = len;
}
chrs += mp_print_strn(print, str, prec, flags, fill, width);
break;
}
case 's':
{
const char *str = va_arg(args, const char*);
......
......@@ -36,7 +36,6 @@
#include "py/runtime0.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
//#include "py/pfenv.h"
#include "py/stream.h" // for mp_obj_print
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
if (type->print != NULL) {
type->print((mp_print_t*)print, o_in, kind);
} else {
mp_printf(print, "<%s>", qstr_str(type->name));
mp_printf(print, "<%q>", type->name);
}
}
......@@ -89,16 +88,16 @@ void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc) {
mp_print_str(print, "Traceback (most recent call last):\n");
for (int i = n - 3; i >= 0; i -= 3) {
#if MICROPY_ENABLE_SOURCE_LINE
mp_printf(print, " File \"%s\", line %d", qstr_str(values[i]), (int)values[i + 1]);
mp_printf(print, " File \"%q\", line %d", values[i], (int)values[i + 1]);
#else
mp_printf(print, " File \"%s\"", qstr_str(values[i]));
mp_printf(print, " File \"%q\"", values[i]);
#endif
// the block name can be NULL if it's unknown
qstr block = values[i + 2];
if (block == MP_QSTR_NULL) {
mp_print_str(print, "\n");
} else {
mp_printf(print, ", in %s\n", qstr_str(block));
mp_printf(print, ", in %q\n", block);
}
}
}
......@@ -387,8 +386,8 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
"indices must be integers"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%s indices must be integers, not %s",
qstr_str(type->name), mp_obj_get_type_str(index)));
"%q indices must be integers, not %s",
type->name, mp_obj_get_type_str(index)));
}
}
......@@ -407,7 +406,7 @@ mp_uint_t mp_get_index(const mp_obj_type_t *type, mp_uint_t len, mp_obj_t index,
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "index out of range"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError,
"%s index out of range", qstr_str(type->name)));
"%q index out of range", type->name));
}
}
}
......
......@@ -62,7 +62,7 @@ STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
kind = PRINT_REPR;
}
if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
mp_printf(print, "%s(", qstr_str(self->base.type->name));
mp_printf(print, "%q(", self->base.type->name);
}
mp_print_str(print, "{");
mp_uint_t cur = 0;
......
......@@ -95,7 +95,7 @@ STATIC void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_pr
mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
bool is_subclass = kind & PRINT_EXC_SUBCLASS;
if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
mp_printf(print, "%s", qstr_str(o->base.type->name));
mp_print_str(print, qstr_str(o->base.type->name));
}
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
uint max_len = MP_STATE_VM(mp_emergency_exception_buf) + mp_emergency_exception_buf_size
- str_data;
vstr_t vstr;
vstr_init_fixed_buf(&vstr, max_len, (char *)str_data);
va_list ap;
va_start(ap, fmt);
str->len = vsnprintf((char *)str_data, max_len, fmt, ap);
vstr_vprintf(&vstr, fmt, ap);
va_end(ap);
str->base.type = &mp_type_str;
str->hash = qstr_compute_hash(str_data, str->len);
str->len = vstr.len;
str->data = str_data;
o->args = tuple;
......
......@@ -128,7 +128,7 @@ qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_fun_bc_t *o = o_in;
mp_printf(print, "<function %s at 0x%x>", qstr_str(mp_obj_fun_get_name(o)), o);
mp_printf(print, "<function %q at 0x%x>", mp_obj_fun_get_name(o), o);
}
#endif
......
......@@ -98,7 +98,7 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_gen_instance_t *self = self_in;
mp_printf(print, "<generator object '%s' at %p>", qstr_str(mp_obj_code_get_name(self->code_state.code_info)), self_in);
mp_printf(print, "<generator object '%q' at %p>", mp_obj_code_get_name(self->code_state.code_info), self_in);
}
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
......
......@@ -36,19 +36,18 @@
STATIC void module_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_module_t *self = self_in;
const char *name = qstr_str(self->name);
#if MICROPY_PY___FILE__
// If we store __file__ to imported modules then try to lookup this
// symbol to give more information about the module.
mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
if (elem != NULL) {
mp_printf(print, "<module '%s' from '%s'>", name, mp_obj_str_get_str(elem->value));
mp_printf(print, "<module '%q' from '%s'>", self->name, mp_obj_str_get_str(elem->value));
return;
}
#endif
mp_printf(print, "<module '%s'>", name);
mp_printf(print, "<module '%q'>", self->name);
}
STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
......
......@@ -56,13 +56,13 @@ STATIC mp_uint_t namedtuple_find_field(mp_obj_namedtuple_type_t *type, qstr name
STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_namedtuple_t *o = o_in;
mp_printf(print, "%s(", qstr_str(o->tuple.base.type->name));
mp_printf(print, "%q(", o->tuple.base.type->name);
const qstr *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
for (mp_uint_t i = 0; i < o->tuple.len; i++) {
if (i > 0) {
mp_print_str(print, ", ");
}
mp_printf(print, "%s=", qstr_str(fields[i]));
mp_printf(print, "%q=", fields[i]);
mp_obj_print_helper(print, o->tuple.items[i], PRINT_REPR);
}
mp_print_str(print, ")");
......@@ -96,8 +96,8 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
num_fields, n_args + n_kw));
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%s() takes %d positional arguments but %d were given",
qstr_str(type->base.name), num_fields, n_args + n_kw));
"%q() takes %d positional arguments but %d were given",
type->base.name, num_fields, n_args + n_kw));
}
}
......@@ -121,8 +121,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unexpected keyword argument '%s'",
qstr_str(kw)));
"unexpected keyword argument '%q'", kw));
}
}
if (arg_objects[id] != NULL) {
......@@ -130,8 +129,7 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
mp_arg_error_terse_mismatch();
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function got multiple values for argument '%s'",
qstr_str(kw)));
"function got multiple values for argument '%q'", kw));
}
}
arg_objects[id] = args[i + 1];
......
......@@ -740,7 +740,7 @@ STATIC mp_int_t instance_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
STATIC void type_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_type_t *self = self_in;
mp_printf(print, "<class '%s'>", qstr_str(self->name));
mp_printf(print, "<class '%q'>", self->name);
}
STATIC mp_obj_t type_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
......@@ -773,7 +773,7 @@ STATIC mp_obj_t type_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, co
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "cannot create instance"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"cannot create '%s' instances", qstr_str(self->name)));
"cannot create '%q' instances", self->name));
}
}
......@@ -861,7 +861,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
"type is not an acceptable base type"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"type '%s' is not an acceptable base type", qstr_str(t->name)));
"type '%q' is not an acceptable base type", t->name));
}
}
}
......
......@@ -149,7 +149,7 @@ mp_obj_t mp_load_global(qstr qst) {
"name not defined"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
"name '%s' is not defined", qstr_str(qst)));
"name '%q' is not defined", qst));
}
}
}
......@@ -228,8 +228,8 @@ mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg) {
"unsupported type for operator"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unsupported type for %s: '%s'",
qstr_str(mp_unary_op_method_name[op]), mp_obj_get_type_str(arg)));
"unsupported type for %q: '%s'",
mp_unary_op_method_name[op], mp_obj_get_type_str(arg)));
}
}
}
......@@ -522,8 +522,8 @@ unsupported_op:
"unsupported type for operator"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"unsupported types for %s: '%s', '%s'",
qstr_str(mp_binary_op_method_name[op]), mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
"unsupported types for %q: '%s', '%s'",
mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
}
zero_division:
......@@ -947,12 +947,12 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
// following CPython, we give a more detailed error message for type objects
if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
"type object '%s' has no attribute '%s'",
qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
"type object '%q' has no attribute '%q'",
((mp_obj_type_t*)base)->name, attr));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
"'%s' object has no attribute '%s'",
mp_obj_get_type_str(base), qstr_str(attr)));
"'%s' object has no attribute '%q'",
mp_obj_get_type_str(base), attr));
}
}
}
......@@ -974,8 +974,8 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
"no such attribute"));
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
"'%s' object has no attribute '%s'",
mp_obj_get_type_str(base), qstr_str(attr)));
"'%s' object has no attribute '%q'",
mp_obj_get_type_str(base), attr));
}
}
......@@ -1180,7 +1180,7 @@ mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
if (dest[1] != MP_OBJ_NULL) {
// Hopefully we can't import bound method from an object
import_error:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %s", qstr_str(name)));
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %q", name));
}
if (dest[0] != MP_OBJ_NULL) {
......
......@@ -6,7 +6,7 @@ mem: total=\\d\+, current=\\d\+, peak=\\d\+
stack: \\d\+ out of \\d\+
GC: total: \\d\+, used: \\d\+, free: \\d\+
No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+
GC memory layout; from 0x\[0-9a-f\]\+:
GC memory layout; from \[0-9a-f\]\+:
########
qstr pool: n_pool=1, n_qstr=\\d, n_str_data_bytes=\\d\+, n_total_bytes=\\d\+
qstr pool: n_pool=1, n_qstr=\\d, n_str_data_bytes=\\d\+, n_total_bytes=\\d\+
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment