Skip to content

Commit 50149a5

Browse files
committed
py: Use mp_arg_check_num in some _make_new functions.
Reduces stmhal code size by about 250 bytes.
1 parent ff8dd3f commit 50149a5

5 files changed

Lines changed: 10 additions & 31 deletions

File tree

py/objexcept.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
#include <stdio.h>
3131

3232
#include "py/mpstate.h"
33-
#include "py/nlr.h"
3433
#include "py/objlist.h"
3534
#include "py/objstr.h"
3635
#include "py/objtuple.h"
3736
#include "py/objtype.h"
37+
#include "py/runtime.h"
3838
#include "py/gc.h"
3939

4040
// Instance of MemoryError exception - needed by mp_malloc_fail
@@ -115,23 +115,17 @@ STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...
115115
}
116116

117117
mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
118-
mp_obj_type_t *type = type_in;
119-
120-
if (n_kw != 0) {
121-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s does not take keyword arguments", mp_obj_get_type_str(type_in)));
122-
}
123-
118+
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
124119
mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0);
125120
if (o == NULL) {
126121
// Couldn't allocate heap memory; use local data instead.
127122
o = &MP_STATE_VM(mp_emergency_exception_obj);
128123
// We can't store any args.
129-
n_args = 0;
130124
o->args = mp_const_empty_tuple;
131125
} else {
132126
o->args = mp_obj_new_tuple(n_args, args);
133127
}
134-
o->base.type = type;
128+
o->base.type = type_in;
135129
o->traceback = MP_OBJ_NULL;
136130
return o;
137131
}

py/objfilter.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/nlr.h"
2827
#include "py/runtime.h"
2928

3029
typedef struct _mp_obj_filter_t {
@@ -34,10 +33,7 @@ typedef struct _mp_obj_filter_t {
3433
} mp_obj_filter_t;
3534

3635
STATIC mp_obj_t filter_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
37-
if (n_args != 2 || n_kw != 0) {
38-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "filter expected 2 arguments"));
39-
}
40-
assert(n_args == 2);
36+
mp_arg_check_num(n_args, n_kw, 2, 2, false);
4137
mp_obj_filter_t *o = m_new_obj(mp_obj_filter_t);
4238
o->base.type = type_in;
4339
o->fun = args[0];

py/objmap.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <stdlib.h>
2828
#include <assert.h>
2929

30-
#include "py/nlr.h"
3130
#include "py/runtime.h"
3231

3332
typedef struct _mp_obj_map_t {
@@ -38,10 +37,7 @@ typedef struct _mp_obj_map_t {
3837
} mp_obj_map_t;
3938

4039
STATIC mp_obj_t map_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
41-
if (n_args < 2 || n_kw != 0) {
42-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "map must have at least 2 arguments and no keyword arguments"));
43-
}
44-
assert(n_args >= 2);
40+
mp_arg_check_num(n_args, n_kw, 2, MP_OBJ_FUN_ARGS_MAX, false);
4541
mp_obj_map_t *o = m_new_obj_var(mp_obj_map_t, mp_obj_t, n_args - 1);
4642
o->base.type = type_in;
4743
o->n_iters = n_args - 1;

py/objobject.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626

2727
#include <stdlib.h>
2828

29-
#include "py/nlr.h"
30-
#include "py/obj.h"
31-
#include "py/runtime0.h"
29+
#include "py/runtime.h"
3230

3331
mp_obj_t instance_make_new(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
3432

@@ -38,10 +36,7 @@ typedef struct _mp_obj_object_t {
3836

3937
STATIC mp_obj_t object_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
4038
(void)args;
41-
if (n_args != 0 || n_kw != 0) {
42-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object takes no arguments"));
43-
}
44-
39+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
4540
mp_obj_object_t *o = m_new_obj(mp_obj_object_t);
4641
o->base.type = type_in;
4742
return o;

py/objtype.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,9 @@ STATIC void super_print(void (*print)(void *env, const char *fmt, ...), void *en
855855

856856
STATIC mp_obj_t super_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
857857
(void)type_in;
858-
if (n_args != 2 || n_kw != 0) {
859-
// 0 arguments are turned into 2 in the compiler
860-
// 1 argument is not yet implemented
861-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "super() requires 2 arguments"));
862-
}
858+
// 0 arguments are turned into 2 in the compiler
859+
// 1 argument is not yet implemented
860+
mp_arg_check_num(n_args, n_kw, 2, 2, false);
863861
return mp_obj_new_super(args[0], args[1]);
864862
}
865863

0 commit comments

Comments
 (0)