argcheck.c 5.8 KB

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