Skip to content

Commit dddd25a

Browse files
committed
Combine similar strings to reduce size of translations
This is a slight trade-off with code size, in places where a "_varg" mp_raise variant is now used. The net savings on trinket_m0 is just 32 bytes. It also means that the translation will include the original English text, and cannot be translated. These are usually names of Python types such as int, set, or dict or special values such as "inf" or "Nan".
1 parent c394af4 commit dddd25a

16 files changed

Lines changed: 29 additions & 16 deletions

File tree

ports/atmel-samd/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
298298

299299
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
300300
if (self->len == 0) {
301-
mp_raise_IndexError(translate("pop from an empty PulseIn"));
301+
mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
302302
}
303303
common_hal_mcu_disable_interrupts();
304304
uint16_t value = self->buffer[self->start];

ports/atmel-samd/common-hal/rtc/RTC.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ int common_hal_rtc_get_calibration(void) {
6868

6969
void common_hal_rtc_set_calibration(int calibration) {
7070
if (calibration > 127 || calibration < -127) {
71+
#if CIRCUITPY_FULL_BUILD
7172
mp_raise_ValueError(translate("calibration value out of range +/-127"));
73+
#else
74+
mp_raise_ValueError(translate("calibration is out of range"));
75+
#endif
7276
}
7377

7478
hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1);

ports/cxd56/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
160160

161161
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
162162
if (self->len == 0) {
163-
mp_raise_IndexError(translate("pop from an empty PulseIn"));
163+
mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
164164
}
165165
common_hal_mcu_disable_interrupts();
166166
uint16_t value = self->buffer[self->start];

ports/mimxrt10xx/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
201201

202202
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
203203
// if (self->len == 0) {
204-
// mp_raise_IndexError(translate("pop from an empty PulseIn"));
204+
// mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
205205
// }
206206
// common_hal_mcu_disable_interrupts();
207207
// uint16_t value = self->buffer[self->start];

ports/nrf/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_
284284

285285
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
286286
if (self->len == 0) {
287-
mp_raise_IndexError(translate("pop from an empty PulseIn"));
287+
mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
288288
}
289289

290290
if ( !self->paused ) {

ports/stm/common-hal/pulseio/PulseIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_
258258

259259
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
260260
if (self->len == 0) {
261-
mp_raise_IndexError(translate("pop from an empty PulseIn"));
261+
mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
262262
}
263263
HAL_NVIC_DisableIRQ(self->irq);
264264
uint16_t value = self->buffer[self->start];

py/obj.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
263263
mp_raise_TypeError(translate("can't convert to int"));
264264
} else {
265265
mp_raise_TypeError_varg(
266-
translate("can't convert %s to int"), mp_obj_get_type_str(arg));
266+
translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "int");
267267
}
268268
}
269269
}
@@ -326,7 +326,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
326326
mp_raise_TypeError(translate("can't convert to float"));
327327
} else {
328328
mp_raise_TypeError_varg(
329-
translate("can't convert %s to float"), mp_obj_get_type_str(arg));
329+
translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "float");
330330
}
331331
}
332332

@@ -359,7 +359,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
359359
mp_raise_TypeError(translate("can't convert to complex"));
360360
} else {
361361
mp_raise_TypeError_varg(
362-
translate("can't convert %s to complex"), mp_obj_get_type_str(arg));
362+
translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "complex");
363363
}
364364
}
365365
}

py/objdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
313313
size_t cur = 0;
314314
mp_map_elem_t *next = dict_iter_next(self, &cur);
315315
if (next == NULL) {
316-
mp_raise_msg(&mp_type_KeyError, translate("popitem(): dictionary is empty"));
316+
mp_raise_msg_varg(&mp_type_KeyError, translate("pop from empty %s"), "dict");
317317
}
318318
self->map.used--;
319319
mp_obj_t items[] = {next->key, next->value};

py/objint.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
141141
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
142142
int cl = fpclassify(val);
143143
if (cl == FP_INFINITE) {
144-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, translate("can't convert inf to int")));
144+
mp_raise_OverflowError_varg(translate("can't convert %s to %s"), "inf", "int");
145145
} else if (cl == FP_NAN) {
146-
mp_raise_ValueError(translate("can't convert NaN to int"));
146+
mp_raise_ValueError_varg(translate("can't convert %s to %s"), "NaN", "int");
147147
} else {
148148
mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
149149
if (icl == MP_FP_CLASS_FIT_SMALLINT) {

py/objlist.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
274274
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
275275
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
276276
if (self->len == 0) {
277-
mp_raise_IndexError(translate("pop from empty list"));
277+
mp_raise_IndexError_varg(translate("pop from empty %s"), "list");
278278
}
279279
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
280280
mp_obj_t ret = self->items[index];

0 commit comments

Comments
 (0)