Skip to content

Commit c4a8004

Browse files
committed
py: Get rid of assert() in method argument checking functions.
Checks for number of args removes where guaranteed by function descriptor, self checking is replaced with mp_check_self(). In few cases, exception is raised instead of assert.
1 parent 83e0eba commit c4a8004

File tree

11 files changed

+33
-45
lines changed

11 files changed

+33
-45
lines changed

py/modbuiltins.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
376376
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_ord_obj, mp_builtin_ord);
377377

378378
STATIC mp_obj_t mp_builtin_pow(size_t n_args, const mp_obj_t *args) {
379-
assert(2 <= n_args && n_args <= 3);
380379
switch (n_args) {
381380
case 2: return mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]);
382381
default: return mp_binary_op(MP_BINARY_OP_MODULO, mp_binary_op(MP_BINARY_OP_POWER, args[0], args[1]), args[2]); // TODO optimise...
@@ -492,7 +491,6 @@ STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) {
492491
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round);
493492

494493
STATIC mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) {
495-
assert(1 <= n_args && n_args <= 2);
496494
mp_obj_t value;
497495
switch (n_args) {
498496
case 1: value = MP_OBJ_NEW_SMALL_INT(0); break;
@@ -508,7 +506,6 @@ STATIC mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) {
508506
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj, 1, 2, mp_builtin_sum);
509507

510508
STATIC mp_obj_t mp_builtin_sorted(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
511-
assert(n_args >= 1);
512509
if (n_args > 1) {
513510
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
514511
"must use keyword argument for key function"));

py/objfilter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ STATIC mp_obj_t filter_make_new(const mp_obj_type_t *type, size_t n_args, size_t
4444
}
4545

4646
STATIC mp_obj_t filter_iternext(mp_obj_t self_in) {
47-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_filter));
47+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_filter));
4848
mp_obj_filter_t *self = MP_OBJ_TO_PTR(self_in);
4949
mp_obj_t next;
5050
while ((next = mp_iternext(self->iter)) != MP_OBJ_STOP_ITERATION) {

py/objgenerator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
9696
}
9797

9898
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
99-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
99+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
100100
mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
101101
if (self->code_state.ip == 0) {
102102
// Trying to resume already stopped generator

py/objlist.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
8989

9090
// Don't pass MP_BINARY_OP_NOT_EQUAL here
9191
STATIC bool list_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
92-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
92+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
9393
if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
9494
return false;
9595
}
@@ -156,7 +156,7 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
156156
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
157157
mp_bound_slice_t slice;
158158
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
159-
assert(0);
159+
mp_not_implemented("");
160160
}
161161

162162
mp_int_t len_adj = slice.start - slice.stop;
@@ -192,11 +192,11 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
192192
#if MICROPY_PY_BUILTINS_SLICE
193193
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
194194
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
195-
assert(MP_OBJ_IS_TYPE(value, &mp_type_list));
195+
mp_check_self(MP_OBJ_IS_TYPE(value, &mp_type_list));
196196
mp_obj_list_t *slice = MP_OBJ_TO_PTR(value);
197197
mp_bound_slice_t slice_out;
198198
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
199-
assert(0);
199+
mp_not_implemented("");
200200
}
201201
mp_int_t len_adj = slice->len - (slice_out.stop - slice_out.start);
202202
//printf("Len adj: %d\n", len_adj);
@@ -230,7 +230,7 @@ STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
230230
}
231231

232232
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
233-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
233+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
234234
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
235235
if (self->len >= self->alloc) {
236236
self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
@@ -242,7 +242,7 @@ mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
242242
}
243243

244244
STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
245-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
245+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
246246
if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
247247
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
248248
mp_obj_list_t *arg = MP_OBJ_TO_PTR(arg_in);
@@ -263,8 +263,7 @@ STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
263263
}
264264

265265
STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
266-
assert(1 <= n_args && n_args <= 2);
267-
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
266+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
268267
mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
269268
if (self->len == 0) {
270269
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
@@ -324,7 +323,7 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
324323
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
325324
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
326325

327-
assert(MP_OBJ_IS_TYPE(pos_args[0], &mp_type_list));
326+
mp_check_self(MP_OBJ_IS_TYPE(pos_args[0], &mp_type_list));
328327
mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]);
329328

330329
if (self->len > 1) {
@@ -337,7 +336,7 @@ mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
337336
}
338337

339338
STATIC mp_obj_t list_clear(mp_obj_t self_in) {
340-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
339+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
341340
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
342341
self->len = 0;
343342
self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
@@ -347,26 +346,25 @@ STATIC mp_obj_t list_clear(mp_obj_t self_in) {
347346
}
348347

349348
STATIC mp_obj_t list_copy(mp_obj_t self_in) {
350-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
349+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
351350
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
352351
return mp_obj_new_list(self->len, self->items);
353352
}
354353

355354
STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
356-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
355+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
357356
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
358357
return mp_seq_count_obj(self->items, self->len, value);
359358
}
360359

361360
STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
362-
assert(2 <= n_args && n_args <= 4);
363-
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
361+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
364362
mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
365363
return mp_seq_index_obj(self->items, self->len, n_args, args);
366364
}
367365

368366
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
369-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
367+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
370368
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
371369
// insert has its own strange index logic
372370
mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
@@ -391,7 +389,7 @@ STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
391389
}
392390

393391
mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
394-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
392+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
395393
mp_obj_t args[] = {self_in, value};
396394
args[1] = list_index(2, args);
397395
list_pop(2, args);
@@ -400,7 +398,7 @@ mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
400398
}
401399

