|
31 | 31 | #include "py/runtime.h"
|
32 | 32 |
|
33 | 33 | void 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) {
|
| 34 | + // NOTE(tannewt): This prevents this function from being optimized away. |
| 35 | + // Without it, functions can crash when reading invalid args. |
| 36 | + asm (""); |
34 | 37 | // TODO maybe take the function name as an argument so we can print nicer error messages
|
35 | 38 |
|
36 | 39 | if (n_kw && !takes_kw) {
|
37 |
| - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 40 | + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
38 | 41 | mp_arg_error_terse_mismatch();
|
39 |
| - } else { |
| 42 | + #else |
40 | 43 | mp_raise_TypeError("function does not take keyword arguments");
|
41 |
| - } |
| 44 | + #endif |
42 | 45 | }
|
43 | 46 |
|
44 | 47 | if (n_args_min == n_args_max) {
|
45 | 48 | if (n_args != n_args_min) {
|
46 |
| - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 49 | + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
47 | 50 | mp_arg_error_terse_mismatch();
|
48 |
| - } else { |
| 51 | + #else |
49 | 52 | mp_raise_TypeError_varg(
|
50 | 53 | "function takes %d positional arguments but %d were given",
|
51 | 54 | n_args_min, n_args);
|
52 |
| - } |
| 55 | + #endif |
53 | 56 | }
|
54 | 57 | } else {
|
55 | 58 | if (n_args < n_args_min) {
|
56 |
| - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 59 | + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
57 | 60 | mp_arg_error_terse_mismatch();
|
58 |
| - } else { |
| 61 | + #else |
59 | 62 | mp_raise_TypeError_varg(
|
60 | 63 | "function missing %d required positional arguments",
|
61 | 64 | n_args_min - n_args);
|
62 |
| - } |
| 65 | + #endif |
63 | 66 | } else if (n_args > n_args_max) {
|
64 |
| - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { |
| 67 | + #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE |
65 | 68 | mp_arg_error_terse_mismatch();
|
66 |
| - } else { |
| 69 | + #else |
67 | 70 | mp_raise_TypeError_varg(
|
68 | 71 | "function expected at most %d arguments, got %d",
|
69 | 72 | n_args_max, n_args);
|
70 |
| - } |
| 73 | + #endif |
71 | 74 | }
|
72 | 75 | }
|
73 | 76 | }
|
|
0 commit comments