argcheck.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(MP_ERROR_TEXT("function doesn't take keyword arguments"));
  40. #endif
  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. mp_raise_msg_varg(&mp_type_TypeError,
  48. MP_ERROR_TEXT("function takes %d positional arguments but %d were given"),
  49. n_args_min, n_args);
  50. #endif
  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. mp_raise_msg_varg(&mp_type_TypeError,
  58. MP_ERROR_TEXT("function missing %d required positional arguments"),
  59. n_args_min - n_args);
  60. #endif
  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. mp_raise_msg_varg(&mp_type_TypeError,
  66. MP_ERROR_TEXT("function expected at most %d arguments, got %d"),
  67. n_args_max, n_args);
  68. #endif
  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. mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%q' argument required"), allowed[i].qst);
  90. #endif
  91. }
  92. out_vals[i] = allowed[i].defval;
  93. continue;
  94. } else {
  95. kws_found++;
  96. given_arg = kw->value;
  97. }
  98. }
  99. if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_BOOL) {
  100. out_vals[i].u_bool = mp_obj_is_true(given_arg);
  101. } else if ((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_INT) {
  102. out_vals[i].u_int = mp_obj_get_int(given_arg);
  103. } else {
  104. assert((allowed[i].flags & MP_ARG_KIND_MASK) == MP_ARG_OBJ);
  105. out_vals[i].u_obj = given_arg;
  106. }
  107. }
  108. if (pos_found < n_pos) {
  109. extra_positional:
  110. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  111. mp_arg_error_terse_mismatch();
  112. #else
  113. // TODO better error message
  114. mp_raise_TypeError(MP_ERROR_TEXT("extra positional arguments given"));
  115. #endif
  116. }
  117. if (kws_found < kws->used) {
  118. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  119. mp_arg_error_terse_mismatch();
  120. #else
  121. // TODO better error message
  122. mp_raise_TypeError(MP_ERROR_TEXT("extra keyword arguments given"));
  123. #endif
  124. }
  125. }
  126. 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) {
  127. mp_map_t kw_args;
  128. mp_map_init_fixed_table(&kw_args, n_kw, args + n_pos);
  129. mp_arg_parse_all(n_pos, args, &kw_args, n_allowed, allowed, out_vals);
  130. }
  131. NORETURN void mp_arg_error_terse_mismatch(void) {
  132. mp_raise_TypeError(MP_ERROR_TEXT("argument num/types mismatch"));
  133. }
  134. #if MICROPY_CPYTHON_COMPAT
  135. NORETURN void mp_arg_error_unimpl_kw(void) {
  136. mp_raise_NotImplementedError(MP_ERROR_TEXT("keyword argument(s) not implemented - use normal args instead"));
  137. }
  138. #endif