From 50149a573063dc4a1a6541686e61c310d5fc8353 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 20 Jan 2015 14:11:27 +0000
Subject: [PATCH] py: Use mp_arg_check_num in some _make_new functions.

Reduces stmhal code size by about 250 bytes.
---
 py/objexcept.c | 12 +++---------
 py/objfilter.c |  6 +-----
 py/objmap.c    |  6 +-----
 py/objobject.c |  9 ++-------
 py/objtype.c   |  8 +++-----
 5 files changed, 10 insertions(+), 31 deletions(-)

diff --git a/py/objexcept.c b/py/objexcept.c
index 837e2e85e..e872db341 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -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;
 }
diff --git a/py/objfilter.c b/py/objfilter.c
index c9ded8d7f..a97c9f554 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 3225b4dc9..d9df9fdea 100644
--- a/py/objmap.c
+++ b/py/objmap.c
@@ -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;
diff --git a/py/objobject.c b/py/objobject.c
index 87295b765..c5675c071 100644
--- a/py/objobject.c
+++ b/py/objobject.c
@@ -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;
diff --git a/py/objtype.c b/py/objtype.c
index 8928de941..46e892a12 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]);
 }
 
-- 
GitLab