core: introduce utils.decode_bytes

This instructs the low-level code to use mp_obj_new_str_copy directly
and avoids the usage of qstr_find_strn lookup.
decode_bytes
Pavol Rusnak 4 years ago
parent c3a61998cb
commit 355c7fcbd5
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "py/objstr.h"
#include "py/runtime.h"
#include "version.h"
@ -115,6 +116,18 @@ STATIC mp_obj_t mod_trezorutils_halt(size_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorutils_halt_obj, 0, 1,
mod_trezorutils_halt);
/// def decode_bytes(b: bytes) -> str:
/// """
/// Decodes string from bytes
/// """
STATIC mp_obj_t mod_trezorutils_decode_bytes(mp_obj_t b) {
mp_buffer_info_t bytes;
mp_get_buffer_raise(b, &bytes, MP_BUFFER_READ);
return mp_obj_new_str_copy(&mp_type_str, bytes.buf, bytes.len);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_decode_bytes_obj,
mod_trezorutils_decode_bytes);
#define PASTER(s) MP_QSTR_##s
#define MP_QSTR(s) PASTER(s)
@ -131,6 +144,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_consteq), MP_ROM_PTR(&mod_trezorutils_consteq_obj)},
{MP_ROM_QSTR(MP_QSTR_memcpy), MP_ROM_PTR(&mod_trezorutils_memcpy_obj)},
{MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&mod_trezorutils_halt_obj)},
{MP_ROM_QSTR(MP_QSTR_decode_bytes),
MP_ROM_PTR(&mod_trezorutils_decode_bytes_obj)},
// various built-in constants
{MP_ROM_QSTR(MP_QSTR_GITREV), MP_ROM_QSTR(MP_QSTR(GITREV))},
{MP_ROM_QSTR(MP_QSTR_VERSION_MAJOR), MP_ROM_INT(VERSION_MAJOR)},

@ -32,6 +32,13 @@ def halt(msg: str = None) -> None:
"""
Halts execution.
"""
# extmod/modtrezorutils/modtrezorutils.c
def decode_bytes(b: bytes) -> str:
"""
Decodes string from bytes
"""
GITREV: str
VERSION_MAJOR: int
VERSION_MINOR: int

@ -9,6 +9,7 @@ from trezorutils import ( # noqa: F401
VERSION_MINOR,
VERSION_PATCH,
consteq,
decode_bytes,
halt,
memcpy,
)

Loading…
Cancel
Save