Skip to content

Compress all translated strings with Huffman coding. #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@
path = lib/tinyusb
url = https://github.com/hathach/tinyusb.git
branch = develop
[submodule "tools/huffman"]
path = tools/huffman
url = https://github.com/tannewt/huffman.git
12 changes: 7 additions & 5 deletions extmod/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "py/runtime.h"
#include "extmod/machine_i2c.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_MACHINE_I2C

typedef mp_machine_soft_i2c_obj_t machine_i2c_obj_t;
Expand Down Expand Up @@ -294,7 +296,7 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s
extern mp_obj_t MICROPY_PY_MACHINE_I2C_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
return MICROPY_PY_MACHINE_I2C_MAKE_NEW(type, n_args, n_kw, args);
#else
mp_raise_ValueError("invalid I2C peripheral");
mp_raise_ValueError(translate("invalid I2C peripheral"));
#endif
}
--n_args;
Expand Down Expand Up @@ -335,7 +337,7 @@ STATIC mp_obj_t machine_i2c_start(mp_obj_t self_in) {
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
if (i2c_p->start == NULL) {
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
}
int ret = i2c_p->start(self);
if (ret != 0) {
Expand All @@ -349,7 +351,7 @@ STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
if (i2c_p->stop == NULL) {
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
}
int ret = i2c_p->stop(self);
if (ret != 0) {
Expand All @@ -363,7 +365,7 @@ STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
if (i2c_p->read == NULL) {
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
}

// get the buffer to read into
Expand All @@ -387,7 +389,7 @@ STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)self->type->protocol;
if (i2c_p->write == NULL) {
mp_raise_msg(&mp_type_OSError, "I2C operation not supported");
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
}

// get the buffer to write from
Expand Down
2 changes: 1 addition & 1 deletion extmod/machine_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
STATIC uintptr_t machine_mem_get_addr(mp_obj_t addr_o, uint align) {
uintptr_t addr = mp_obj_int_get_truncated(addr_o);
if ((addr & (align - 1)) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, translate("address %08x is not aligned to %d bytes"), addr, align));
}
return addr;
}
Expand Down
12 changes: 7 additions & 5 deletions extmod/machine_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "py/runtime.h"
#include "extmod/machine_spi.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_MACHINE_SPI

// if a port didn't define MSB/LSB constants then provide them
Expand All @@ -52,7 +54,7 @@ mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args);
#else
mp_raise_ValueError("invalid SPI peripheral");
mp_raise_ValueError(translate("invalid SPI peripheral"));
#endif
}
--n_args;
Expand Down Expand Up @@ -119,7 +121,7 @@ STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp
mp_buffer_info_t dest;
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
if (src.len != dest.len) {
mp_raise_ValueError("buffers must be the same length");
mp_raise_ValueError(translate("buffers must be the same length"));
}
mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
return mp_const_none;
Expand Down Expand Up @@ -202,15 +204,15 @@ STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
self->spi.polarity = args[ARG_polarity].u_int;
self->spi.phase = args[ARG_phase].u_int;
if (args[ARG_bits].u_int != 8) {
mp_raise_ValueError("bits must be 8");
mp_raise_ValueError(translate("bits must be 8"));
}
if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
mp_raise_ValueError("firstbit must be MSB");
mp_raise_ValueError(translate("firstbit must be MSB"));
}
if (args[ARG_sck].u_obj == MP_OBJ_NULL
|| args[ARG_mosi].u_obj == MP_OBJ_NULL
|| args[ARG_miso].u_obj == MP_OBJ_NULL) {
mp_raise_ValueError("must specify all of sck/mosi/miso");
mp_raise_ValueError(translate("must specify all of sck/mosi/miso"));
}
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
Expand Down
2 changes: 1 addition & 1 deletion extmod/modframebuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
case FRAMEBUF_GS8:
break;
default:
mp_raise_ValueError("invalid format");
mp_raise_ValueError(translate("invalid format"));
}

return MP_OBJ_FROM_PTR(o);
Expand Down
8 changes: 4 additions & 4 deletions extmod/modubinascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
static void check_not_unicode(const mp_obj_t arg) {
#if MICROPY_CPYTHON_COMPAT
if (MP_OBJ_IS_STR(arg)) {
mp_raise_TypeError("a bytes-like object is required");
mp_raise_TypeError(translate("a bytes-like object is required"));
}
#endif
}
Expand Down Expand Up @@ -87,7 +87,7 @@ mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);