402400
STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
403-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
401+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
404402
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
405403

406404
mp_int_t len = self->len;

py/objmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ STATIC mp_obj_t map_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
4949
}
5050

5151
STATIC mp_obj_t map_iternext(mp_obj_t self_in) {
52-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_map));
52+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_map));
5353
mp_obj_map_t *self = MP_OBJ_TO_PTR(self_in);
5454
mp_obj_t *nextses = m_new(mp_obj_t, self->n_iters);
5555

py/objproperty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const mp_obj_type_t mp_type_property = {
100100
};
101101

102102
const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) {
103-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_property));
103+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_property));
104104
mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
105105
return self->proxy;
106106
}

py/objreversed.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ STATIC mp_obj_t reversed_make_new(const mp_obj_type_t *type, size_t n_args, size
5757
}
5858

5959
STATIC mp_obj_t reversed_iternext(mp_obj_t self_in) {
60-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_reversed));
60+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_reversed));
6161
mp_obj_reversed_t *self = MP_OBJ_TO_PTR(self_in);
6262

6363
// "raise" stop iteration if we are at the end (the start) of the sequence

py/objset.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
224224
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
225225

226226
STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
227-
assert(n_args > 0);
228-
229227
mp_obj_t self;
230228
if (update) {
231229
check_set(args[0]);
@@ -443,8 +441,6 @@ STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
443441
}
444442

445443
STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
446-
assert(n_args > 0);
447-
448444
for (mp_uint_t i = 1; i < n_args; i++) {
449445
set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
450446
}
@@ -587,7 +583,7 @@ mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) {
587583
}
588584

589585
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
590-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
586+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
591587
mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
592588
mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
593589
}

py/objstr.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
412412
}
413413

414414
STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
415-
assert(MP_OBJ_IS_STR_OR_BYTES(self_in));
415+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(self_in));
416416
const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
417417

418418
// get separation string
@@ -648,8 +648,7 @@ STATIC mp_obj_t str_rsplit(size_t n_args, const mp_obj_t *args) {
648648

649649
STATIC mp_obj_t str_finder(mp_uint_t n_args, const mp_obj_t *args, mp_int_t direction, bool is_index) {
650650
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
651-
assert(2 <= n_args && n_args <= 4);
652-
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
651+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
653652

654653
// check argument type
655654
if (mp_obj_get_type(args[1]) != self_type) {
@@ -734,8 +733,7 @@ STATIC mp_obj_t str_endswith(size_t n_args, const mp_obj_t *args) {
734733
enum { LSTRIP, RSTRIP, STRIP };
735734

736735
STATIC mp_obj_t str_uni_strip(int type, mp_uint_t n_args, const mp_obj_t *args) {
737-
assert(1 <= n_args && n_args <= 2);
738-
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
736+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
739737
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
740738

741739
const byte *chars_to_del;
@@ -1327,7 +1325,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
13271325
}
13281326

13291327
mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
1330-
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
1328+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
13311329

13321330
GET_STR_DATA_LEN(args[0], str, len);
13331331
int arg_i = 0;
@@ -1336,7 +1334,7 @@ mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
13361334
}
13371335

13381336
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_obj_t *args, mp_obj_t dict) {
1339-
assert(MP_OBJ_IS_STR_OR_BYTES(pattern));
1337+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(pattern));
13401338

13411339
GET_STR_DATA_LEN(pattern, str, len);
13421340
const byte *start_str = str;
@@ -1530,7 +1528,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
15301528
// The implementation is optimized, returning the original string if there's
15311529
// nothing to replace.
15321530
STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
1533-
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
1531+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
15341532

15351533
mp_int_t max_rep = -1;
15361534
if (n_args == 4) {
@@ -1636,8 +1634,7 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
16361634

16371635
STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
16381636
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
1639-
assert(2 <= n_args && n_args <= 4);
1640-
assert(MP_OBJ_IS_STR_OR_BYTES(args[0]));
1637+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
16411638

16421639
// check argument type
16431640
if (mp_obj_get_type(args[1]) != self_type) {
@@ -1677,7 +1674,7 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
16771674

16781675
#if MICROPY_PY_BUILTINS_STR_PARTITION
16791676
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, mp_int_t direction) {
1680-
assert(MP_OBJ_IS_STR_OR_BYTES(self_in));
1677+
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(self_in));
16811678
mp_obj_type_t *self_type = mp_obj_get_type(self_in);
16821679
if (self_type != mp_obj_get_type(arg)) {
16831680
bad_implicit_conversion(arg);

py/objtuple.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg
106106
STATIC bool tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
107107
mp_obj_type_t *self_type = mp_obj_get_type(self_in);
108108
if (self_type->getiter != mp_obj_tuple_getiter) {
109-
assert(0);
109+
mp_raise_TypeError("");
110110
}
111111
mp_obj_type_t *another_type = mp_obj_get_type(another_in);
112112
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
@@ -202,14 +202,14 @@ mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in) {
202202
}
203203

204204
STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
205-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
205+
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
206206
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
207207
return mp_seq_count_obj(self->items, self->len, value);
208208
}
209209
STATIC MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
210210

211211
STATIC mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args) {
212-
assert(MP_OBJ_IS_TYPE(args[0], &mp_type_tuple));
212+
mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_tuple));
213213
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(args[0]);
214214
return mp_seq_index_obj(self->items, self->len, n_args, args);
215215
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy