Skip to content

Commit 1fb03b7

Browse files
authored
Merge pull request #7175 from jepler/simplify-messages-save-flash
Simplify argument checking to reduce translated strings
2 parents ae2bbbb + 319d9b0 commit 1fb03b7

File tree

9 files changed

+14
-87
lines changed

9 files changed

+14
-87
lines changed

locale/circuitpython.pot

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ msgstr ""
110110
msgid "%q index out of range"
111111
msgstr ""
112112

113-
#: py/obj.c
114-
msgid "%q indices must be integers, not %s"
115-
msgstr ""
116-
117113
#: shared-module/bitbangio/SPI.c
118114
msgid "%q init failed"
119115
msgstr ""
@@ -2341,10 +2337,6 @@ msgstr ""
23412337
msgid "WatchDogTimer.mode cannot be changed once set to WatchDogMode.RESET"
23422338
msgstr ""
23432339

2344-
#: shared-bindings/watchdog/WatchDogTimer.c
2345-
msgid "WatchDogTimer.timeout must be greater than 0"
2346-
msgstr ""
2347-
23482340
#: py/builtinhelp.c
23492341
#, c-format
23502342
msgid ""
@@ -3166,10 +3158,6 @@ msgstr ""
31663158
msgid "index out of range"
31673159
msgstr ""
31683160

3169-
#: py/obj.c
3170-
msgid "indices must be integers"
3171-
msgstr ""
3172-
31733161
#: extmod/ulab/code/ndarray.c
31743162
msgid "indices must be integers, slices, or Boolean lists"
31753163
msgstr ""
@@ -3586,10 +3574,6 @@ msgstr ""
35863574
msgid "no such attribute"
35873575
msgstr ""
35883576

3589-
#: shared-bindings/usb_hid/__init__.c
3590-
msgid "non-Device in %q"
3591-
msgstr ""
3592-
35933577
#: ports/espressif/common-hal/_bleio/Connection.c
35943578
#: ports/nrf/common-hal/_bleio/Connection.c
35953579
msgid "non-UUID found in service_uuids_whitelist"
@@ -3863,11 +3847,6 @@ msgstr ""
38633847
msgid "relative import"
38643848
msgstr ""
38653849

3866-
#: py/obj.c
3867-
#, c-format
3868-
msgid "requested length %d but object has length %d"
3869-
msgstr ""
3870-
38713850
#: extmod/ulab/code/ndarray_operators.c
38723851
msgid "results cannot be cast to specified type"
38733852
msgstr ""
@@ -3931,10 +3910,6 @@ msgstr ""
39313910
msgid "sign not allowed with integer format specifier 'c'"
39323911
msgstr ""
39333912

3934-
#: py/objstr.c
3935-
msgid "single '}' encountered in format string"
3936-
msgstr ""
3937-
39383913
#: extmod/ulab/code/ulab_tools.c
39393914
msgid "size is defined for ndarrays only"
39403915
msgstr ""
@@ -4047,10 +4022,6 @@ msgstr ""
40474022
msgid "syntax error in uctypes descriptor"
40484023
msgstr ""
40494024

4050-
#: shared-bindings/touchio/TouchIn.c
4051-
msgid "threshold must be in the range 0-65536"
4052-
msgstr ""
4053-
40544025
#: shared-bindings/time/__init__.c
40554026
msgid "time.struct_time() takes a 9-sequence"
40564027
msgstr ""
@@ -4062,10 +4033,6 @@ msgstr ""
40624033
msgid "timeout duration exceeded the maximum supported value"
40634034
msgstr ""
40644035

4065-
#: shared-bindings/busio/UART.c
4066-
msgid "timeout must be 0.0-100.0 seconds"
4067-
msgstr ""
4068-
40694036
#: ports/nrf/common-hal/_bleio/Adapter.c
40704037
msgid "timeout must be < 655.35 secs"
40714038
msgstr ""
@@ -4119,10 +4086,6 @@ msgstr ""
41194086
msgid "trapz is defined for 1D iterables"
41204087
msgstr ""
41214088

