blob: 9f225345d55e3c187d9d810fcbd88c5fc7762ffd [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
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/runtime.h"
Damien Georgea3f94e02014-04-20 00:13:22 +010032
Damien Georgee7cd1692016-03-14 22:35:48 +000033void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) {
Damien Georgea3f94e02014-04-20 00:13:22 +010034 // TODO maybe take the function name as an argument so we can print nicer error messages
35
36 if (n_kw && !takes_kw) {
Damien George1e9a92f2014-11-06 17:36:16 +000037 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000038 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000039 } else {
Damien George94c41bb2017-03-28 22:37:26 +110040 mp_raise_TypeError("function does not take keyword arguments");
Damien George1e9a92f2014-11-06 17:36:16 +000041 }
Damien Georgea3f94e02014-04-20 00:13:22 +010042 }
43
44 if (n_args_min == n_args_max) {
45 if (n_args != n_args_min) {
Damien George1e9a92f2014-11-06 17:36:16 +000046 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000047 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000048 } else {
49 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
50 "function takes %d positional arguments but %d were given",
51 n_args_min, n_args));
52 }
Damien Georgea3f94e02014-04-20 00:13:22 +010053 }
54 } else {
55 if (n_args < n_args_min) {
Damien George1e9a92f2014-11-06 17:36:16 +000056 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000057 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000058 } else {
59 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
60 "function missing %d required positional arguments",
61 n_args_min - n_args));
62 }
Damien Georgea3f94e02014-04-20 00:13:22 +010063 } else if (n_args > n_args_max) {
Damien George1e9a92f2014-11-06 17:36:16 +000064 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000065 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000066 } else {
67 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
68 "function expected at most %d arguments, got %d",
69 n_args_max, n_args));
70 }
Damien Georgea3f94e02014-04-20 00:13:22 +010071 }
72 }
73}
74
Damien Georgee7cd1692016-03-14 22:35:48 +000075void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
76 size_t pos_found = 0, kws_found = 0;
77 for (size_t i = 0; i < n_allowed; i++) {
Damien Georgea3f94e02014-04-20 00:13:22 +010078 mp_obj_t given_arg;
79 if (i < n_pos) {
Damien Georgedbc81df2014-04-26 11:19:17 +010080 if (allowed[i].flags & MP_ARG_KW_ONLY) {
Damien George64ba6ca2014-04-21 00:09:44 +010081 goto extra_positional;
Damien Georgea3f94e02014-04-20 00:13:22 +010082 }
83 pos_found++;
84 given_arg = pos[i];
85 } else {
Damien George50912e72015-01-20 11:55:10 +000086 mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP);
Damien Georgea3f94e02014-04-20 00:13:22 +010087 if (kw == NULL) {
Damien Georgedbc81df2014-04-26 11:19:17 +010088 if (allowed[i].flags & MP_ARG_REQUIRED) {
Damien George1e9a92f2014-11-06 17:36:16 +000089 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +000090 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +000091 } else {
92 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Damien George044c4732015-04-11 13:03:37 +010093 "'%q' argument required", allowed[i].qst));
Damien George1e9a92f2014-11-06 17:36:16 +000094 }
Damien Georgea3f94e02014-04-20 00:13:22 +010095 }
96 out_vals[i] = allowed[i].defval;
97 continue;
98 } else {
99 kws_found++;
100 given_arg = kw->value;
101 }
102 }
Damien Georgedbc81df2014-04-26 11:19:17 +0100103 if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100104 out_vals[i].u_bool = mp_obj_is_true(given_arg);
Damien Georgedbc81df2014-04-26 11:19:17 +0100105 } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) {
Damien Georgea3f94e02014-04-20 00:13:22 +0100106 out_vals[i].u_int = mp_obj_get_int(given_arg);
Damien Georgea3f94e02014-04-20 00:13:22 +0100107 } else {
Damien George3be4f882016-09-30 16:45:43 +1000108 assert((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ);
109 out_vals[i].u_obj = given_arg;
Damien Georgea3f94e02014-04-20 00:13:22 +0100110 }
111 }
112 if (pos_found < n_pos) {
Damien George64ba6ca2014-04-21 00:09:44 +0100113 extra_positional:
Damien George1e9a92f2014-11-06 17:36:16 +0000114 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +0000115 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +0000116 } else {
117 // TODO better error message
Damien George94c41bb2017-03-28 22:37:26 +1100118 mp_raise_TypeError("extra positional arguments given");
Damien George1e9a92f2014-11-06 17:36:16 +0000119 }
Damien Georgea3f94e02014-04-20 00:13:22 +0100120 }
121 if (kws_found < kws->used) {
Damien George1e9a92f2014-11-06 17:36:16 +0000122 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Damien George7f233842015-01-01 15:33:50 +0000123 mp_arg_error_terse_mismatch();
Damien George1e9a92f2014-11-06 17:36:16 +0000124 } else {
125 // TODO better error message
Damien George94c41bb2017-03-28 22:37:26 +1100126 mp_raise_TypeError("extra keyword arguments given");
Damien George1e9a92f2014-11-06 17:36:16 +0000127 }
Damien Georgea3f94e02014-04-20 00:13:22 +0100128 }
129}
Damien George491cbd62014-05-06 16:38:54 +0000130
Damien Georgee7cd1692016-03-14 22:35:48 +0000131void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals) {
Damien George491cbd62014-05-06 16:38:54 +0000132 mp_map_t kw_args;
133 mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
134 mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
135}
Damien Georgec53b4082014-05-06 16:52:35 +0000136
stijnfbfd3552015-01-02 12:05:44 +0100137#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE || _MSC_VER
Damien George7f233842015-01-01 15:33:50 +0000138NORETURN void mp_arg_error_terse_mismatch(void) {
Damien George94c41bb2017-03-28 22:37:26 +1100139 mp_raise_TypeError("argument num/types mismatch");
Damien George7f233842015-01-01 15:33:50 +0000140}
141#endif
142
Paul Sokolovsky47d3bd32014-05-06 19:25:25 +0300143#if MICROPY_CPYTHON_COMPAT
Damien Georgec53b4082014-05-06 16:52:35 +0000144NORETURN void mp_arg_error_unimpl_kw(void) {
Damien George821b7f22015-09-03 23:14:06 +0100145 mp_not_implemented("keyword argument(s) not yet implemented - use normal args instead");
Paul Sokolovsky47d3bd32014-05-06 19:25:25 +0300146}
147#endif