From ee7a880d6e669423309c3a5f8c20b59027604572 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Sun, 11 May 2014 18:37:21 +0100
Subject: [PATCH] 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.
---
 py/objbool.c     | 10 ++++++----
 py/objcomplex.c  |  9 ++++-----
 py/objfloat.c    |  7 +++----
 py/objint.c      |  7 ++-----
 py/objlist.c     |  7 ++-----
 py/objproperty.c |  5 +----
 py/objtuple.c    |  8 +++-----
 py/objtype.c     |  2 +-
 py/objzip.c      |  2 +-
 9 files changed, 23 insertions(+), 34 deletions(-)

diff --git a/py/objbool.c b/py/objbool.c
index 179ab7659..e31e7a2a4 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -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; }
     }
 }
 
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 1caa49ede..18e0edc51 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -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));
     }
 }
 
diff --git a/py/objfloat.c b/py/objfloat.c
index 5260fadc1..e27942143 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -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));
     }
 }
 
diff --git a/py/objint.c b/py/objint.c
index 73b4c5d0b..d1d99a253 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -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));
     }
 }
 
diff --git a/py/objlist.c b/py/objlist.c
index 9e30ebb4a..0ef685dae 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -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));
     }
 }
 
diff --git a/py/objproperty.c b/py/objproperty.c
index 439cb9ad8..b4f2e7d49 100644
--- a/py/objproperty.c
+++ b/py/objproperty.c
@@ -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
     }
diff --git a/py/objtuple.c b/py/objtuple.c
index 1ec75239b..44ee95dd9 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -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));
     }
 }
 
diff --git a/py/objtype.c b/py/objtype.c
index c579477db..5f3e1db5d 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -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:
diff --git a/py/objzip.c b/py/objzip.c
index 8f8946264..97a14eb87 100644
--- a/py/objzip.c
+++ b/py/objzip.c
@@ -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;
-- 
GitLab