Skip to content

Commit a7b10d4

Browse files
authored
Merge pull request #6522 from jepler/must-be-int
Improve argument checking & reduce strings to translate
2 parents c291a02 + 4f75d09 commit a7b10d4

File tree

11 files changed

+35
-102
lines changed

11 files changed

+35
-102
lines changed

locale/circuitpython.pot

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,8 +2526,7 @@ msgstr ""
25262526
msgid "can't cancel self"
25272527
msgstr ""
25282528

2529-
#: py/obj.c py/objint.c shared-bindings/i2ctarget/I2CTarget.c
2530-
#: shared-module/adafruit_pixelbuf/PixelBuf.c
2529+
#: py/obj.c py/objint.c shared-module/adafruit_pixelbuf/PixelBuf.c
25312530
msgid "can't convert %q to %q"
25322531
msgstr ""
25332532

@@ -2714,10 +2713,6 @@ msgstr ""
27142713
msgid "color must be between 0x000000 and 0xffffff"
27152714
msgstr ""
27162715

2717-
#: shared-bindings/displayio/ColorConverter.c
2718-
msgid "color should be an int"
2719-
msgstr ""
2720-
27212716
#: py/emitnative.c
27222717
msgid "comparison of int and uint"
27232718
msgstr ""
@@ -2869,10 +2864,6 @@ msgstr ""
28692864
msgid "end of format while looking for conversion specifier"
28702865
msgstr ""
28712866

2872-
#: shared-bindings/displayio/Shape.c
2873-
msgid "end_x should be an int"
2874-
msgstr ""
2875-
28762867
#: shared-bindings/alarm/time/TimeAlarm.c
28772868
msgid "epoch_time not supported on this board"
28782869
msgstr ""
@@ -3739,10 +3730,6 @@ msgstr ""
37393730
msgid "palette must be 32 bytes long"
37403731
msgstr ""
37413732

3742-
#: shared-bindings/displayio/Palette.c
3743-
msgid "palette_index should be an int"
3744-
msgstr ""
3745-
37463733
#: py/emitinlinextensa.c
37473734
msgid "parameters must be registers in sequence a2 to a5"
37483735
msgstr ""
@@ -3980,10 +3967,6 @@ msgstr ""
39803967
msgid "start/end indices"
39813968
msgstr ""
39823969

3983-
#: shared-bindings/displayio/Shape.c
3984-
msgid "start_x should be an int"
3985-
msgstr ""
3986-
39873970
#: shared-bindings/random/__init__.c
39883971
msgid "step must be non-zero"
39893972
msgstr ""
@@ -4198,7 +4181,6 @@ msgid "unreadable attribute"
41984181
msgstr ""
41994182

42004183
#: shared-bindings/displayio/TileGrid.c shared-bindings/vectorio/VectorShape.c
4201-
#: shared-module/vectorio/Polygon.c shared-module/vectorio/VectorShape.c
42024184
msgid "unsupported %q type"
42034185
msgstr ""
42044186

@@ -4328,10 +4310,6 @@ msgstr ""
43284310
msgid "xTaskCreate failed"
43294311
msgstr ""
43304312

4331-
#: shared-bindings/displayio/Shape.c
4332-
msgid "y should be an int"
4333-
msgstr ""
4334-
43354313
#: shared-module/displayio/Shape.c
43364314
msgid "y value out of bounds"
43374315
msgstr ""

py/argcheck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ mp_float_t mp_arg_validate_obj_float_non_negative(mp_obj_t float_in, mp_float_t
189189
? default_for_null
190190
: mp_obj_get_float(float_in);
191191
if (f <= (mp_float_t)0.0) {
192-
mp_raise_ValueError_varg(translate("%q must be >= 0"), arg_name);
192+
mp_raise_ValueError_varg(translate("%q must be >= %d"), arg_name, 0);
193193
}
194194
return f;
195195
}

shared-bindings/audiobusio/PDMIn.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4,
188188
STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) {
189189
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj);
190190
check_for_deinit(self);
191-
if (!mp_obj_is_small_int(destination_length) || MP_OBJ_SMALL_INT_VALUE(destination_length) < 0) {
192-
mp_raise_TypeError(translate("destination_length must be an int >= 0"));
193-
}
194-
uint32_t length = MP_OBJ_SMALL_INT_VALUE(destination_length);
191+
uint32_t length = mp_arg_validate_type_int(destination_length, MP_QSTR_length);
192+
mp_arg_validate_length_min(length, 0, MP_QSTR_length);
195193

