1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2013, 2014 Damien P. George |
7 | * |
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 | */ |
26 | |
27 | #include <stdlib.h> |
28 | #include <assert.h> |
29 | |
30 | #include "py/runtime.h" |
31 | |
32 | void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) { |
33 | // TODO maybe take the function name as an argument so we can print nicer error messages |
34 | |
35 | // The reverse of MP_OBJ_FUN_MAKE_SIG |
36 | bool takes_kw = sig & 1; |
37 | size_t n_args_min = sig >> 17; |
38 | size_t n_args_max = (sig >> 1) & 0xffff; |
39 | |
40 | if (n_kw && !takes_kw) { |
41 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
42 | mp_arg_error_terse_mismatch(); |
43 | #else |
44 | mp_raise_TypeError(MP_ERROR_TEXT("function doesn't take keyword arguments" )); |
45 | #endif |
46 | } |
47 | |
48 | if (n_args_min == n_args_max) { |
49 | if (n_args != n_args_min) { |
50 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
51 | mp_arg_error_terse_mismatch(); |
52 | #else |
53 | mp_raise_msg_varg(&mp_type_TypeError, |
54 | MP_ERROR_TEXT("function takes %d positional arguments but %d were given" ), |
55 | n_args_min, n_args); |
56 | #endif |
57 | } |
58 | } else { |
59 | if (n_args < n_args_min) { |
60 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
61 | mp_arg_error_terse_mismatch(); |
62 | #else |
63 | mp_raise_msg_varg(&mp_type_TypeError, |
64 | MP_ERROR_TEXT("function missing %d required positional arguments" ), |
65 | n_args_min - n_args); |
66 | #endif |
67 | } else if (n_args > n_args_max) { |
68 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
69 | mp_arg_error_terse_mismatch(); |
70 | #else |
71 | mp_raise_msg_varg(&mp_type_TypeError, |
72 | MP_ERROR_TEXT("function expected at most %d arguments, got %d" ), |
73 | n_args_max, n_args); |
74 | #endif |
75 | } |
76 | } |
77 | } |
78 | |
79 | void 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) { |
80 | size_t pos_found = 0, kws_found = 0; |
81 | for (size_t i = 0; i < n_allowed; i++) { |
82 | mp_obj_t given_arg; |
83 | if (i < n_pos) { |
84 | if (allowed[i].flags & MP_ARG_KW_ONLY) { |
85 | goto extra_positional; |
86 | } |
87 | pos_found++; |
88 | given_arg = pos[i]; |
89 | } else { |
90 | mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP); |
91 | if (kw == NULL) { |
92 | if (allowed[i].flags & MP_ARG_REQUIRED) { |
93 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
94 | mp_arg_error_terse_mismatch(); |
95 | #else |
96 | mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%q' argument required" ), allowed[i].qst); |
97 | #endif |
98 | } |
99 | out_vals[i] = allowed[i].defval; |
100 | continue; |
101 | } else { |
102 | kws_found++; |
103 | given_arg = kw->value; |
104 | } |
105 | } |
106 | if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) { |
107 | out_vals[i].u_bool = mp_obj_is_true(given_arg); |
108 | } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) { |
109 | out_vals[i].u_int = mp_obj_get_int(given_arg); |
110 | } else { |
111 | assert((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ); |
112 | out_vals[i].u_obj = given_arg; |
113 | } |
114 | } |
115 | if (pos_found < n_pos) { |
116 | : |
117 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
118 | mp_arg_error_terse_mismatch(); |
119 | #else |
120 | // TODO better error message |
121 | mp_raise_TypeError(MP_ERROR_TEXT("extra positional arguments given" )); |
122 | #endif |
123 | } |
124 | if (kws_found < kws->used) { |
125 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
126 | mp_arg_error_terse_mismatch(); |
127 | #else |
128 | // TODO better error message |
129 | mp_raise_TypeError(MP_ERROR_TEXT("extra keyword arguments given" )); |
130 | #endif |
131 | } |
132 | } |
133 | |
134 | void 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) { |
135 | mp_map_t kw_args; |
136 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos); |
137 | mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals); |
138 | } |
139 | |
140 | NORETURN void mp_arg_error_terse_mismatch(void) { |
141 | mp_raise_TypeError(MP_ERROR_TEXT("argument num/types mismatch" )); |
142 | } |
143 | |
144 | #if MICROPY_CPYTHON_COMPAT |
145 | NORETURN void mp_arg_error_unimpl_kw(void) { |
146 | mp_raise_NotImplementedError(MP_ERROR_TEXT("keyword argument(s) not yet implemented - use normal args instead" )); |
147 | } |
148 | #endif |
149 | |