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".
This commit is contained in:
Jeff Epler 2020-08-01 12:14:55 -05:00
parent c394af4128
commit dddd25a776
16 changed files with 29 additions and 16 deletions

View File

@ -298,7 +298,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from an empty PulseIn")); mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
} }
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
uint16_t value = self->buffer[self->start]; uint16_t value = self->buffer[self->start];

View File

@ -68,7 +68,11 @@ int common_hal_rtc_get_calibration(void) {
void common_hal_rtc_set_calibration(int calibration) { void common_hal_rtc_set_calibration(int calibration) {
if (calibration > 127 || calibration < -127) { if (calibration > 127 || calibration < -127) {
#if CIRCUITPY_FULL_BUILD
mp_raise_ValueError(translate("calibration value out of range +/-127")); mp_raise_ValueError(translate("calibration value out of range +/-127"));
#else
mp_raise_ValueError(translate("calibration is out of range"));
#endif
} }
hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1); hri_rtcmode0_write_FREQCORR_SIGN_bit(RTC, calibration < 0 ? 0 : 1);

View File

@ -160,7 +160,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from an empty PulseIn")); mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
} }
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
uint16_t value = self->buffer[self->start]; uint16_t value = self->buffer[self->start];

View File

@ -201,7 +201,7 @@ void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
// if (self->len == 0) { // if (self->len == 0) {
// mp_raise_IndexError(translate("pop from an empty PulseIn")); // mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
// } // }
// common_hal_mcu_disable_interrupts(); // common_hal_mcu_disable_interrupts();
// uint16_t value = self->buffer[self->start]; // uint16_t value = self->buffer[self->start];

View File

@ -284,7 +284,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from an empty PulseIn")); mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
} }
if ( !self->paused ) { if ( !self->paused ) {

View File

@ -258,7 +258,7 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from an empty PulseIn")); mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
} }
HAL_NVIC_DisableIRQ(self->irq); HAL_NVIC_DisableIRQ(self->irq);
uint16_t value = self->buffer[self->start]; uint16_t value = self->buffer[self->start];

View File

@ -263,7 +263,7 @@ mp_int_t mp_obj_get_int(mp_const_obj_t arg) {
mp_raise_TypeError(translate("can't convert to int")); mp_raise_TypeError(translate("can't convert to int"));
} else { } else {
mp_raise_TypeError_varg( mp_raise_TypeError_varg(
translate("can't convert %s to int"), mp_obj_get_type_str(arg)); translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "int");
} }
} }
} }
@ -326,7 +326,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
mp_raise_TypeError(translate("can't convert to float")); mp_raise_TypeError(translate("can't convert to float"));
} else { } else {
mp_raise_TypeError_varg( mp_raise_TypeError_varg(
translate("can't convert %s to float"), mp_obj_get_type_str(arg)); translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "float");
} }
} }
@ -359,7 +359,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
mp_raise_TypeError(translate("can't convert to complex")); mp_raise_TypeError(translate("can't convert to complex"));
} else { } else {
mp_raise_TypeError_varg( mp_raise_TypeError_varg(
translate("can't convert %s to complex"), mp_obj_get_type_str(arg)); translate("can't convert %s to %s"), mp_obj_get_type_str(arg), "complex");
} }
} }
} }

View File

@ -313,7 +313,7 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
size_t cur = 0; size_t cur = 0;
mp_map_elem_t *next = dict_iter_next(self, &cur); mp_map_elem_t *next = dict_iter_next(self, &cur);
if (next == NULL) { if (next == NULL) {
mp_raise_msg(&mp_type_KeyError, translate("popitem(): dictionary is empty")); mp_raise_msg_varg(&mp_type_KeyError, translate("pop from empty %s"), "dict");
} }
self->map.used--; self->map.used--;
mp_obj_t items[] = {next->key, next->value}; mp_obj_t items[] = {next->key, next->value};

View File

@ -141,9 +141,9 @@ STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
int cl = fpclassify(val); int cl = fpclassify(val);
if (cl == FP_INFINITE) { if (cl == FP_INFINITE) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, translate("can't convert inf to int"))); mp_raise_OverflowError_varg(translate("can't convert %s to %s"), "inf", "int");
} else if (cl == FP_NAN) { } else if (cl == FP_NAN) {
mp_raise_ValueError(translate("can't convert NaN to int")); mp_raise_ValueError_varg(translate("can't convert %s to %s"), "NaN", "int");
} else { } else {
mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val); mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
if (icl == MP_FP_CLASS_FIT_SMALLINT) { if (icl == MP_FP_CLASS_FIT_SMALLINT) {

View File

@ -274,7 +274,7 @@ STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list)); mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list); mp_obj_list_t *self = mp_instance_cast_to_native_base(args[0], &mp_type_list);
if (self->len == 0) { if (self->len == 0) {
mp_raise_IndexError(translate("pop from empty list")); mp_raise_IndexError_varg(translate("pop from empty %s"), "list");
} }
size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false); size_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
mp_obj_t ret = self->items[index]; mp_obj_t ret = self->items[index];

