Skip to content

Commit ee7a880

Browse files
committed
py: Use mp_arg_check_num in more places.
Updated functions now do proper checking that n_kw==0, and are simpler because they don't have to explicitly raise an exception. Down side is that the error messages no longer include the function name, but that's acceptable. Saves order 300 text bytes on x64 and ARM.
1 parent 1d34e32 commit ee7a880

File tree

9 files changed

+23
-34
lines changed

9 files changed

+23
-34
lines changed

py/objbool.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ STATIC void bool_print(void (*print)(void *env, const char *fmt, ...), void *env
4949
}
5050

5151
STATIC mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
52-
// TODO check n_kw == 0
52+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
5353

5454
switch (n_args) {
55-
case 0: return mp_const_false;
56-
case 1: if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
57-
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bool takes at most 1 argument, %d given", n_args));
55+
case 0:
56+
return mp_const_false;
57+
case 1:
58+
default: // must be 0 or 1
59+
if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
5860
}
5961
}
6062

py/objcomplex.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "obj.h"
3535
#include "parsenum.h"
3636
#include "runtime0.h"
37+
#include "runtime.h"
3738

3839
#if MICROPY_ENABLE_FLOAT
3940

@@ -74,7 +75,7 @@ STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *
7475
}
7576

7677
STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
77-
// TODO check n_kw == 0
78+
mp_arg_check_num(n_args, n_kw, 0, 2, false);
7879

7980
switch (n_args) {
8081
case 0:
@@ -94,7 +95,8 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
9495
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
9596
}
9697

97-
case 2: {
98+
case 2:
99+
default: {
98100
mp_float_t real, imag;
99101
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
100102
mp_obj_complex_get(args[0], &real, &imag);
@@ -112,9 +114,6 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
112114
}
113115
return mp_obj_new_complex(real, imag);
114116
}
115-
116-
default:
117-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "complex takes at most 2 arguments, %d given", n_args));
118117
}
119118
}
120119

py/objfloat.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "obj.h"
3838
#include "parsenum.h"
3939
#include "runtime0.h"
40+
#include "runtime.h"
4041

4142
#if MICROPY_ENABLE_FLOAT
4243

@@ -66,13 +67,14 @@ STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *en
6667
}
6768

6869
STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
69-
// TODO check n_kw == 0
70+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
7071

7172
switch (n_args) {
7273
case 0:
7374
return mp_obj_new_float(0);
7475

7576
case 1:
77+
default:
7678
if (MP_OBJ_IS_STR(args[0])) {
7779
// a string, parse it
7880
uint l;
@@ -85,9 +87,6 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
8587
// something else, try to cast it to a float
8688
return mp_obj_new_float(mp_obj_get_float(args[0]));
8789
}
88-
89-
default:
90-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "float takes at most 1 argument, %d given", n_args));
9190
}
9291
}
9392

py/objint.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
// This dispatcher function is expected to be independent of the implementation of long int
4848
STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
49-
// TODO check n_kw == 0
49+
mp_arg_check_num(n_args, n_kw, 0, 2, false);
5050

5151
switch (n_args) {
5252
case 0:
@@ -67,16 +67,13 @@ STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, co
6767
}
6868

6969
case 2:
70-
{
70+
default: {
7171
// should be a string, parse it
7272
// TODO proper error checking of argument types
7373
uint l;
7474
const char *s = mp_obj_str_get_data(args[0], &l);
7575
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
7676
}
77-
78-
default:
79-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
8077
}
8178
}
8279

py/objlist.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,20 @@ STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
6969
}
7070

7171
STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
72-
// TODO check n_kw == 0
72+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
7373

7474
switch (n_args) {
7575
case 0:
7676
// return a new, empty list
7777
return mp_obj_new_list(0, NULL);
7878

7979
case 1:
80-
{
80+
default: {
8181
// make list from iterable
8282
// TODO: optimize list/tuple
8383
mp_obj_t list = mp_obj_new_list(0, NULL);
8484
return list_extend_from_iter(list, args[0]);
8585
}
86-
87-
default:
88-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args));
8986
}
9087
}
9188

py/objproperty.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ typedef struct _mp_obj_property_t {
4242
} mp_obj_property_t;
4343

4444
STATIC mp_obj_t property_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
45-
// TODO check n_kw == 0
45+
mp_arg_check_num(n_args, n_kw, 0, 4, false);
4646

4747
mp_obj_property_t *o = m_new_obj(mp_obj_property_t);
4848
o->base.type = &mp_type_property;
49-
if (n_args >= 5) {
50-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "property takes at most 4 arguments"));
51-
}
5249
if (n_args >= 4) {
5350
// doc ignored
5451
}

py/objtuple.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ void mp_obj_tuple_print(void (*print)(void *env, const char *fmt, ...), void *en
5757
}
5858

5959
STATIC mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
60-
// TODO check n_kw == 0
60+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
6161

6262
switch (n_args) {
6363
case 0:
6464
// return a empty tuple
6565
return mp_const_empty_tuple;
6666

67-
case 1: {
67+
case 1:
68+
default: {
6869
// 1 argument, an iterable from which we make a new tuple
6970
if (MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
7071
return args[0];
@@ -91,9 +92,6 @@ STATIC mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw,
9192

9293
return tuple;
9394
}
94-
95-
default:
96-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "tuple takes at most 1 argument, %d given", n_args));
9795
}
9896
}
9997

py/objtype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ STATIC void type_print(void (*print)(void *env, const char *fmt, ...), void *env
540540
}
541541

542542
STATIC mp_obj_t type_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
543-
// TODO check n_kw == 0
543+
mp_arg_check_num(n_args, n_kw, 1, 3, false);
544544

545545
switch (n_args) {
546546
case 1:

py/objzip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef struct _mp_obj_zip_t {
4040
} mp_obj_zip_t;
4141

4242
STATIC mp_obj_t zip_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
43-
// TODO check n_kw == 0
43+
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
4444

4545
mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
4646
o->base.type = &mp_type_zip;

0 commit comments

Comments
 (0)