py: Use mp_arg_check_num in some _make_new functions.
Reduces stmhal code size by about 250 bytes.
This commit is contained in:
parent
ff8dd3f486
commit
50149a5730
@ -30,11 +30,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
@ -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];
|
||||
|
@ -27,7 +27,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#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;
|
||||
|
@ -26,9 +26,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
|
@ -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]);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user