Paul Sokolovsky | d32bab2 | 2014-05-01 02:53:07 +0300 | [diff] [blame] | 1 | /* |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
Paul Sokolovsky | d32bab2 | 2014-05-01 02:53:07 +0300 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 6 | * Copyright (c) 2013, 2014 Damien P. George |
Paul Sokolovsky | d32bab2 | 2014-05-01 02:53:07 +0300 | [diff] [blame] | 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 | */ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 26 | |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
| 29 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 30 | #include "py/nlr.h" |
| 31 | #include "py/runtime.h" |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 32 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 33 | void 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 George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 34 | // TODO maybe take the function name as an argument so we can print nicer error messages |
| 35 | |
| 36 | if (n_kw && !takes_kw) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 37 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 38 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 39 | } else { |
| 40 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 41 | "function does not take keyword arguments")); |
| 42 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | if (n_args_min == n_args_max) { |
| 46 | if (n_args != n_args_min) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 47 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 48 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 49 | } else { |
| 50 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 51 | "function takes %d positional arguments but %d were given", |
| 52 | n_args_min, n_args)); |
| 53 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 54 | } |
| 55 | } else { |
| 56 | if (n_args < n_args_min) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 57 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 58 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 59 | } else { |
| 60 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 61 | "function missing %d required positional arguments", |
| 62 | n_args_min - n_args)); |
| 63 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 64 | } else if (n_args > n_args_max) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 65 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 66 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 67 | } else { |
| 68 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
| 69 | "function expected at most %d arguments, got %d", |
| 70 | n_args_max, n_args)); |
| 71 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 76 | void 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) { |
| 77 | mp_uint_t pos_found = 0, kws_found = 0; |
| 78 | for (mp_uint_t i = 0; i < n_allowed; i++) { |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 79 | mp_obj_t given_arg; |
| 80 | if (i < n_pos) { |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 81 | if (allowed[i].flags & MP_ARG_KW_ONLY) { |
Damien George | 64ba6ca | 2014-04-21 00:09:44 +0100 | [diff] [blame] | 82 | goto extra_positional; |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 83 | } |
| 84 | pos_found++; |
| 85 | given_arg = pos[i]; |
| 86 | } else { |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 87 | mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP); |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 88 | if (kw == NULL) { |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 89 | if (allowed[i].flags & MP_ARG_REQUIRED) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 90 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 91 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 92 | } else { |
| 93 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 94 | "'%q' argument required", allowed[i].qst)); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 95 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 96 | } |
| 97 | out_vals[i] = allowed[i].defval; |
| 98 | continue; |
| 99 | } else { |
| 100 | kws_found++; |
| 101 | given_arg = kw->value; |
| 102 | } |
| 103 | } |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 104 | if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) { |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 105 | out_vals[i].u_bool = mp_obj_is_true(given_arg); |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 106 | } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) { |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 107 | out_vals[i].u_int = mp_obj_get_int(given_arg); |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 108 | } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ) { |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 109 | out_vals[i].u_obj = given_arg; |
| 110 | } else { |
| 111 | assert(0); |
| 112 | } |
| 113 | } |
| 114 | if (pos_found < n_pos) { |
Damien George | 64ba6ca | 2014-04-21 00:09:44 +0100 | [diff] [blame] | 115 | extra_positional: |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 116 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 117 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 118 | } else { |
| 119 | // TODO better error message |
| 120 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 121 | "extra positional arguments given")); |
| 122 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 123 | } |
| 124 | if (kws_found < kws->used) { |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 125 | if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 126 | mp_arg_error_terse_mismatch(); |
Damien George | 1e9a92f | 2014-11-06 17:36:16 +0000 | [diff] [blame] | 127 | } else { |
| 128 | // TODO better error message |
| 129 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, |
| 130 | "extra keyword arguments given")); |
| 131 | } |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
Damien George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 134 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 135 | void 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 George | 491cbd6 | 2014-05-06 16:38:54 +0000 | [diff] [blame] | 136 | mp_map_t kw_args; |
| 137 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos); |
| 138 | mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals); |
| 139 | } |
Damien George | c53b408 | 2014-05-06 16:52:35 +0000 | [diff] [blame] | 140 | |
stijn | fbfd355 | 2015-01-02 12:05:44 +0100 | [diff] [blame] | 141 | #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE || _MSC_VER |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 142 | NORETURN void mp_arg_error_terse_mismatch(void) { |
| 143 | nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "argument num/types mismatch")); |
| 144 | } |
| 145 | #endif |
| 146 | |
Paul Sokolovsky | 47d3bd3 | 2014-05-06 19:25:25 +0300 | [diff] [blame] | 147 | #if MICROPY_CPYTHON_COMPAT |
Damien George | c53b408 | 2014-05-06 16:52:35 +0000 | [diff] [blame] | 148 | NORETURN void mp_arg_error_unimpl_kw(void) { |
Damien George | 821b7f2 | 2015-09-03 23:14:06 +0100 | [diff] [blame^] | 149 | mp_not_implemented("keyword argument(s) not yet implemented - use normal args instead"); |
Paul Sokolovsky | 47d3bd3 | 2014-05-06 19:25:25 +0300 | [diff] [blame] | 150 | } |
| 151 | #endif |