|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 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 "py/obj.h" |
| 28 | + |
| 29 | +#if MICROPY_PY_TYPING |
| 30 | + |
| 31 | +// Implement roughly the equivalent of the following minimal Python typing module, meant to support the |
| 32 | +// typing syntax at runtime but otherwise ignoring any functionality: |
| 33 | +// |
| 34 | +// class _AnyCall: |
| 35 | +// def __init__(*args, **kwargs): |
| 36 | +// pass |
| 37 | +// |
| 38 | +// def __call__(self, *args, **kwargs): |
| 39 | +// return self |
| 40 | +// |
| 41 | +// def __getitem__(self, attr): |
| 42 | +// return self |
| 43 | +// |
| 44 | +// _typing_obj = _AnyCall() |
| 45 | +// |
| 46 | +// def __getattr__(attr): |
| 47 | +// return _typing_obj |
| 48 | +// |
| 49 | +// Note this works together with the micropython compiler itself ignoring type hints, i.e. when encountering |
| 50 | +// |
| 51 | +// def hello(name: str) -> None: |
| 52 | +// pass |
| 53 | +// |
| 54 | +// both str and None hints are simply ignored. |
| 55 | + |
| 56 | +typedef struct _mp_obj_any_call_t |
| 57 | +{ |
| 58 | + mp_obj_base_t base; |
| 59 | +} mp_obj_any_call_t; |
| 60 | + |
| 61 | +extern const mp_obj_type_t mp_type_any_call_t; |
| 62 | + |
| 63 | + |
| 64 | +// Can be used both for __new__ and __call__: the latter's prototype is |
| 65 | +// (mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) |
| 66 | +// so this function works as long as the argument size matches. |
| 67 | +static mp_obj_t any_call_new(const mp_obj_type_t *arg0, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 68 | + #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D |
| 69 | + MP_STATIC_ASSERT(sizeof(mp_obj_type_t *) == sizeof(mp_obj_t)); |
| 70 | + #endif |
| 71 | + // If it's an actual instance we're used for __call__ so return self_in. |
| 72 | + if (mp_obj_get_type(MP_OBJ_FROM_PTR(arg0)) == &mp_type_any_call_t) { |
| 73 | + return MP_OBJ_FROM_PTR(arg0); |
| 74 | + } |
| 75 | + // Could also be we're being called as a function/decorator, return the decorated thing then. |
| 76 | + // TODO obviously a bit of a hack, plus doesn't work for decorators with arguments. |
| 77 | + // Note could test mp_obj_is_fun on the first arg here, then being called as decorator etc that |
| 78 | + // is true, but turns out just returning the last argument works in more cases, like |
| 79 | + // UserId = typing.NewType("UserId", int) |
| 80 | + // assert UserId(1) == 1 |
| 81 | + if (n_args != 0) { |
| 82 | + return args[n_args - 1]; |
| 83 | + } |
| 84 | + return mp_obj_malloc(mp_obj_any_call_t, arg0); |
| 85 | +} |
| 86 | + |
| 87 | +#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D |
| 88 | +static mp_obj_t any_call_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 89 | + if (mp_obj_get_type(self_in) == &mp_type_any_call_t || n_args == 0) { |
| 90 | + return self_in; |
| 91 | + } |
| 92 | + return args[n_args - 1]; |
| 93 | +} |
| 94 | +#else |
| 95 | +#define any_call_call any_call_new |
| 96 | +#endif |
| 97 | + |
| 98 | +#if defined(MICROPY_UNIX_COVERAGE) |
| 99 | +void any_call_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); |
| 100 | +#endif |
| 101 | + |
| 102 | +static mp_obj_t any_call_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) { |
| 103 | + return self_in; |
| 104 | +} |
| 105 | + |
| 106 | +// TODO could probably apply same trick as in any_call_new here and merge any_call_module_attr, |
| 107 | +// but still have to test if that's worth it code-size wise. |
| 108 | +static void any_call_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
| 109 | + // Only loading is supported. |
| 110 | + if (dest[0] == MP_OBJ_NULL) { |
| 111 | + if (attr != MP_QSTR___str__ && attr != MP_QSTR___repr__) { |
| 112 | + dest[0] = self_in; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void any_call_module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { |
| 118 | + // Only loading is supported. |
| 119 | + if (dest[0] == MP_OBJ_NULL) { |
| 120 | + dest[0] = MP_OBJ_FROM_PTR(&mp_type_any_call_t); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +static MP_DEFINE_CONST_OBJ_TYPE( |
| 125 | + mp_type_any_call_t, |
| 126 | + MP_QSTR_any_call, |
| 127 | + MP_TYPE_FLAG_NONE, |
| 128 | + make_new, any_call_new, |
| 129 | + attr, any_call_attr, |
| 130 | + subscr, any_call_subscr, |
| 131 | + call, any_call_call |
| 132 | + ); |
| 133 | + |
| 134 | + |
| 135 | +static const mp_rom_map_elem_t mp_module_typing_globals_table[] = { |
| 136 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_typing) }, |
| 137 | +}; |
| 138 | + |
| 139 | +static MP_DEFINE_CONST_DICT(mp_module_typing_globals, mp_module_typing_globals_table); |
| 140 | + |
| 141 | +const mp_obj_module_t mp_module_typing = { |
| 142 | + .base = { &mp_type_module }, |
| 143 | + .globals = (mp_obj_dict_t *)&mp_module_typing_globals, |
| 144 | +}; |
| 145 | + |
| 146 | +// Extensible such that a typing module implemented in Python still has priority. |
| 147 | +MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_typing, mp_module_typing); |
| 148 | +MP_REGISTER_MODULE_DELEGATION(mp_module_typing, any_call_module_attr); |
| 149 | + |
| 150 | + |
| 151 | +#if MICROPY_PY_TYPING_EXTRA_MODULES |
| 152 | +MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_abc, mp_module_typing); |
| 153 | +MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR___future__, mp_module_typing); |
| 154 | +MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_typing_extensions, mp_module_typing); |
| 155 | +#endif |
| 156 | + |
| 157 | +#endif // MICROPY_PY_TYPING |
0 commit comments