Skip to content

Commit bc18155

Browse files
committed
py/modsys: Add optional attribute delegation.
To be enabled when needed by specific sys attributes. Signed-off-by: Damien George <damien@micropython.org>
1 parent 3356b5e commit bc18155

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎py/modsys.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "py/builtin.h"
2929
#include "py/objlist.h"
30+
#include "py/objmodule.h"
3031
#include "py/objtuple.h"
3132
#include "py/objstr.h"
3233
#include "py/objint.h"
@@ -182,6 +183,18 @@ STATIC mp_obj_t mp_sys_settrace(mp_obj_t obj) {
182183
MP_DEFINE_CONST_FUN_OBJ_1(mp_sys_settrace_obj, mp_sys_settrace);
183184
#endif // MICROPY_PY_SYS_SETTRACE
184185

186+
#if MICROPY_PY_SYS_ATTR_DELEGATION
187+
STATIC const uint16_t sys_mutable_keys[] = {
188+
MP_QSTRnull,
189+
};
190+
191+
STATIC void mp_module_sys_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
192+
MP_STATIC_ASSERT(MP_ARRAY_SIZE(sys_mutable_keys) == MP_SYS_MUTABLE_NUM + 1);
193+
MP_STATIC_ASSERT(MP_ARRAY_SIZE(MP_STATE_VM(sys_mutable)) == MP_SYS_MUTABLE_NUM);
194+
mp_module_generic_attr(attr, dest, sys_mutable_keys, MP_STATE_VM(sys_mutable));
195+
}
196+
#endif
197+
185198
STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
186199
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys) },
187200

@@ -244,6 +257,11 @@ STATIC const mp_rom_map_elem_t mp_module_sys_globals_table[] = {
244257
#if MICROPY_PY_SYS_ATEXIT
245258
{ MP_ROM_QSTR(MP_QSTR_atexit), MP_ROM_PTR(&mp_sys_atexit_obj) },
246259
#endif
260+
261+
#if MICROPY_PY_SYS_ATTR_DELEGATION
262+
// Delegation of attr lookup.
263+
MP_MODULE_ATTR_DELEGATION_ENTRY(&mp_module_sys_attr),
264+
#endif
247265
};
248266

249267
STATIC MP_DEFINE_CONST_DICT(mp_module_sys_globals, mp_module_sys_globals_table);

‎py/mpconfig.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,12 @@ typedef double mp_float_t;
13771377
#define MICROPY_PY_SYS_STDIO_BUFFER (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
13781378
#endif
13791379

1380+
// Whether the sys module supports attribute delegation
1381+
// This is enabled automatically when needed by other features
1382+
#ifndef MICROPY_PY_SYS_ATTR_DELEGATION
1383+
#define MICROPY_PY_SYS_ATTR_DELEGATION (0)
1384+
#endif
1385+
13801386
// Whether to provide "uerrno" module
13811387
#ifndef MICROPY_PY_UERRNO
13821388
#define MICROPY_PY_UERRNO (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)

‎py/mpstate.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
// memory system, runtime and virtual machine. The state is a global
4141
// variable, but in the future it is hoped that the state can become local.
4242

43+
enum {
44+
MP_SYS_MUTABLE_NUM,
45+
};
46+
4347
// This structure contains dynamic configuration for the compiler.
4448
#if MICROPY_DYNAMIC_COMPILER
4549
typedef struct mp_dynamic_compiler_t {
@@ -158,6 +162,11 @@ typedef struct _mp_state_vm_t {
158162
// must be initialised after the call to mp_init.
159163
mp_obj_list_t mp_sys_path_obj;
160164
mp_obj_list_t mp_sys_argv_obj;
165+
166+
#if MICROPY_PY_SYS_ATTR_DELEGATION
167+
// Contains mutable sys attributes.
168+
mp_obj_t sys_mutable[MP_SYS_MUTABLE_NUM];
169+
#endif
161170
#endif
162171

163172
// dictionary for overridden builtins

0 commit comments

Comments
 (0)