View File

@ -368,7 +368,7 @@ STATIC mp_obj_t set_pop(mp_obj_t self_in) {
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t obj = mp_set_remove_first(&self->set); mp_obj_t obj = mp_set_remove_first(&self->set);
if (obj == MP_OBJ_NULL) { if (obj == MP_OBJ_NULL) {
mp_raise_msg(&mp_type_KeyError, translate("pop from an empty set")); mp_raise_IndexError_varg(translate("pop from empty %s"), "set");
} }
return obj; return obj;
} }

View File

@ -1546,6 +1546,14 @@ NORETURN void mp_raise_IndexError(const compressed_string_t *msg) {
mp_raise_msg(&mp_type_IndexError, msg); mp_raise_msg(&mp_type_IndexError, msg);
} }
NORETURN void mp_raise_IndexError_varg(const compressed_string_t *fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_IndexError, fmt, argptr);
va_end(argptr);
nlr_raise(exception);
}
NORETURN void mp_raise_ValueError(const compressed_string_t *msg) { NORETURN void mp_raise_ValueError(const compressed_string_t *msg) {
mp_raise_msg(&mp_type_ValueError, msg); mp_raise_msg(&mp_type_ValueError, msg);
} }

View File

@ -160,6 +160,7 @@ NORETURN void mp_raise_AttributeError(const compressed_string_t *msg);
NORETURN void mp_raise_RuntimeError(const compressed_string_t *msg); NORETURN void mp_raise_RuntimeError(const compressed_string_t *msg);
NORETURN void mp_raise_ImportError(const compressed_string_t *msg); NORETURN void mp_raise_ImportError(const compressed_string_t *msg);
NORETURN void mp_raise_IndexError(const compressed_string_t *msg); NORETURN void mp_raise_IndexError(const compressed_string_t *msg);
NORETURN void mp_raise_IndexError_varg(const compressed_string_t *msg, ...);
NORETURN void mp_raise_OSError(int errno_); NORETURN void mp_raise_OSError(int errno_);
NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str); NORETURN void mp_raise_OSError_errno_str(int errno_, mp_obj_t str);
NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg); NORETURN void mp_raise_OSError_msg(const compressed_string_t *msg);

View File

@ -86,7 +86,7 @@ STATIC mp_obj_t i2cperipheral_i2c_peripheral_make_new(const mp_obj_type_t *type,
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
mp_int_t value; mp_int_t value;
if (!mp_obj_get_int_maybe(item, &value)) { if (!mp_obj_get_int_maybe(item, &value)) {
mp_raise_TypeError(translate("can't convert address to int")); mp_raise_TypeError_varg(translate("can't convert %s to %s"), "address", "int");
} }
if (value < 0x00 || value > 0x7f) { if (value < 0x00 || value > 0x7f) {
mp_raise_ValueError(translate("address out of bounds")); mp_raise_ValueError(translate("address out of bounds"));

View File

@ -133,7 +133,7 @@ STATIC mp_obj_t ps2io_ps2_obj_popleft(mp_obj_t self_in) {
int b = common_hal_ps2io_ps2_popleft(self); int b = common_hal_ps2io_ps2_popleft(self);
if (b < 0) { if (b < 0) {
mp_raise_IndexError(translate("Pop from an empty Ps2 buffer")); mp_raise_IndexError_varg(translate("pop from empty %s"), "Ps2 buffer");
} }
return MP_OBJ_NEW_SMALL_INT(b); return MP_OBJ_NEW_SMALL_INT(b);
} }

View File

@ -280,7 +280,7 @@ STATIC mp_obj_t time_mktime(mp_obj_t t) {
mp_obj_tuple_get(t, &len, &elem); mp_obj_tuple_get(t, &len, &elem);
if (len != 9) { if (len != 9) {
mp_raise_TypeError(translate("function takes exactly 9 arguments")); mp_raise_TypeError_varg(translate("function takes %d positional arguments but %d were given"), 9);
} }
if (mp_obj_get_int(elem[0]) < 2000) { if (mp_obj_get_int(elem[0]) < 2000) {