196194
mp_buffer_info_t bufinfo;
197195
if (mp_obj_is_type(destination, &mp_type_fileio)) {

shared-bindings/displayio/ColorConverter.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, siz
7272
STATIC mp_obj_t displayio_colorconverter_obj_convert(mp_obj_t self_in, mp_obj_t color_obj) {
7373
displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);
7474

75-
mp_int_t color;
76-
if (!mp_obj_get_int_maybe(color_obj, &color)) {
77-
mp_raise_ValueError(translate("color should be an int"));
78-
}
75+
mp_int_t color = mp_arg_validate_type_int(color_obj, MP_QSTR_color);
7976
_displayio_colorspace_t colorspace;
8077
colorspace.depth = 16;
8178
uint32_t output_color;

shared-bindings/displayio/Palette.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
156156
STATIC mp_obj_t displayio_palette_obj_make_transparent(mp_obj_t self_in, mp_obj_t palette_index_obj) {
157157
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);
158158

159-
mp_int_t palette_index;
160-
if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) {
161-
mp_raise_ValueError(translate("palette_index should be an int"));
162-
}
163-
palette_index = mp_arg_validate_int_range(palette_index, 0, common_hal_displayio_palette_get_len(self) - 1, MP_QSTR_palette_index);
159+
mp_int_t palette_index = mp_arg_validate_type_int(palette_index_obj, MP_QSTR_palette_index);
160+
mp_arg_validate_int_range(palette_index, 0, common_hal_displayio_palette_get_len(self) - 1, MP_QSTR_palette_index);
164161

165162
common_hal_displayio_palette_make_transparent(self, palette_index);
166163
return mp_const_none;
@@ -173,10 +170,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_make_transparent_obj, displayio_pale
173170
STATIC mp_obj_t displayio_palette_obj_make_opaque(mp_obj_t self_in, mp_obj_t palette_index_obj) {
174171
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);
175172

176-
mp_int_t palette_index;
177-
if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) {
178-
mp_raise_ValueError(translate("palette_index should be an int"));
179-
}
173+
mp_int_t palette_index = mp_arg_validate_type_int(palette_index_obj, MP_QSTR_palette_index);
180174
palette_index = mp_arg_validate_int_range(palette_index, 0, common_hal_displayio_palette_get_len(self) - 1, MP_QSTR_palette_index);
181175

182176
common_hal_displayio_palette_make_opaque(self, palette_index);
@@ -191,10 +185,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_palette_make_opaque_obj, displayio_palette_o
191185
STATIC mp_obj_t displayio_palette_obj_is_transparent(mp_obj_t self_in, mp_obj_t palette_index_obj) {
192186
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);
193187

194-
mp_int_t palette_index;
195-
if (!mp_obj_get_int_maybe(palette_index_obj, &palette_index)) {
196-
mp_raise_ValueError(translate("palette_index should be an int"));
197-
}
188+
mp_int_t palette_index = mp_arg_validate_type_int(palette_index_obj, MP_QSTR_palette_index);
198189
palette_index = mp_arg_validate_int_range(palette_index, 0, common_hal_displayio_palette_get_len(self) - 1, MP_QSTR_palette_index);
199190

200191
return mp_obj_new_bool(common_hal_displayio_palette_is_transparent(self, palette_index));

shared-bindings/displayio/Shape.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,9 @@ STATIC mp_obj_t displayio_shape_make_new(const mp_obj_type_t *type, size_t n_arg
8080
STATIC mp_obj_t displayio_shape_obj_set_boundary(size_t n_args, const mp_obj_t *args) {
8181
(void)n_args;
8282
displayio_shape_t *self = MP_OBJ_TO_PTR(args[0]);
83-
mp_int_t y;
84-
if (!mp_obj_get_int_maybe(args[1], &y)) {
85-
mp_raise_ValueError(translate("y should be an int"));
86-
}
87-
mp_int_t start_x;
88-
if (!mp_obj_get_int_maybe(args[2], &start_x)) {
89-
mp_raise_ValueError(translate("start_x should be an int"));
90-
}
91-
mp_int_t end_x;
92-
if (!mp_obj_get_int_maybe(args[3], &end_x)) {
93-
mp_raise_ValueError(translate("end_x should be an int"));
94-
}
83+
mp_int_t y = mp_arg_validate_type_int(args[1], MP_QSTR_y);
84+
mp_int_t start_x = mp_arg_validate_type_int(args[1], MP_QSTR_start_x);
85+
mp_int_t end_x = mp_arg_validate_type_int(args[1], MP_QSTR_end_x);
9586
common_hal_displayio_shape_set_boundary(self, y, start_x, end_x);
9687

9788
return mp_const_none;

shared-bindings/i2ctarget/I2CTarget.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ STATIC mp_obj_t i2ctarget_i2c_target_make_new(const mp_obj_type_t *type, size_t
8585
uint8_t *addresses = NULL;
8686
unsigned int i = 0;
8787
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
88-
mp_int_t value;
89-
if (!mp_obj_get_int_maybe(item, &value)) {
90-
mp_raise_TypeError_varg(translate("can't convert %q to %q"), MP_QSTR_address, MP_QSTR_int);
91-
}
92-
if (value < 0x00 || value > 0x7f) {
93-
mp_raise_ValueError(translate("address out of bounds"));
94-
}
88+
mp_uint_t value = mp_arg_validate_int_range(mp_obj_get_int(item), 0x00, 0x7f, MP_QSTR_address);
9589
addresses = m_renew(uint8_t, addresses, i, i + 1);
9690
addresses[i++] = value;
9791
}

shared-bindings/vectorio/Circle.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg
3434
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
3535

3636
mp_int_t radius = args[ARG_radius].u_int;
37-
if (radius < 1) {
38-
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_radius);
39-
}
37+
mp_arg_validate_int_min(radius, 1, MP_QSTR_radius);
4038

4139
vectorio_circle_t *self = m_new_obj(vectorio_circle_t);
4240
self->base.type = &vectorio_circle_type;

shared-bindings/vectorio/Rectangle.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_
3434
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
3535

3636
mp_int_t width = args[ARG_width].u_int;
37-
if (width < 1) {
38-
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_width);
39-
}
37+
mp_arg_validate_int_min(width, 1, MP_QSTR_width);
4038
mp_int_t height = args[ARG_height].u_int;
41-
if (height < 1) {
42-
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_height);
43-
}
39+
mp_arg_validate_int_min(height, 1, MP_QSTR_height);
4440

