diff --git a/python/port/port.cpp b/python/port/port.cpp index 97fffd671..a91fe7e86 100644 --- a/python/port/port.cpp +++ b/python/port/port.cpp @@ -7,6 +7,13 @@ #include #include +/* py/parsenum.h is a C header which uses C keyword restrict. + * It does not exist in C++ so we define it here in order to be able to include + * py/parsenum.h header. */ +#ifdef __cplusplus +#define restrict // disable +#endif + extern "C" { #include "py/builtin.h" #include "py/compile.h" @@ -15,6 +22,7 @@ extern "C" { #include "py/mperrno.h" #include "py/mphal.h" #include "py/nlr.h" +#include "py/parsenum.h" #include "py/repl.h" #include "py/runtime.h" #include "py/stackctrl.h" @@ -206,11 +214,11 @@ KDColor MicroPython::ColorParser::ParseColor(mp_obj_t input, ColorModes ColorMod if(l != 7){ mp_raise_ValueError("RGB hex values are 6 bytes long"); } - uint32_t ColorInt = mp_obj_get_int(mp_obj_new_int_via_str(color+1, 16)); + uint32_t ColorInt = mp_obj_get_int(mp_parse_num_integer(color+1, strlen(color+1), 16, NULL)); return KDColor::RGB24(ColorInt); } - mp_float_t GreyLevel = mp_obj_float_get(mp_obj_new_float_via_str(color)); + mp_float_t GreyLevel = mp_obj_float_get(mp_parse_num_decimal(color, strlen(color), false, false, NULL)); if(GreyLevel >= 0 && GreyLevel <= 1){ return KDColor::RGB888( 255 * (float) GreyLevel, diff --git a/python/src/py/obj.h b/python/src/py/obj.h index e2d2b584a..5b54892ce 100644 --- a/python/src/py/obj.h +++ b/python/src/py/obj.h @@ -90,7 +90,6 @@ static inline bool mp_obj_is_qstr(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 2); } #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2) #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2)) -mp_obj_t mp_obj_new_int_via_str(const char* value, int base); #if MICROPY_PY_BUILTINS_FLOAT #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj) @@ -101,7 +100,6 @@ extern const struct _mp_obj_float_t mp_const_float_pi_obj; #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float) mp_float_t mp_obj_float_get(mp_obj_t self_in); mp_obj_t mp_obj_new_float(mp_float_t value); -mp_obj_t mp_obj_new_float_via_str(const char* value); #endif static inline bool mp_obj_is_obj(mp_const_obj_t o) diff --git a/python/src/py/objfloat.c b/python/src/py/objfloat.c index c638796ac..3da549bb2 100644 --- a/python/src/py/objfloat.c +++ b/python/src/py/objfloat.c @@ -201,24 +201,6 @@ mp_obj_t mp_obj_new_float(mp_float_t value) { return MP_OBJ_FROM_PTR(o); } -mp_obj_t mp_obj_new_float_via_str(const char* value) { - mp_obj_float_t *o = m_new(mp_obj_float_t, 1); - o->base.type = &mp_type_float; - //Avoid \x0 - int length = 0; - for(int i = 0; i <= sizeof(value); i++){ - if(value[i] == '\x0'){ - length = i; - break; - } - }; - if(length == 0){ - length = (int) sizeof(value); - } - o->value = mp_obj_float_get(mp_parse_num_decimal(value, length, false, false, NULL)); - return MP_OBJ_FROM_PTR(o); -} - mp_float_t mp_obj_float_get(mp_obj_t self_in) { assert(mp_obj_is_float(self_in)); mp_obj_float_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/python/src/py/objint.c b/python/src/py/objint.c index b139b09bc..2fdcf5864 100644 --- a/python/src/py/objint.c +++ b/python/src/py/objint.c @@ -462,17 +462,3 @@ const mp_obj_type_t mp_type_int = { .binary_op = mp_obj_int_binary_op, .locals_dict = (mp_obj_dict_t*)&int_locals_dict, }; - -mp_obj_t mp_obj_new_int_via_str(const char* value, int base) { - int length = 0; - for(int i = 0; i <= sizeof(value); i++){ - if(value[i] == '\x0'){ - length = i; - break; - } - }; - if(length == 0){ - length = (int) sizeof(value); - } - return mp_parse_num_integer(value, length, base, NULL); -}