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

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.
parent 1d34e324
......@@ -49,12 +49,14 @@ STATIC void bool_print(void (*print)(void *env, const char *fmt, ...), void *env
}
STATIC mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0: return mp_const_false;
case 1: if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bool takes at most 1 argument, %d given", n_args));
case 0:
return mp_const_false;
case 1:
default: // must be 0 or 1
if (mp_obj_is_true(args[0])) { return mp_const_true; } else { return mp_const_false; }
}
}
......
......@@ -34,6 +34,7 @@
#include "obj.h"
#include "parsenum.h"
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
......@@ -74,7 +75,7 @@ STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *
}
STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 2, false);
switch (n_args) {
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
return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
}
case 2: {
case 2:
default: {
mp_float_t real, imag;
if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
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
}
return mp_obj_new_complex(real, imag);
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "complex takes at most 2 arguments, %d given", n_args));
}
}
......
......@@ -37,6 +37,7 @@
#include "obj.h"
#include "parsenum.h"
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_ENABLE_FLOAT
......@@ -66,13 +67,14 @@ STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *en
}
STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0:
return mp_obj_new_float(0);
case 1:
default:
if (MP_OBJ_IS_STR(args[0])) {
// a string, parse it
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
// something else, try to cast it to a float
return mp_obj_new_float(mp_obj_get_float(args[0]));
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "float takes at most 1 argument, %d given", n_args));
}
}
......
......@@ -46,7 +46,7 @@
// This dispatcher function is expected to be independent of the implementation of long int
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) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 2, false);
switch (n_args) {
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
}
case 2:
{
default: {
// should be a string, parse it
// TODO proper error checking of argument types
uint l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
}
}
......
......@@ -69,7 +69,7 @@ STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
}
STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0:
......@@ -77,15 +77,12 @@ STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
return mp_obj_new_list(0, NULL);
case 1:
{
default: {
// make list from iterable
// TODO: optimize list/tuple
mp_obj_t list = mp_obj_new_list(0, NULL);
return list_extend_from_iter(list, args[0]);
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args));
}
}
......
......@@ -42,13 +42,10 @@ typedef struct _mp_obj_property_t {
} mp_obj_property_t;
STATIC mp_obj_t property_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 4, false);
mp_obj_property_t *o = m_new_obj(mp_obj_property_t);
o->base.type = &mp_type_property;
if (n_args >= 5) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "property takes at most 4 arguments"));
}
if (n_args >= 4) {
// doc ignored
}
......
......@@ -57,14 +57,15 @@ void mp_obj_tuple_print(void (*print)(void *env, const char *fmt, ...), void *en
}
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) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, 1, false);
switch (n_args) {
case 0:
// return a empty tuple
return mp_const_empty_tuple;
case 1: {
case 1:
default: {
// 1 argument, an iterable from which we make a new tuple
if (MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
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,
return tuple;
}
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "tuple takes at most 1 argument, %d given", n_args));
}
}
......
......@@ -540,7 +540,7 @@ STATIC void type_print(void (*print)(void *env, const char *fmt, ...), void *env
}
STATIC mp_obj_t type_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 1, 3, false);
switch (n_args) {
case 1:
......
......@@ -40,7 +40,7 @@ typedef struct _mp_obj_zip_t {
} mp_obj_zip_t;
STATIC mp_obj_t zip_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// TODO check n_kw == 0
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
o->base.type = &mp_type_zip;
......
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