Commit a3f94e00 authored by Damien George's avatar Damien George
Browse files

py: Add arg checking helper functions.

These are to assist in writing native C functions that take positional
and keyword arguments.  mp_arg_check_num is for just checking the
number of arguments is correct.  mp_arg_parse_all is for parsing
positional and keyword arguments with default values.
parent 27dd4710
#include <stdlib.h>
#include <assert.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw) {
// TODO maybe take the function name as an argument so we can print nicer error messages
if (n_kw && !takes_kw) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
}
if (n_args_min == n_args_max) {
if (n_args != n_args_min) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function takes %d positional arguments but %d were given",
n_args_min, n_args));
}
} else {
if (n_args < n_args_min) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing %d required positional arguments",
n_args_min - n_args));
} else if (n_args > n_args_max) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function expected at most %d arguments, got %d",
n_args_max, n_args));
}
}
}
void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_parse_t *allowed, mp_arg_parse_val_t *out_vals) {
uint pos_found = 0, kws_found = 0;
for (uint i = 0; i < n_allowed; i++) {
mp_obj_t given_arg;
if (i < n_pos) {
if (allowed[i].flags & MP_ARG_PARSE_KW_ONLY) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' argument must be given by a keyword", qstr_str(allowed[i].qstr)));
}
pos_found++;
given_arg = pos[i];
} else {
mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qstr), MP_MAP_LOOKUP);
if (kw == NULL) {
if (allowed[i].flags & MP_ARG_PARSE_REQUIRED) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' argument required", qstr_str(allowed[i].qstr)));
}
out_vals[i] = allowed[i].defval;
continue;
} else {
kws_found++;
given_arg = kw->value;
}
}
if (allowed[i].flags == MP_ARG_PARSE_BOOL) {
out_vals[i].u_bool = mp_obj_is_true(given_arg);
} else if (allowed[i].flags == MP_ARG_PARSE_INT) {
out_vals[i].u_int = mp_obj_get_int(given_arg);
} else if (allowed[i].flags == MP_ARG_PARSE_OBJ) {
out_vals[i].u_obj = given_arg;
} else {
assert(0);
}
}
if (pos_found < n_pos) {
// TODO better error message
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra positional arguments given"));
}
if (kws_found < kws->used) {
// TODO better error message
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "extra keyword arguments given"));
}
}
...@@ -75,7 +75,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) { ...@@ -75,7 +75,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
} }
STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_check_nargs(n_args, 1, 2, n_kw, false); mp_arg_check_num(n_args, n_kw, 1, 2, false);
// get typecode // get typecode
uint l; uint l;
...@@ -91,7 +91,7 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m ...@@ -91,7 +91,7 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
} }
STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_check_nargs(n_args, 0, 1, n_kw, false); mp_arg_check_num(n_args, n_kw, 0, 1, false);
if (n_args == 0) { if (n_args == 0) {
// no args: construct an empty bytearray // no args: construct an empty bytearray
......
...@@ -25,36 +25,6 @@ ...@@ -25,36 +25,6 @@
// mp_obj_fun_native_t defined in obj.h // mp_obj_fun_native_t defined in obj.h
STATIC void check_nargs(mp_obj_fun_native_t *self, int n_args, int n_kw) {
mp_check_nargs(n_args, self->n_args_min, self->n_args_max, n_kw, self->is_kw);
}
void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw) {
// TODO maybe take the function name as an argument so we can print nicer error messages
if (n_kw && !is_kw) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
}
if (n_args_min == n_args_max) {
if (n_args != n_args_min) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function takes %d positional arguments but %d were given",
n_args_min, n_args));
}
} else {
if (n_args < n_args_min) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function missing %d required positional arguments",
n_args_min - n_args));
} else if (n_args > n_args_max) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"function expected at most %d arguments, got %d",
n_args_max, n_args));
}
}
}
STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
switch (op) { switch (op) {
case MP_BINARY_OP_EQUAL: case MP_BINARY_OP_EQUAL:
...@@ -70,7 +40,7 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const ...@@ -70,7 +40,7 @@ STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const
mp_obj_fun_native_t *self = self_in; mp_obj_fun_native_t *self = self_in;
// check number of arguments // check number of arguments
check_nargs(self, n_args, n_kw); mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
if (self->is_kw) { if (self->is_kw) {
// function allows keywords // function allows keywords
......
...@@ -57,7 +57,7 @@ typedef struct _mp_obj_range_t { ...@@ -57,7 +57,7 @@ typedef struct _mp_obj_range_t {
} mp_obj_range_t; } mp_obj_range_t;
STATIC mp_obj_t range_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { STATIC mp_obj_t range_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_check_nargs(n_args, 1, 3, n_kw, false); mp_arg_check_num(n_args, n_kw, 1, 3, false);
mp_obj_range_t *o = m_new_obj(mp_obj_range_t); mp_obj_range_t *o = m_new_obj(mp_obj_range_t);
o->base.type = &mp_type_range; o->base.type = &mp_type_range;
......
...@@ -40,6 +40,7 @@ PY_O_BASENAME = \ ...@@ -40,6 +40,7 @@ PY_O_BASENAME = \
parsenum.o \ parsenum.o \
emitglue.o \ emitglue.o \
runtime.o \ runtime.o \
argcheck.o \
map.o \ map.o \
obj.o \ obj.o \
objarray.o \ objarray.o \
......
...@@ -4,10 +4,32 @@ typedef enum { ...@@ -4,10 +4,32 @@ typedef enum {
MP_VM_RETURN_EXCEPTION, MP_VM_RETURN_EXCEPTION,
} mp_vm_return_kind_t; } mp_vm_return_kind_t;
typedef enum {
MP_ARG_PARSE_BOOL = 0x001,
MP_ARG_PARSE_INT = 0x002,
MP_ARG_PARSE_OBJ = 0x003,
MP_ARG_PARSE_KIND_MASK = 0x0ff,
MP_ARG_PARSE_REQUIRED = 0x100,
MP_ARG_PARSE_KW_ONLY = 0x200,
} mp_arg_parse_flag_t;
typedef union _mp_arg_parse_val_t {
bool u_bool;
machine_int_t u_int;
mp_obj_t u_obj;
} mp_arg_parse_val_t;
typedef struct _mp_arg_parse_t {
qstr qstr;
machine_uint_t flags;
mp_arg_parse_val_t defval;
} mp_arg_parse_t;
void mp_init(void); void mp_init(void);
void mp_deinit(void); void mp_deinit(void);
void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw); void mp_arg_check_num(uint n_args, uint n_kw, uint n_args_min, uint n_args_max, bool takes_kw);
void mp_arg_parse_all(uint n_pos, const mp_obj_t *pos, mp_map_t *kws, uint n_allowed, const mp_arg_parse_t *allowed, mp_arg_parse_val_t *out_vals);
mp_obj_dict_t *mp_locals_get(void); mp_obj_dict_t *mp_locals_get(void);
void mp_locals_set(mp_obj_dict_t *d); void mp_locals_set(mp_obj_dict_t *d);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment