Skip to content

Commit b2b8520

Browse files
committed
Always use preprocessor for MICROPY_ERROR_REPORTING
This ensures that only the translate("") alternative that will be used is seen after preprocessing. Improves the quality of the Huffman encoding and reduces binary size slightly. Also makes one "enhanced" error message only occur when ERROR_REPORTING_DETAILED: Instead of the word-for-word python3 error message "Type object has no attribute '%q'", the message will be "'type' object has no attribute '%q'". Also reduces binary size. (that's rolled into this commit as it was right next to a change to use the preprocessor for MICROPY_ERROR_REPORTING) Note that the odd semicolon after "value_error:" in parsenum.c is necessary due to a detail of the C grammar, in which a declaration cannot follow a label directly.
1 parent c06fc8e commit b2b8520

File tree

11 files changed

+178
-175
lines changed

11 files changed

+178
-175
lines changed

py/argcheck.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
9999
mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP);
100100
if (kw == NULL) {
101101
if (allowed[i].flags & MP_ARG_REQUIRED) {
102-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
102+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
103103
mp_arg_error_terse_mismatch();
104-
} else {
104+
#else
105105
mp_raise_TypeError_varg(
106106
translate("'%q' argument required"), allowed[i].qst);
107-
}
107+
#endif
108108
}
109109
out_vals[i] = allowed[i].defval;
110110
continue;
@@ -124,20 +124,20 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
124124
}
125125
if (pos_found < n_pos) {
126126
extra_positional:
127-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
127+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
128128
mp_arg_error_terse_mismatch();
129-
} else {
129+
#else
130130
// TODO better error message
131131
mp_raise_TypeError(translate("extra positional arguments given"));
132-
}
132+
#endif
133133
}
134134
if (kws_found < kws->used) {
135-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
135+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
136136
mp_arg_error_terse_mismatch();
137-
} else {
137+
#else
138138
// TODO better error message
139139
mp_raise_TypeError(translate("extra keyword arguments given"));
140-
}
140+
#endif
141141
}
142142
}
143143

py/builtinimport.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
426426
mp_module_call_init(mod_name, module_obj);
427427
} else {
428428
// couldn't find the file, so fail
429-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
429+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
430430
mp_raise_ImportError(translate("module not found"));
431-
} else {
431+
#else
432432
mp_raise_msg_varg(&mp_type_ImportError,
433433
translate("no module named '%q'"), mod_name);
434-
}
434+
#endif
435435
}
436436
} else {
437437
// found the file, so get the module
@@ -538,12 +538,12 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
538538
#endif
539539

540540
// Couldn't find the module, so fail
541-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
541+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
542542
mp_raise_msg(&mp_type_ImportError, translate("module not found"));
543-
} else {
543+
#else
544544
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError,
545545
translate("no module named '%q'"), module_name_qstr));
546-
}
546+
#endif
547547
}
548548

549549
#endif // MICROPY_ENABLE_EXTERNAL_IMPORT

py/compile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,21 +2486,21 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
24862486
compile_node(comp, pn_i);
24872487
if (is_dict) {
24882488
if (!is_key_value) {
2489-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
2489+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
24902490
compile_syntax_error(comp, (mp_parse_node_t)pns, translate("invalid syntax"));
2491-
} else {
2491+
#else
24922492
compile_syntax_error(comp, (mp_parse_node_t)pns, translate("expecting key:value for dict"));
2493-
}
2493+
#endif
24942494
return;
24952495
}
24962496
EMIT(store_map);
24972497
} else {
24982498
if (is_key_value) {
2499-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
2499+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
25002500
compile_syntax_error(comp, (mp_parse_node_t)pns, translate("invalid syntax"));
2501-
} else {
2501+
#else
25022502
compile_syntax_error(comp, (mp_parse_node_t)pns, translate("expecting just a value for set"));
2503-
}
2503+
#endif
25042504
return;
25052505
}
25062506
}

py/modbuiltins.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
346346
}
347347
}
348348

