Skip to content

Commit 4fe08ff

Browse files
jeplerdpgeorge
authored andcommitted
all: Avoid qstr_str calls when printing a qstr.
The `%q` formatter of mp_printf can be used instead, and may save a small amount of code. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
1 parent c20302d commit 4fe08ff

6 files changed

Lines changed: 25 additions & 25 deletions

File tree

ports/mimxrt/machine_pin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
329329
static void machine_pin_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
330330
(void)kind;
331331
const machine_pin_obj_t *self = MP_OBJ_TO_PTR(o);
332-
mp_printf(print, "Pin(%s)", qstr_str(self->name));
332+
mp_printf(print, "Pin(%q)", (qstr)self->name);
333333
}
334334

335335
// pin(id, mode, pull, ...)

ports/stm32/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,10 +1589,10 @@ MP_DEFINE_CONST_OBJ_TYPE(
15891589
static void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
15901590
pyb_timer_channel_obj_t *self = MP_OBJ_TO_PTR(self_in);
15911591

1592-
mp_printf(print, "TimerChannel(timer=%u, channel=%u, mode=%s)",
1592+
mp_printf(print, "TimerChannel(timer=%u, channel=%u, mode=%q)",
15931593
self->timer->tim_id,
15941594
self->channel,
1595-
qstr_str(channel_mode_info[self->mode].name));
1595+
(qstr)channel_mode_info[self->mode].name);
15961596
}
15971597

15981598
/// \method capture([value])

ports/unix/modjni.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static void jmethod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
357357
(void)kind;
358358
mp_obj_jmethod_t *self = MP_OBJ_TO_PTR(self_in);
359359
// Variable value printed as cast to int
360-
mp_printf(print, "<jmethod '%s'>", qstr_str(self->name));
360+
mp_printf(print, "<jmethod '%q'>", (qstr)self->name);
361361
}
362362

363363
#define IMATCH(s, static) ((!strncmp(s, static, sizeof(static) - 1)) && (s += sizeof(static) - 1))

py/emitndebug.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static void asm_debug_reg_imm(asm_debug_t *as, const char *op, int reg, int imm)
134134

135135
#if !MICROPY_PERSISTENT_CODE_SAVE
136136
static void asm_debug_reg_qstr(asm_debug_t *as, const char *op, int reg, int qst) {
137-
asm_debug_printf(as, "%s(%s, %s)\n", op, reg_name_table[reg], qstr_str(qst));
137+
asm_debug_printf(as, "%s(%s, %q)\n", op, reg_name_table[reg], (qstr)qst);
138138
}
139139
#endif
140140

py/parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ void mp_parse_node_print(const mp_print_t *print, mp_parse_node_t pn, size_t ind
491491
uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
492492
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
493493
case MP_PARSE_NODE_ID:
494-
mp_printf(print, "id(%s)\n", qstr_str(arg));
494+
mp_printf(print, "id(%q)\n", (qstr)arg);
495495
break;
496496
case MP_PARSE_NODE_STRING:
497-
mp_printf(print, "str(%s)\n", qstr_str(arg));
497+
mp_printf(print, "str(%q)\n", (qstr)arg);
498498
break;
499499
default:
500500
assert(MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN);

py/showbc.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, size_t
9999
#else
100100
qstr source_file = cm->source_file;
101101
#endif
102-
mp_printf(print, "File %s, code block '%s' (descriptor: %p, bytecode @%p %u bytes)\n",
103-
qstr_str(source_file), qstr_str(block_name), rc, ip_start, (unsigned)fun_data_len);
102+
mp_printf(print, "File %q, code block '%q' (descriptor: %p, bytecode @%p %u bytes)\n",
103+
source_file, block_name, rc, ip_start, (unsigned)fun_data_len);
104104

105105
// raw bytecode dump
106106
size_t prelude_size = ip - ip_start + n_info + n_cell;
@@ -121,7 +121,7 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, size_t
121121
#if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
122122
qst = cm->qstr_table[qst];
123123
#endif
124-
mp_printf(print, " %s", qstr_str(qst));
124+
mp_printf(print, " %q", qst);
125125
}
126126
mp_printf(print, "\n");
127127

@@ -189,7 +189,7 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
189189

190190
case MP_BC_LOAD_CONST_STRING:
191191
DECODE_QSTR;
192-
mp_printf(print, "LOAD_CONST_STRING '%s'", qstr_str(qst));
192+
mp_printf(print, "LOAD_CONST_STRING '%q'", qst);
193193
break;
194194

