diff --git a/py/objexcept.c b/py/objexcept.c index 837e2e85eb..e872db341c 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -30,11 +30,11 @@ #include #include "py/mpstate.h" -#include "py/nlr.h" #include "py/objlist.h" #include "py/objstr.h" #include "py/objtuple.h" #include "py/objtype.h" +#include "py/runtime.h" #include "py/gc.h" // 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, ... } 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) { - mp_obj_type_t *type = type_in; - - if (n_kw != 0) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "%s does not take keyword arguments", mp_obj_get_type_str(type_in))); - } - + mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false); mp_obj_exception_t *o = m_new_obj_var_maybe(mp_obj_exception_t, mp_obj_t, 0); if (o == NULL) { // Couldn't allocate heap memory; use local data instead. o = &MP_STATE_VM(mp_emergency_exception_obj); // We can't store any args. - n_args = 0; o->args = mp_const_empty_tuple; } else { o->args = mp_obj_new_tuple(n_args, args); } - o->base.type = type; + o->base.type = type_in; o->traceback = MP_OBJ_NULL; return o; } diff --git a/py/objfilter.c b/py/objfilter.c index c9ded8d7f4..a97c9f554c 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -24,7 +24,6 @@ * THE SOFTWARE. */ -#include "py/nlr.h" #include "py/runtime.h" typedef struct _mp_obj_filter_t { @@ -34,10 +33,7 @@ typedef struct _mp_obj_filter_t { } mp_obj_filter_t; 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) { - if (n_args != 2 || n_kw != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "filter expected 2 arguments")); - } - assert(n_args == 2); + mp_arg_check_num(n_args, n_kw, 2, 2, false); mp_obj_filter_t *o = m_new_obj(mp_obj_filter_t); o->base.type = type_in; o->fun = args[0]; diff --git a/py/objmap.c b/py/objmap.c index 3225b4dc93..d9df9fdea3 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -27,7 +27,6 @@ #include #include -#include "py/nlr.h" #include "py/runtime.h" typedef struct _mp_obj_map_t { @@ -38,10 +37,7 @@ typedef struct _mp_obj_map_t { } mp_obj_map_t; 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) { - if (n_args < 2 || n_kw != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "map must have at least 2 arguments and no keyword arguments")); - } - assert(n_args >= 2); + mp_arg_check_num(n_args, n_kw, 2, MP_OBJ_FUN_ARGS_MAX, false); mp_obj_map_t *o = m_new_obj_var(mp_obj_map_t, mp_obj_t, n_args - 1); o->base.type = type_in; o->n_iters = n_args - 1; diff --git a/py/objobject.c b/py/objobject.c index 87295b765b..c5675c0719 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -26,9 +26,7 @@ #include -#include "py/nlr.h" -#include "py/obj.h" -#include "py/runtime0.h" +#include "py/runtime.h" 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); @@ -38,10 +36,7 @@ typedef struct _mp_obj_object_t { 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) { (void)args; - if (n_args != 0 || n_kw != 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object takes no arguments")); - } - + mp_arg_check_num(n_args, n_kw, 0, 0, false); mp_obj_object_t *o = m_new_obj(mp_obj_object_t); o->base.type = type_in; return o; diff --git a/py/objtype.c b/py/objtype.c index 8928de9417..46e892a121 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -855,11 +855,9 @@ STATIC void super_print(void (*print)(void *env, const char *fmt, ...), void *en 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) { (void)type_in; - if (n_args != 2 || n_kw != 0) { - // 0 arguments are turned into 2 in the compiler - // 1 argument is not yet implemented - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "super() requires 2 arguments")); - } + // 0 arguments are turned into 2 in the compiler + // 1 argument is not yet implemented + mp_arg_check_num(n_args, n_kw, 2, 2, false); return mp_obj_new_super(args[0], args[1]); }