349-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
349+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
350350
mp_raise_TypeError(translate("ord expects a character"));
351-
} else {
351+
#else
352352
mp_raise_TypeError_varg(
353353
translate("ord() expected a character, but string of length %d found"), (int)len);
354-
}
354+
#endif
355355
}
356356
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
357357

py/modsys.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,11 @@ STATIC const MP_DEFINE_STR_OBJ(platform_obj, MICROPY_PY_SYS_PLATFORM);
9292

9393
// exit([retval]): raise SystemExit, with optional argument given to the exception
9494
STATIC mp_obj_t mp_sys_exit(size_t n_args, const mp_obj_t *args) {
95-
mp_obj_t exc;
9695
if (n_args == 0) {
97-
exc = mp_obj_new_exception(&mp_type_SystemExit);
96+
nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
9897
} else {
9998
mp_raise_arg1(&mp_type_SystemExit, args[0]);
10099
}
101-
nlr_raise(exc);
102100
}
103101
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
104102

py/obj.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
262262
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_int)) {
263263
return mp_obj_int_get_checked(arg);
264264
} else {
265-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
265+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
266266
mp_raise_TypeError_varg(translate("can't convert to %q"), MP_QSTR_int);
267-
} else {
267+
#else
268268
mp_raise_TypeError_varg(
269269
translate("can't convert %q to %q"), mp_obj_get_type_qstr(arg), MP_QSTR_int);
270-
}
270+
#endif
271271
}
272272
}
273273

@@ -325,12 +325,12 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
325325
mp_float_t val;
326326

327327
if (!mp_obj_get_float_maybe(arg, &val)) {
328-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
328+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
329329
mp_raise_TypeError_varg(translate("can't convert to %q"), MP_QSTR_float);
330-
} else {
330+
#else
331331
mp_raise_TypeError_varg(
332332
translate("can't convert %q to %q"), mp_obj_get_type_qstr(arg), MP_QSTR_float);
333-
}
333+
#endif
334334
}
335335

336336
return val;
@@ -358,12 +358,12 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
358358
} else if (MP_OBJ_IS_TYPE(arg, &mp_type_complex)) {
359359
mp_obj_complex_get(arg, real, imag);
360360
} else {
361-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
361+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
362362
mp_raise_TypeError_varg(translate("can't convert to %q"), MP_QSTR_complex);
363-
} else {
363+
#else
364364
mp_raise_TypeError_varg(
365365
translate("can't convert %q to %q"), mp_obj_get_type_qstr(arg), MP_QSTR_complex);
366-
}
366+
#endif
367367
}
368368
}
369369
#endif
@@ -376,12 +376,12 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
376376
} else if (MP_OBJ_IS_TYPE(o, &mp_type_list)) {
377377
mp_obj_list_get(o, len, items);
378378
} else {
379-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
379+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
380380
mp_raise_TypeError(translate("expected tuple/list"));
381-
} else {
381+
#else
382382
mp_raise_TypeError_varg(
383383
translate("object '%q' is not a tuple or list"), mp_obj_get_type_qstr(o));
384-
}
384+
#endif
385385
}
386386
}
387387

@@ -390,12 +390,12 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
390390
size_t seq_len;
391391
mp_obj_get_array(o, &seq_len, items);
392392
if (seq_len != len) {
393-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
393+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
394394
mp_raise_ValueError(translate("tuple/list has wrong length"));
395-
} else {
395+
#else
396396
mp_raise_ValueError_varg(translate("requested length %d but object has length %d"),
397397
(int)len, (int)seq_len);
398-
}
398+
#endif
399399
}
400400
}
401401

@@ -405,13 +405,13 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
405405
if (MP_OBJ_IS_SMALL_INT(index)) {
406406
i = MP_OBJ_SMALL_INT_VALUE(index);
407407
} else if (!mp_obj_get_int_maybe(index, &i)) {
408-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
408+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
409409
mp_raise_TypeError(translate("indices must be integers"));
410-
} else {
410+
#else
411411
mp_raise_TypeError_varg(
412412
translate("%q indices must be integers, not %q"),
413413
type->name, mp_obj_get_type_qstr(index));
414-
}
414+
#endif
415415
}
416416