if ((bufinfo.len & 1) != 0) {
mp_raise_ValueError("odd-length string");
mp_raise_ValueError(translate("odd-length string"));
}
vstr_t vstr;
vstr_init_len(&vstr, bufinfo.len / 2);
Expand All @@ -98,7 +98,7 @@ mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
if (unichar_isxdigit(hex_ch)) {
hex_byte += unichar_xdigit_value(hex_ch);
} else {
mp_raise_ValueError("non-hex digit found");
mp_raise_ValueError(translate("non-hex digit found"));
}
if (i & 1) {
hex_byte <<= 4;
Expand Down Expand Up @@ -166,7 +166,7 @@ mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
}

if (nbits) {
mp_raise_ValueError("incorrect padding");
mp_raise_ValueError(translate("incorrect padding"));
}

return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Expand Down
12 changes: 7 additions & 5 deletions extmod/moductypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "py/objtuple.h"
#include "py/binary.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UCTYPES

/// \module uctypes - Access data structures in memory
Expand Down Expand Up @@ -117,7 +119,7 @@ typedef struct _mp_obj_uctypes_struct_t {
} mp_obj_uctypes_struct_t;

STATIC NORETURN void syntax_error(void) {
mp_raise_TypeError("syntax error in uctypes descriptor");
mp_raise_TypeError(translate("syntax error in uctypes descriptor"));
}

STATIC mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Expand Down Expand Up @@ -214,7 +216,7 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_
// but scalar structure field is lowered into native Python int, so all
// type info is lost. So, we cannot say if it's scalar type description,
// or such lowered scalar.
mp_raise_TypeError("Cannot unambiguously get sizeof scalar");
mp_raise_TypeError(translate("Cannot unambiguously get sizeof scalar"));
}
syntax_error();
}
Expand Down Expand Up @@ -392,7 +394,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set

// TODO: Support at least OrderedDict in addition
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
mp_raise_TypeError("struct: no fields");
mp_raise_TypeError(translate("struct: no fields"));
}

mp_obj_t deref = mp_obj_dict_get(self->desc, MP_OBJ_NEW_QSTR(attr));
Expand Down Expand Up @@ -525,7 +527,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
} else {
// load / store
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
mp_raise_TypeError("struct: cannot index");
mp_raise_TypeError(translate("struct: cannot index"));
}

mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
Expand All @@ -539,7 +541,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
uint val_type = GET_TYPE(arr_sz, VAL_TYPE_BITS);
arr_sz &= VALUE_MASK(VAL_TYPE_BITS);
if (index >= arr_sz) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "struct: index out of range"));
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, translate("struct: index out of range")));
}

if (t->len == 2) {
Expand Down
4 changes: 3 additions & 1 deletion extmod/moduhashlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#include "py/runtime.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UHASHLIB

#if MICROPY_PY_UHASHLIB_SHA256
Expand Down Expand Up @@ -97,7 +99,7 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
static void check_not_unicode(const mp_obj_t arg) {
#if MICROPY_CPYTHON_COMPAT
if (MP_OBJ_IS_STR(arg)) {
mp_raise_TypeError("a bytes-like object is required");
mp_raise_TypeError(translate("a bytes-like object is required"));
}
#endif
}
Expand Down
6 changes: 4 additions & 2 deletions extmod/moduheapq.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
#include "py/objlist.h"
#include "py/runtime.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UHEAPQ

// the algorithm here is modelled on CPython's heapq.py

STATIC mp_obj_list_t *get_heap(mp_obj_t heap_in) {
if (!MP_OBJ_IS_TYPE(heap_in, &mp_type_list)) {
mp_raise_TypeError("heap must be a list");
mp_raise_TypeError(translate("heap must be a list"));
}
return MP_OBJ_TO_PTR(heap_in);
}
Expand Down Expand Up @@ -81,7 +83,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_uheapq_heappush_obj, mod_uheapq_heappush);
STATIC mp_obj_t mod_uheapq_heappop(mp_obj_t heap_in) {
mp_obj_list_t *heap = get_heap(heap_in);
if (heap->len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, translate("empty heap")));
}
mp_obj_t item = heap->items[0];
heap->len -= 1;
Expand Down
4 changes: 3 additions & 1 deletion extmod/modujson.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "py/runtime.h"
#include "py/stream.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UJSON

