From 89797fd3f9d36cd7e2e7a125dbbe02f598874060 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 4 Aug 2020 11:15:22 -0500 Subject: [PATCH] various: Use mp_obj_get_type_qstr more widely This removes runtime allocations of the cstring version of the qstring. It is not a size improvement --- extmod/vfs_fat_file.c | 2 +- extmod/vfs_posix_file.c | 2 +- ports/unix/file.c | 2 +- py/obj.c | 14 +++++++------- py/objstr.c | 14 +++++++------- py/objstrunicode.c | 2 +- py/objtype.c | 12 ++++++------ py/proto.c | 4 ++-- py/runtime.c | 28 ++++++++++++++-------------- 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 0642f4a9ce..89786bfa36 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -42,7 +42,7 @@ const byte fresult_to_errno_table[20] = { STATIC void file_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; - mp_printf(print, "", mp_obj_get_type_str(self_in), MP_OBJ_TO_PTR(self_in)); + mp_printf(print, "", mp_obj_get_type_qstr(self_in), MP_OBJ_TO_PTR(self_in)); } STATIC mp_uint_t file_obj_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { diff --git a/extmod/vfs_posix_file.c b/extmod/vfs_posix_file.c index eefc1d905a..3f887785e7 100644 --- a/extmod/vfs_posix_file.c +++ b/extmod/vfs_posix_file.c @@ -34,7 +34,7 @@ STATIC void check_fd_is_open(const mp_obj_vfs_posix_file_t *o) { STATIC void vfs_posix_file_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; mp_obj_vfs_posix_file_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "", mp_obj_get_type_str(self_in), self->fd); + mp_printf(print, "", mp_obj_get_type_qstr(self_in), self->fd); } mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in) { diff --git a/ports/unix/file.c b/ports/unix/file.c index e4f62e3d5a..222dca4621 100644 --- a/ports/unix/file.c +++ b/ports/unix/file.c @@ -60,7 +60,7 @@ extern const mp_obj_type_t mp_type_textio; STATIC void fdfile_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; mp_obj_fdfile_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "", mp_obj_get_type_str(self_in), self->fd); + mp_printf(print, "", mp_obj_get_type_qstr(self_in), self->fd); } STATIC mp_uint_t fdfile_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) { diff --git a/py/obj.c b/py/obj.c index 404fdd5d9d..41fdaf6330 100644 --- a/py/obj.c +++ b/py/obj.c @@ -381,7 +381,7 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) { mp_raise_TypeError(translate("expected tuple/list")); } else { mp_raise_TypeError_varg( - translate("object '%s' is not a tuple or list"), mp_obj_get_type_str(o)); + translate("object '%q' is not a tuple or list"), mp_obj_get_type_qstr(o)); } } } @@ -410,8 +410,8 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool mp_raise_TypeError(translate("indices must be integers")); } else { mp_raise_TypeError_varg( - translate("%q indices must be integers, not %s"), - type->name, mp_obj_get_type_str(index)); + translate("%q indices must be integers, not %q"), + type->name, mp_obj_get_type_qstr(index)); } } @@ -465,7 +465,7 @@ mp_obj_t mp_obj_len(mp_obj_t o_in) { mp_raise_TypeError(translate("object has no len")); } else { mp_raise_TypeError_varg( - translate("object of type '%s' has no len()"), mp_obj_get_type_str(o_in)); + translate("object of type '%q' has no len()"), mp_obj_get_type_qstr(o_in)); } } else { return len; @@ -508,21 +508,21 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) { mp_raise_TypeError(translate("object does not support item deletion")); } else { mp_raise_TypeError_varg( - translate("'%s' object does not support item deletion"), mp_obj_get_type_str(base)); + translate("'%q' object does not support item deletion"), mp_obj_get_type_qstr(base)); } } else if (value == MP_OBJ_SENTINEL) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object is not subscriptable")); } else { mp_raise_TypeError_varg( - translate("'%s' object is not subscriptable"), mp_obj_get_type_str(base)); + translate("'%q' object is not subscriptable"), mp_obj_get_type_qstr(base)); } } else { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object does not support item assignment")); } else { mp_raise_TypeError_varg( - translate("'%s' object does not support item assignment"), mp_obj_get_type_str(base)); + translate("'%q' object does not support item assignment"), mp_obj_get_type_qstr(base)); } } } diff --git a/py/objstr.c b/py/objstr.c index 5e0c6fdfaa..572bf299d4 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -1280,8 +1280,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar terse_str_format_value_error(); } else { mp_raise_ValueError_varg( - translate("unknown format code '%c' for object of type '%s'"), - type, mp_obj_get_type_str(arg)); + translate("unknown format code '%c' for object of type '%q'"), + type, mp_obj_get_type_qstr(arg)); } } } @@ -1352,8 +1352,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar terse_str_format_value_error(); } else { mp_raise_ValueError_varg( - translate("unknown format code '%c' for object of type '%s'"), - type, mp_obj_get_type_str(arg)); + translate("unknown format code '%c' for object of type '%q'"), + type, mp_obj_get_type_qstr(arg)); } } } else { @@ -1388,8 +1388,8 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar terse_str_format_value_error(); } else { mp_raise_ValueError_varg( - translate("unknown format code '%c' for object of type '%s'"), - type, mp_obj_get_type_str(arg)); + translate("unknown format code '%c' for object of type '%q'"), + type, mp_obj_get_type_qstr(arg)); } } } @@ -2133,7 +2133,7 @@ STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("can't convert to str implicitly")); } else { - const qstr src_name = mp_obj_get_type(self_in)->name; + const qstr src_name = mp_obj_get_type_qstr(self_in); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, translate("can't convert '%q' object to %q implicitly"), src_name, src_name == MP_QSTR_str ? MP_QSTR_bytes : MP_QSTR_str)); diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 351a67e913..19d89a31ce 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -151,7 +151,7 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s if (MP_OBJ_IS_SMALL_INT(index)) { i = MP_OBJ_SMALL_INT_VALUE(index); } else if (!mp_obj_get_int_maybe(index, &i)) { - mp_raise_TypeError_varg(translate("string indices must be integers, not %s"), mp_obj_get_type_str(index)); + mp_raise_TypeError_varg(translate("string indices must be integers, not %q"), mp_obj_get_type_qstr(index)); } const byte *s, *top = self_data + self_len; if (i < 0) diff --git a/py/objtype.c b/py/objtype.c index fd51ce36b8..ccd014c335 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -193,7 +193,7 @@ STATIC void mp_obj_class_lookup(struct class_lookup_data *lookup, const mp_obj_ printf("mp_obj_class_lookup: Returning: "); mp_obj_print(lookup->dest[0], PRINT_REPR); printf(" "); // Don't try to repr() lookup->dest[1], as we can be called recursively - printf("<%s @%p>\n", mp_obj_get_type_str(lookup->dest[1]), lookup->dest[1]); + printf("<%q @%p>\n", mp_obj_get_type_qstr(lookup->dest[1]), lookup->dest[1]); #endif return; } @@ -285,7 +285,7 @@ STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k } // TODO: CPython prints fully-qualified type name - mp_printf(print, "<%s object at %p>", mp_obj_get_type_str(self_in), self); + mp_printf(print, "<%q object at %p>", mp_obj_get_type_qstr(self_in), self); } mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { @@ -376,8 +376,8 @@ mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, cons if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("__init__() should return None")); } else { - mp_raise_TypeError_varg(translate("__init__() should return None, not '%s'"), - mp_obj_get_type_str(init_ret)); + mp_raise_TypeError_varg(translate("__init__() should return None, not '%q'"), + mp_obj_get_type_qstr(init_ret)); } } @@ -891,8 +891,8 @@ mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object not callable")); } else { - mp_raise_TypeError_varg(translate("'%s' object is not callable"), - mp_obj_get_type_str(self_in)); + mp_raise_TypeError_varg(translate("'%q' object is not callable"), + mp_obj_get_type_qstr(self_in)); } } mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/py/proto.c b/py/proto.c index e5053130b8..e4da157f05 100644 --- a/py/proto.c +++ b/py/proto.c @@ -45,6 +45,6 @@ const void *mp_proto_get_or_throw(uint16_t name, mp_const_obj_t obj) { if (proto) { return proto; } - mp_raise_TypeError_varg(translate("'%s' object does not support '%q'"), - mp_obj_get_type_str(obj), name); + mp_raise_TypeError_varg(translate("'%q' object does not support '%q'"), + mp_obj_get_type_qstr(obj), name); } diff --git a/py/runtime.c b/py/runtime.c index 87c24dc562..f9ef7819d1 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -279,8 +279,8 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) { mp_raise_TypeError(translate("unsupported type for operator")); } else { mp_raise_TypeError_varg( - translate("unsupported type for %q: '%s'"), - mp_unary_op_method_name[op], mp_obj_get_type_str(arg)); + translate("unsupported type for %q: '%q'"), + mp_unary_op_method_name[op], mp_obj_get_type_qstr(arg)); } } } @@ -586,8 +586,8 @@ unsupported_op: mp_raise_TypeError(translate("unsupported type for operator")); } else { mp_raise_TypeError_varg( - translate("unsupported types for %q: '%s', '%s'"), - mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)); + translate("unsupported types for %q: '%q', '%q'"), + mp_binary_op_method_name[op], mp_obj_get_type_qstr(lhs), mp_obj_get_type_qstr(rhs)); } zero_division: @@ -627,7 +627,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object not callable")); } else { - mp_raise_TypeError_varg(translate("'%s' object is not callable"), mp_obj_get_type_str(fun_in)); + mp_raise_TypeError_varg(translate("'%q' object is not callable"), mp_obj_get_type_qstr(fun_in)); } } @@ -1104,8 +1104,8 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) { ((mp_obj_type_t*)MP_OBJ_TO_PTR(base))->name, attr)); } else { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, - translate("'%s' object has no attribute '%q'"), - mp_obj_get_type_str(base), attr)); + translate("'%q' object has no attribute '%q'"), + mp_obj_get_type_qstr(base), attr)); } } } @@ -1172,8 +1172,8 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { mp_raise_AttributeError(translate("no such attribute")); } else { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, - translate("'%s' object cannot assign attribute '%q'"), - mp_obj_get_type_str(base), attr)); + translate("'%q' object cannot assign attribute '%q'"), + mp_obj_get_type_qstr(base), attr)); } } @@ -1213,7 +1213,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { mp_raise_TypeError(translate("object not iterable")); } else { mp_raise_TypeError_varg( - translate("'%s' object is not iterable"), mp_obj_get_type_str(o_in)); + translate("'%q' object is not iterable"), mp_obj_get_type_qstr(o_in)); } } @@ -1234,8 +1234,8 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object not an iterator")); } else { - mp_raise_TypeError_varg(translate("'%s' object is not an iterator"), - mp_obj_get_type_str(o_in)); + mp_raise_TypeError_varg(translate("'%q' object is not an iterator"), + mp_obj_get_type_qstr(o_in)); } } } @@ -1270,8 +1270,8 @@ mp_obj_t mp_iternext(mp_obj_t o_in) { if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { mp_raise_TypeError(translate("object not an iterator")); } else { - mp_raise_TypeError_varg(translate("'%s' object is not an iterator"), - mp_obj_get_type_str(o_in)); + mp_raise_TypeError_varg(translate("'%q' object is not an iterator"), + mp_obj_get_type_qstr(o_in)); } } }