195195
case MP_BC_LOAD_CONST_OBJ:
@@ -214,27 +214,27 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
214214

215215
case MP_BC_LOAD_NAME:
216216
DECODE_QSTR;
217-
mp_printf(print, "LOAD_NAME %s", qstr_str(qst));
217+
mp_printf(print, "LOAD_NAME %q", qst);
218218
break;
219219

220220
case MP_BC_LOAD_GLOBAL:
221221
DECODE_QSTR;
222-
mp_printf(print, "LOAD_GLOBAL %s", qstr_str(qst));
222+
mp_printf(print, "LOAD_GLOBAL %q", qst);
223223
break;
224224

225225
case MP_BC_LOAD_ATTR:
226226
DECODE_QSTR;
227-
mp_printf(print, "LOAD_ATTR %s", qstr_str(qst));
227+
mp_printf(print, "LOAD_ATTR %q", qst);
228228
break;
229229

230230
case MP_BC_LOAD_METHOD:
231231
DECODE_QSTR;
232-
mp_printf(print, "LOAD_METHOD %s", qstr_str(qst));
232+
mp_printf(print, "LOAD_METHOD %q", qst);
233233
break;
234234

235235
case MP_BC_LOAD_SUPER_METHOD:
236236
DECODE_QSTR;
237-
mp_printf(print, "LOAD_SUPER_METHOD %s", qstr_str(qst));
237+
mp_printf(print, "LOAD_SUPER_METHOD %q", qst);
238238
break;
239239

240240
case MP_BC_LOAD_BUILD_CLASS:
@@ -257,17 +257,17 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
257257

258258
case MP_BC_STORE_NAME:
259259
DECODE_QSTR;
260-
mp_printf(print, "STORE_NAME %s", qstr_str(qst));
260+
mp_printf(print, "STORE_NAME %q", qst);
261261
break;
262262

263263
case MP_BC_STORE_GLOBAL:
264264
DECODE_QSTR;
265-
mp_printf(print, "STORE_GLOBAL %s", qstr_str(qst));
265+
mp_printf(print, "STORE_GLOBAL %q", qst);
266266
break;
267267

268268
case MP_BC_STORE_ATTR:
269269
DECODE_QSTR;
270-
mp_printf(print, "STORE_ATTR %s", qstr_str(qst));
270+
mp_printf(print, "STORE_ATTR %q", qst);
271271
break;
272272

273273
case MP_BC_STORE_SUBSCR:
@@ -286,12 +286,12 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
286286

287287
case MP_BC_DELETE_NAME:
288288
DECODE_QSTR;
289-
mp_printf(print, "DELETE_NAME %s", qstr_str(qst));
289+
mp_printf(print, "DELETE_NAME %q", qst);
290290
break;
291291

292292
case MP_BC_DELETE_GLOBAL:
293293
DECODE_QSTR;
294-
mp_printf(print, "DELETE_GLOBAL %s", qstr_str(qst));
294+
mp_printf(print, "DELETE_GLOBAL %q", qst);
295295
break;
296296

297297
case MP_BC_DUP_TOP:
@@ -506,12 +506,12 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
506506

507507
case MP_BC_IMPORT_NAME:
508508
DECODE_QSTR;
509-
mp_printf(print, "IMPORT_NAME '%s'", qstr_str(qst));
509+
mp_printf(print, "IMPORT_NAME '%q'", qst);
510510
break;
511511

512512
case MP_BC_IMPORT_FROM:
513513
DECODE_QSTR;
514-
mp_printf(print, "IMPORT_FROM '%s'", qstr_str(qst));
514+
mp_printf(print, "IMPORT_FROM '%q'", qst);
515515
break;
516516

517517
case MP_BC_IMPORT_STAR:
@@ -527,10 +527,10 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
527527
mp_printf(print, "STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
528528
} else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
529529
mp_uint_t op = ip[-1] - MP_BC_UNARY_OP_MULTI;
530-
mp_printf(print, "UNARY_OP " UINT_FMT " %s", op, qstr_str(mp_unary_op_method_name[op]));
530+
mp_printf(print, "UNARY_OP " UINT_FMT " %q", op, (qstr)mp_unary_op_method_name[op]);
531531
} else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
532532
mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
533-
mp_printf(print, "BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
533+
mp_printf(print, "BINARY_OP " UINT_FMT " %q", op, (qstr)mp_binary_op_method_name[op]);
534534
} else {
535535
mp_printf(print, "code %p, byte code 0x%02x not implemented\n", ip - 1, ip[-1]);
536536
assert(0);

0 commit comments

Comments
 (0)