417417
if (i < 0) {
@@ -425,12 +425,12 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
425425
}
426426
} else {
427427
if (i < 0 || (mp_uint_t)i >= len) {
428-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
428+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
429429
mp_raise_IndexError(translate("index out of range"));
430-
} else {
430+
#else
431431
mp_raise_msg_varg(&mp_type_IndexError,
432432
translate("%q index out of range"), type->name);
433-
}
433+
#endif
434434
}
435435
}
436436

@@ -460,12 +460,12 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) {
460460
mp_obj_t mp_obj_len(mp_obj_t o_in) {
461461
mp_obj_t len = mp_obj_len_maybe(o_in);
462462
if (len == MP_OBJ_NULL) {
463-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
463+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
464464
mp_raise_TypeError(translate("object has no len"));
465-
} else {
465+
#else
466466
mp_raise_TypeError_varg(
467467
translate("object of type '%q' has no len()"), mp_obj_get_type_qstr(o_in));
468-
}
468+
#endif
469469
} else {
470470
return len;
471471
}
@@ -503,26 +503,26 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
503503
}
504504
}
505505
if (value == MP_OBJ_NULL) {
506-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
506+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
507507
mp_raise_TypeError(translate("object does not support item deletion"));
508-
} else {
508+
#else
509509
mp_raise_TypeError_varg(
510510
translate("'%q' object does not support item deletion"), mp_obj_get_type_qstr(base));
511-
}
511+
#endif
512512
} else if (value == MP_OBJ_SENTINEL) {
513-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
513+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
514514
mp_raise_TypeError(translate("object is not subscriptable"));
515-
} else {
515+
#else
516516
mp_raise_TypeError_varg(
517517
translate("'%q' object is not subscriptable"), mp_obj_get_type_qstr(base));
518-
}
518+
#endif
519519
} else {
520-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
520+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
521521
mp_raise_TypeError(translate("object does not support item assignment"));
522-
} else {
522+
#else
523523
mp_raise_TypeError_varg(
524524
translate("'%q' object does not support item assignment"), mp_obj_get_type_qstr(base));
525-
}
525+
#endif
526526
}
527527
}
528528

py/objnamedtuple.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, const
102102
n_kw = kw_args->used;
103103
}
104104
if (n_args + n_kw != num_fields) {
105-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
105+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
106106
mp_arg_error_terse_mismatch();
107-
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
107+
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
108108
mp_raise_TypeError_varg(
109109
translate("function takes %d positional arguments but %d were given"),
110110
num_fields, n_args + n_kw);
111-
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED) {
111+
#else
112112
mp_raise_TypeError_varg(
113113
translate("%q() takes %d positional arguments but %d were given"),
114114
type->base.name, num_fields, n_args + n_kw);
115-
}
115+
#endif
116116
}
117117

118118
// Create a tuple and set the type to this namedtuple
@@ -128,20 +128,20 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, const
128128
qstr kw = mp_obj_str_get_qstr(kw_args->table[i].key);
129129
size_t id = mp_obj_namedtuple_find_field(type, kw);
130130
if (id == (size_t)-1) {
131-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
131+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
132132
mp_arg_error_terse_mismatch();
133-
} else {
133+
#else
134134
mp_raise_TypeError_varg(
135135
translate("unexpected keyword argument '%q'"), kw);
136-
}
136+
#endif
137137
}
138138
if (tuple->items[id] != MP_OBJ_NULL) {
139-
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
139+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
140140
mp_arg_error_terse_mismatch();
141-
} else {
141+
#else
142142
mp_raise_TypeError_varg(
143143
translate("function got multiple values for argument '%q'"), kw);
144-
}
144+
#endif
145145
}
146146
tuple->items[id] = kw_args->table[i].value;
147147
}

0 commit comments

Comments
 (0)