py/objmodule: Add a table of built-in modules with delegation.

This replaces the previous QSTR_null entry in the globals dict which could
leak out to Python (e.g. via iteration of mod.__dict__) and could lead to
crashes.

It results in smaller code size at the expense of turning a lookup into a
loop, but the list it is looping over likely only contains one or two
elements.

To allow a module to register its custom attr function it can use the new
`MP_REGISTER_MODULE_DELEGATION` macro.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared
2023-06-05 15:52:57 +10:00
parent eb85f4d4c9
commit 13c817e61c
9 changed files with 67 additions and 36 deletions

View File

@@ -839,8 +839,8 @@ typedef double mp_float_t;
#define MICROPY_STREAMS_POSIX_API (0)
#endif
// Whether modules can use MP_MODULE_ATTR_DELEGATION_ENTRY() to delegate failed
// attribute lookups.
// Whether modules can use MP_REGISTER_MODULE_DELEGATION() to delegate failed
// attribute lookups to a custom handler function.
#ifndef MICROPY_MODULE_ATTR_DELEGATION
#define MICROPY_MODULE_ATTR_DELEGATION (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
#endif