Skip to content

Commit

Permalink
Merge pull request #111 from pimoroni/patch-texthandling
Browse files Browse the repository at this point in the history
Fix for MicroPython hard lock when passing in non-string object to text function
  • Loading branch information
Gadgetoid authored Mar 30, 2021
2 parents d7e3a98 + 787ba58 commit c3f609e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 39 deletions.
38 changes: 26 additions & 12 deletions micropython/modules/breakout_roundlcd/breakout_roundlcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,22 +411,36 @@ mp_obj_t BreakoutRoundLCD_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t

breakout_roundlcd_BreakoutRoundLCD_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_roundlcd_BreakoutRoundLCD_obj_t);

mp_check_self(mp_obj_is_str_or_bytes(args[ARG_text].u_obj));
GET_STR_DATA_LEN(args[ARG_text].u_obj, str, str_len);
mp_obj_t text_obj = args[ARG_text].u_obj;
if(mp_obj_is_str_or_bytes(text_obj)) {
GET_STR_DATA_LEN(text_obj, str, str_len);

std::string t((const char*)str);
std::string t((const char*)str);

int x = args[ARG_x].u_int;
int y = args[ARG_y].u_int;
int wrap = args[ARG_wrap].u_int;
int x = args[ARG_x].u_int;
int y = args[ARG_y].u_int;
int wrap = args[ARG_wrap].u_int;

Point p(x, y);
if(n_args == 5) {
int scale = args[ARG_scale].u_int;
self->breakout->text(t, p, wrap, scale);
Point p(x, y);
if(n_args == 5) {
int scale = args[ARG_scale].u_int;
self->breakout->text(t, p, wrap, scale);
}
else
self->breakout->text(t, p, wrap);
}
else
self->breakout->text(t, p, wrap);
else if(mp_obj_is_float(text_obj)) {
mp_raise_TypeError("can't convert 'float' object to str implicitly");
}
else if(mp_obj_is_int(text_obj)) {
mp_raise_TypeError("can't convert 'int' object to str implicitly");
}
else if(mp_obj_is_bool(text_obj)) {
mp_raise_TypeError("can't convert 'bool' object to str implicitly");
}
else {
mp_raise_TypeError("can't convert object to str implicitly");
}

return mp_const_none;
}
Expand Down
43 changes: 28 additions & 15 deletions micropython/modules/pico_display/pico_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,35 @@ mp_obj_t picodisplay_character(mp_uint_t n_args, const mp_obj_t *args) {

mp_obj_t picodisplay_text(mp_uint_t n_args, const mp_obj_t *args) {
if(display != nullptr) {
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
GET_STR_DATA_LEN(args[0], str, str_len);

std::string t((const char*)str);

int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]);

Point p(x, y);
if(n_args == 5) {
int scale = mp_obj_get_int(args[4]);
display->text(t, p, wrap, scale);
if(mp_obj_is_str_or_bytes(args[0])) {
GET_STR_DATA_LEN(args[0], str, str_len);

std::string t((const char*)str);

int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]);

Point p(x, y);
if(n_args == 5) {
int scale = mp_obj_get_int(args[4]);
display->text(t, p, wrap, scale);
}
else
display->text(t, p, wrap);
}
else if(mp_obj_is_float(args[0])) {
mp_raise_TypeError("can't convert 'float' object to str implicitly");
}
else if(mp_obj_is_int(args[0])) {
mp_raise_TypeError("can't convert 'int' object to str implicitly");
}
else if(mp_obj_is_bool(args[0])) {
mp_raise_TypeError("can't convert 'bool' object to str implicitly");
}
else {
mp_raise_TypeError("can't convert object to str implicitly");
}
else
display->text(t, p, wrap);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
Expand Down
37 changes: 25 additions & 12 deletions micropython/modules/pico_explorer/pico_explorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,35 @@ mp_obj_t picoexplorer_character(mp_uint_t n_args, const mp_obj_t *args) {

mp_obj_t picoexplorer_text(mp_uint_t n_args, const mp_obj_t *args) {
if(explorer != nullptr) {
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
GET_STR_DATA_LEN(args[0], str, str_len);
if(mp_obj_is_str_or_bytes(args[0])) {
GET_STR_DATA_LEN(args[0], str, str_len);

std::string t((const char*)str);
std::string t((const char*)str);

int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]);
int x = mp_obj_get_int(args[1]);
int y = mp_obj_get_int(args[2]);
int wrap = mp_obj_get_int(args[3]);

Point p(x, y);
if(n_args == 5) {
int scale = mp_obj_get_int(args[4]);
explorer->text(t, p, wrap, scale);
Point p(x, y);
if(n_args == 5) {
int scale = mp_obj_get_int(args[4]);
explorer->text(t, p, wrap, scale);
}
else
explorer->text(t, p, wrap);
}
else if(mp_obj_is_float(args[0])) {
mp_raise_TypeError("can't convert 'float' object to str implicitly");
}
else if(mp_obj_is_int(args[0])) {
mp_raise_TypeError("can't convert 'int' object to str implicitly");
}
else if(mp_obj_is_bool(args[0])) {
mp_raise_TypeError("can't convert 'bool' object to str implicitly");
}
else {
mp_raise_TypeError("can't convert object to str implicitly");
}
else
explorer->text(t, p, wrap);
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
Expand Down

0 comments on commit c3f609e

Please sign in to comment.