From 005226ae548a45bde03a2b2400998185aa68495e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 May 2018 12:28:36 -0500 Subject: [PATCH] uhashlib: some functions should refuse unicode for python3 compatibility .. this maybe should be subject to MICROPY_CPYTHON_COMPAT, except that is not defined in the main circuitpython ports so it would be a change that makes no difference. --- extmod/moduhashlib.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 3fad69247e..7599b84964 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -36,6 +36,14 @@ #include "lib/axtls/crypto/crypto.h" #endif +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"); + } +#endif +} + typedef struct _mp_obj_hash_t { mp_obj_base_t base; char state[0]; @@ -70,6 +78,7 @@ STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n #endif STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) { + check_not_unicode(arg); mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); @@ -80,6 +89,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update); #if MICROPY_PY_UHASHLIB_SHA1 STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) { + check_not_unicode(arg); mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);