4122-
#: py/obj.c
4123-
msgid "tuple/list has wrong length"
4124-
msgstr ""
4125-
41264089
#: ports/espressif/common-hal/canio/CAN.c
41274090
#, c-format
41284091
msgid "twai_driver_install returned esp-idf error #%d"
@@ -4201,7 +4164,8 @@ msgid "unknown type '%q'"
42014164
msgstr ""
42024165

42034166
#: py/objstr.c
4204-
msgid "unmatched '{' in format"
4167+
#, c-format
4168+
msgid "unmatched '%c' in format"
42054169
msgstr ""
42064170

42074171
#: py/objtype.c py/runtime.c
@@ -4272,10 +4236,6 @@ msgstr ""
42724236
msgid "watchdog not initialized"
42734237
msgstr ""
42744238

4275-
#: shared-bindings/watchdog/WatchDogTimer.c
4276-
msgid "watchdog timeout must be greater than 0"
4277-
msgstr ""
4278-
42794239
#: shared-bindings/is31fl3741/FrameBuffer.c
42804240
msgid "width must be greater than zero"
42814241
msgstr ""

py/obj.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,7 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
488488
void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
489489
size_t seq_len;
490490
mp_obj_get_array(o, &seq_len, items);
491-
if (seq_len != len) {
492-
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
493-
mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
494-
#else
495-
mp_raise_ValueError_varg(
496-
MP_ERROR_TEXT("requested length %d but object has length %d"), (int)len, (int)seq_len);
497-
#endif
498-
}
491+
mp_arg_validate_length(seq_len, len, mp_obj_get_type(o)->name);
499492
}
500493

501494
// is_slice determines whether the index is a slice index
@@ -504,13 +497,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
504497
if (mp_obj_is_small_int(index)) {
505498
i = MP_OBJ_SMALL_INT_VALUE(index);
506499
} else if (!mp_obj_get_int_maybe(index, &i)) {
507-
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
508-
mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
509-
#else
510-
mp_raise_TypeError_varg(
511-
MP_ERROR_TEXT("%q indices must be integers, not %s"),
512-
type->name, mp_obj_get_type_str(index));
513-
#endif
500+
mp_raise_TypeError_varg(translate("%q must be of type %q"), MP_QSTR_index, MP_QSTR_int);
514501
}
515502

516503
if (i < 0) {
@@ -523,14 +510,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
523510
i = len;
524511
}
525512
} else {
526-
if (i < 0 || (mp_uint_t)i >= len) {
527-
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
528-
mp_raise_IndexError(MP_ERROR_TEXT("index out of range"));
529-
#else
530-
mp_raise_msg_varg(&mp_type_IndexError,
531-
MP_ERROR_TEXT("%q index out of range"), type->name);
532-
#endif
533-
}
513+
mp_arg_validate_index_range(i, 0, len - 1, MP_QSTR_index);
534514
}
535515