4541
vectorio_rectangle_t *self = m_new_obj(vectorio_rectangle_t);
4642
self->base.type = &vectorio_rectangle_type;

shared-module/vectorio/Polygon.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515

1616
// Converts a list of points tuples to a flat list of ints for speedier internal use.
17-
// Also validates the points.
17+
// Also validates the points. If this fails due to invalid types or values, the
18+
// number of points is 0 and the points_list is NULL.
1819
static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple_list) {
1920
size_t len = 0;
2021
mp_obj_t *items;
@@ -25,15 +26,12 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
2526
mp_raise_TypeError(translate("Polygon needs at least 3 points"));
2627
}
2728

28-
if (self->len < 2 * len) {
29-
if (self->points_list != NULL) {
30-
VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list));
31-
gc_free(self->points_list);
32-
}
33-
self->points_list = gc_alloc(2 * len * sizeof(uint16_t), false, false);
34-
VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(uint16_t));
35-
}
36-
self->len = 2 * len;
29+
int16_t *points_list = gc_realloc(self->points_list, 2 * len * sizeof(uint16_t), true);
30+
VECTORIO_POLYGON_DEBUG("realloc(%p, %d) -> %p", self->points_list, 2 * len * sizeof(uint16_t), points_list);
31+
32+
// In case the validation calls below fail, set these values temporarily
33+
self->points_list = NULL;
34+
self->len = 0;
3735

3836
for (uint16_t i = 0; i < len; ++i) {
3937
size_t tuple_len = 0;
@@ -42,20 +40,16 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
4240

4341
mp_arg_validate_length(tuple_len, 2, MP_QSTR_point);
4442

45-
mp_int_t x;
46-
mp_int_t y;
47-
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
48-
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)
49-
|| x < SHRT_MIN || x > SHRT_MAX || y < SHRT_MIN || y > SHRT_MAX
50-
) {
51-
gc_free(self->points_list);
52-
self->points_list = NULL;
53-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
54-
self->len = 0;
55-
}
56-
self->points_list[2 * i ] = (int16_t)x;
57-
self->points_list[2 * i + 1] = (int16_t)y;
43+
mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x);
44+
mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x);
45+
mp_int_t y = mp_arg_validate_type_int(tuple_items[1], MP_QSTR_y);
46+
mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y);
47+
points_list[2 * i ] = (int16_t)x;
48+
points_list[2 * i + 1] = (int16_t)y;
5849
}
50+
51+
self->points_list = points_list;
52+
self->len = 2 * len;
5953
}
6054

6155

shared-module/vectorio/VectorShape.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,8 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
277277
mp_obj_tuple_get(xy, &tuple_len, &tuple_items);
278278
mp_arg_validate_length(tuple_len, 2, MP_QSTR_location);
279279

280-
mp_int_t x;
281-
mp_int_t y;
282-
if (!mp_obj_get_int_maybe(tuple_items[ 0 ], &x)
283-
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
284-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
285-
}
280+
mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x);
281+
mp_int_t y = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_y);
286282
bool dirty = false;
287283
if (self->x != x) {
288284
check_bounds_and_set_x(self, x);

0 commit comments

Comments
 (0)