blob: 4bc0629b12a7844cb3814894b73d4829f11befba [file] [log] [blame]
Paul Sokolovskyd32bab22014-05-01 02:53:07 +03001/*
Damien George04b91472014-05-03 23:27:38 +01002 * This file is part of the Micro Python project, http://micropython.org/
Paul Sokolovskyd32bab22014-05-01 02:53:07 +03003 *
4 * The MIT License (MIT)
5 *
Damien George04b91472014-05-03 23:27:38 +01006 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyd32bab22014-05-01 02:53:07 +03007 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
Damien George04b91472014-05-03 23:27:38 +010026
Damien Georgea3f94e02014-04-20 00:13:22 +010027#include <stdlib.h>
28#include <assert.h>
29
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damien Georgea3f94e02014-04-20 00:13:22 +010031#include "nlr.h"
32#include "misc.h"
Damien Georgea3f94e02014-04-20 00:13:22 +010033#include "qstr.h"
34#include "obj.h"
35#include "runtime.h"
36
Damien George4abff752014-08-30 14:59:21 +010037void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw) {
Damien Georgea3f94e02014-04-20 00:13:22 +010038 // TODO maybe take the function name as an argument so we can print nicer error messages
39
40 if (n_kw && !takes_kw) {
Damien George1e9a92f2014-11-06 17:36:16 +000041 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000042 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000043 } else {
44 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
45 "function does not take keyword arguments"));
46 }
Damien Georgea3f94e02014-04-20 00:13:22 +010047 }
48
49 if (n_args_min == n_args_max) {
50 if (n_args != n_args_min) {
Damien George1e9a92f2014-11-06 17:36:16 +000051 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000052 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000053 } else {
54 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
55 "function takes %d positional arguments but %d were given",
56 n_args_min, n_args));
57 }
Damien Georgea3f94e02014-04-20 00:13:22 +010058 }
59 } else {
60 if (n_args < n_args_min) {
Damien George1e9a92f2014-11-06 17:36:16 +000061 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000062 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000063 } else {
64 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
65 "function missing %d required positional arguments",
66 n_args_min - n_args));
67 }
Damien Georgea3f94e02014-04-20 00:13:22 +010068 } else if (n_args > n_args_max) {
Damien George1e9a92f2014-11-06 17:36:16 +000069 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000070 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000071 } else {
72 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
73 "function expected at most %d arguments, got %d",
74 n_args_max, n_args));
75 }
Damien Georgea3f94e02014-04-20 00:13:22 +010076 }
77 }
78}
79
Damien George4abff752014-08-30 14:59:21 +010080void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
81 mp_uint_t pos_found = 0, kws_found = 0;
82 for (mp_uint_t i = 0; i < n_allowed; i++) {
Damien Georgea3f94e02014-04-20 00:13:22 +010083 mp_obj_t given_arg;
84 if (i < n_pos) {
Damien Georgedbc81df2014-04-26 11:19:17 +010085 if (allowed[i].flags & MP_ARG_KW_ONLY) {
Damien George64ba6ca2014-04-21 00:09:44 +010086 goto extra_positional;
Damien Georgea3f94e02014-04-20 00:13:22 +010087 }
88 pos_found++;
89 given_arg = pos[i];
90 } else {
91 mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qstr), MP_MAP_LOOKUP);
92 if (kw == NULL) {
Damien Georgedbc81df2014-04-26 11:19:17 +010093 if (allowed[i].flags & MP_ARG_REQUIRED) {
Damien George1e9a92f2014-11-06 17:36:16 +000094 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000095 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000096 } else {
97 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
98 "'%s' argument required",
99 qstr_str(allowed[i].qstr)));
100 }
Damien Georgea3f94e02014-04-20 00:13:22 +0100101 }
102 out_vals[i] = allowed[i].defval;
103 continue;
104 } else {
105 kws_found++;
106 given_arg = kw->value;
107 }
108 }
Damien Georgedbc81df2014-04-26 11:19:17 +0100109 if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100110 out_vals[i].u_bool = mp_obj_is_true(given_arg);
Damien Georgedbc81df2014-04-26 11:19:17 +0100111 } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100112 out_vals[i].u_int = mp_obj_get_int(given_arg);
Damien Georgedbc81df2014-04-26 11:19:17 +0100113 } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100114 out_vals[i].u_obj = given_arg;
115 } else {
116 assert(0);
117 }
118 }
119 if (pos_found < n_pos) {
Damien George64ba6ca2014-04-21 00:09:44 +0100120 extra_positional:
Damien George1e9a92f2014-11-06 17:36:16 +0000121 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +0000122 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +0000123 } else {
124 // TODO better error message
125 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
126 "extra positional arguments given"));
127 }
Damien Georgea3f94e02014-04-20 00:13:22 +0100128 }
129 if (kws_found < kws->used) {
Damien George1e9a92f2014-11-06 17:36:16 +0000130 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +0000131 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +0000132 } else {
133 // TODO better error message
134 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
135 "extra keyword arguments given"));
136 }
Damien Georgea3f94e02014-04-20 00:13:22 +0100137 }
138}
Damien George491cbd62014-05-06 16:38:54 +0000139
Damien George4abff752014-08-30 14:59:21 +0100140void mp_arg_parse_all_kw_array(mp_uint_t n_pos, mp_uint_t n_kw, const mp_obj_t *args, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
Damien George491cbd62014-05-06 16:38:54 +0000141 mp_map_t kw_args;
142 mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
143 mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
144}
Damien Georgec53b4082014-05-06 16:52:35 +0000145
Damien George7f233842015-01-01 15:33:50 +0000146#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
147NORETURN void mp_arg_error_terse_mismatch(void) {
148 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch"));
149}
150#endif
151
Paul Sokolovsky47d3bd32014-05-06 19:25:25 +0300152#if MICROPY_CPYTHON_COMPAT
Damien Georgec53b4082014-05-06 16:52:35 +0000153NORETURN void mp_arg_error_unimpl_kw(void) {
Paul Sokolovsky47d3bd32014-05-06 19:25:25 +0300154 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
155 "keyword argument(s) not yet implemented - use normal args instead"));
156}
157#endif