536516
// By this point 0 <= i <= len and so fits in a size_t

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
986986
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
987987
terse_str_format_value_error();
988988
#else
989-
mp_raise_ValueError(MP_ERROR_TEXT("single '}' encountered in format string"));
989+
mp_raise_ValueError_varg(MP_ERROR_TEXT("unmatched '%c' in format"), '}');
990990
#endif
991991
}
992992
if (*str != '{') {
@@ -1063,7 +1063,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
10631063
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
10641064
terse_str_format_value_error();
10651065
#else
1066-
mp_raise_ValueError(MP_ERROR_TEXT("unmatched '{' in format"));
1066+
mp_raise_ValueError_varg(MP_ERROR_TEXT("unmatched '%c' in format"), '{');
10671067
#endif
10681068
}
10691069
if (*str != '}') {

py/objtype.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11811181
mp_obj_t *bases_items;
11821182
mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items);
11831183
for (size_t i = 0; i < bases_len; i++) {
1184-
if (!mp_obj_is_type(bases_items[i], &mp_type_type)) {
1185-
mp_raise_TypeError(MP_ERROR_TEXT("type is not an acceptable base type"));
1186-
}
1184+
mp_arg_validate_type(bases_items[i], &mp_type_type, MP_QSTR___class__);
11871185
mp_obj_type_t *t = MP_OBJ_TO_PTR(bases_items[i]);
11881186
// TODO: Verify with CPy, tested on function type
11891187
if (t->make_new == NULL) {

shared-bindings/busio/UART.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj;
9191

9292
#if CIRCUITPY_BUSIO_UART
9393
STATIC void validate_timeout(mp_float_t timeout) {
94-
if (timeout < (mp_float_t)0.0f || timeout > (mp_float_t)100.0f) {
95-
mp_raise_ValueError(translate("timeout must be 0.0-100.0 seconds"));
96-
}
94+
mp_arg_validate_int_range((int)timeout, 0, 100, MP_QSTR_timeout);
9795
}
9896
#endif // CIRCUITPY_BUSIO_UART
9997

shared-bindings/terminalio/Terminal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
//| scroll_area: displayio.TileGrid,
6262
//| font: fontio.BuiltinFont,
6363
//| *,
64-
//| status_bar: displayio.TileGrid = None
64+
//| status_bar: Optional[displayio.TileGrid] = None
6565
//| ) -> None:
6666
//| """Terminal manages tile indices and cursor position based on VT100 commands. The font should be
6767
//| a `fontio.BuiltinFont` and the TileGrid's bitmap should match the font's bitmap."""

shared-bindings/touchio/TouchIn.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ STATIC mp_obj_t touchio_touchin_obj_set_threshold(mp_obj_t self_in, mp_obj_t thr
159159
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
160160
check_for_deinit(self);
161161
uint32_t new_threshold = mp_obj_get_int(threshold_obj);
162-
if (new_threshold < 0 || new_threshold > UINT16_MAX) {
163-
// I would use MP_STRINGIFY(UINT16_MAX), but that prints "0xffff" instead of 65536.
164-
mp_raise_ValueError(translate("threshold must be in the range 0-65536"));
165-
}
162+
mp_arg_validate_int_range(new_threshold, 0, UINT16_MAX, MP_QSTR_threshold);
166163
common_hal_touchio_touchin_set_threshold(self, new_threshold);
167164
return mp_const_none;
168165
}

shared-bindings/usb_hid/__init__.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ STATIC mp_obj_t usb_hid_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t
123123
const mp_int_t len = mp_obj_get_int(mp_obj_len(devices));
124124
for (mp_int_t i = 0; i < len; i++) {
125125
mp_obj_t item = mp_obj_subscr(devices, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL);
126-
if (!mp_obj_is_type(item, &usb_hid_device_type)) {
127-
mp_raise_ValueError_varg(translate("non-Device in %q"), MP_QSTR_devices);
128-
}
126+
mp_arg_validate_type(item, &usb_hid_device_type, MP_QSTR___class__);
129127
}
130128

131129
uint8_t boot_device =

shared-bindings/watchdog/WatchDogTimer.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_timeout(mp_obj_t self_in, mp_obj_
9494
watchdog_watchdogtimer_obj_t *self = MP_OBJ_TO_PTR(self_in);
9595
mp_float_t timeout = mp_obj_get_float(timeout_obj);
9696

97-
if (timeout <= 0) {
98-
mp_raise_ValueError(translate("watchdog timeout must be greater than 0"));
99-
}
97+
mp_arg_validate_int_min((int)timeout, 0, MP_QSTR_timeout);
10098

10199
common_hal_watchdog_set_timeout(self, timeout);
102100
return mp_const_none;
@@ -136,9 +134,7 @@ STATIC mp_obj_t watchdog_watchdogtimer_obj_set_mode(mp_obj_t self_in, mp_obj_t m
136134

137135
// When setting the mode, the timeout value must be greater than zero
138136
if (new_mode == WATCHDOGMODE_RESET || new_mode == WATCHDOGMODE_RAISE) {
139-
if (current_timeout <= 0) {
140-
mp_raise_ValueError(translate("WatchDogTimer.timeout must be greater than 0"));
141-
}
137+
mp_arg_validate_int_min((int)current_timeout, 0, MP_QSTR_timeout);
142138
}
143139

144140
// Don't allow changing the mode once the watchdog timer has been started

0 commit comments

Comments
 (0)