STATIC mp_obj_t mod_ujson_dump(mp_obj_t obj, mp_obj_t stream) {
Expand Down Expand Up @@ -276,7 +278,7 @@ STATIC mp_obj_t mod_ujson_load(mp_obj_t stream_obj) {
return stack_top;

fail:
mp_raise_ValueError("syntax error in JSON");
mp_raise_ValueError(translate("syntax error in JSON"));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_load_obj, mod_ujson_load);

Expand Down
4 changes: 2 additions & 2 deletions extmod/modure.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) {
mp_obj_t s = mp_obj_new_str_of_type(str_type, (const byte*)subj.begin, caps[0] - subj.begin);
mp_obj_list_append(retval, s);
if (self->re.sub > 0) {
mp_raise_NotImplementedError("Splitting with sub-captures");
mp_raise_NotImplementedError(translate("Splitting with sub-captures"));
}
subj.begin = caps[1];
if (maxsplit > 0 && --maxsplit == 0) {
Expand Down Expand Up @@ -204,7 +204,7 @@ STATIC mp_obj_t mod_re_compile(size_t n_args, const mp_obj_t *args) {
int error = re1_5_compilecode(&o->re, re_str);
if (error != 0) {
error:
mp_raise_ValueError("Error in regex");
mp_raise_ValueError(translate("Error in regex"));
}
if (flags & FLAG_DEBUG) {
re1_5_dumpcode(&o->re);
Expand Down
6 changes: 4 additions & 2 deletions extmod/modussl_axtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "py/runtime.h"
#include "py/stream.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_USSL && MICROPY_SSL_AXTLS

#include "ssl.h"
Expand Down Expand Up @@ -76,13 +78,13 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
const byte *data = (const byte*)mp_obj_str_get_data(args->key.u_obj, &len);
int res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_RSA_KEY, data, len, NULL);
if (res != SSL_OK) {
mp_raise_ValueError("invalid key");
mp_raise_ValueError(translate("invalid key"));
}

data = (const byte*)mp_obj_str_get_data(args->cert.u_obj, &len);
res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_X509_CERT, data, len, NULL);
if (res != SSL_OK) {
mp_raise_ValueError("invalid cert");
mp_raise_ValueError(translate("invalid cert"));
}
}

Expand Down
8 changes: 5 additions & 3 deletions extmod/modutimeq.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "py/runtime.h"
#include "py/smallint.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UTIMEQ

#define MODULO MICROPY_PY_UTIME_TICKS_PERIOD
Expand Down Expand Up @@ -126,7 +128,7 @@ STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) {
mp_obj_t heap_in = args[0];
mp_obj_utimeq_t *heap = get_heap(heap_in);
if (heap->len == heap->alloc) {
mp_raise_IndexError("queue overflow");
mp_raise_IndexError(translate("queue overflow"));
}
mp_uint_t l = heap->len;
heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]);
Expand All @@ -142,7 +144,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_utimeq_heappush_obj, 4, 4, mod_ut
STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) {
mp_obj_utimeq_t *heap = get_heap(heap_in);
if (heap->len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
mp_raise_IndexError(translate("empty heap"));
}
mp_obj_list_t *ret = MP_OBJ_TO_PTR(list_ref);
if (!MP_OBJ_IS_TYPE(list_ref, &mp_type_list) || ret->len < 3) {
Expand All @@ -167,7 +169,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop);
STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) {
mp_obj_utimeq_t *heap = get_heap(heap_in);
if (heap->len == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap"));
mp_raise_IndexError(translate("empty heap"));
}

struct qentry *item = &heap->items[0];
Expand Down
4 changes: 3 additions & 1 deletion extmod/moduzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "py/stream.h"
#include "py/mperrno.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_UZLIB

#include "../../lib/uzlib/src/tinf.h"
Expand Down Expand Up @@ -92,7 +94,7 @@ STATIC mp_obj_t decompio_make_new(const mp_obj_type_t *type, size_t n_args, size
dict_opt = uzlib_zlib_parse_header(&o->decomp);
if (dict_opt < 0) {
header_error:
mp_raise_ValueError("compression header");
mp_raise_ValueError(translate("compression header"));
}
dict_sz = 1 << dict_opt;
} else {
Expand Down
4 changes: 3 additions & 1 deletion extmod/uos_dupterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "py/stream.h"
#include "lib/utils/interrupt_char.h"

#include "supervisor/shared/translate.h"

#if MICROPY_PY_OS_DUPTERM

void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
Expand Down Expand Up @@ -115,7 +117,7 @@ STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) {
}

if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) {
mp_raise_ValueError("invalid dupterm index");
mp_raise_ValueError(translate("invalid dupterm index"));
}

mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]);
Expand Down
1 change: 1 addition & 0 deletions extmod/vfs_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#if defined(MICROPY_VFS_POSIX) && MICROPY_VFS_POSIX

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
Expand Down
Loading