8000 py/objmodule: Implement PEP 562's __getattr__ for modules. · micropython/micropython@454cca6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 454cca6

Browse files
pmp-pdpgeorge
authored andcommitted
py/objmodule: Implement PEP 562's __getattr__ for modules.
Configurable via MICROPY_MODULE_GETATTR, disabled by default. Among other things __getattr__ for modules can help to build lazy loading / code unloading at runtime.
1 parent a527313 commit 454cca6

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

ports/unix/mpconfigport_coverage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define MICROPY_FLOAT_HIGH_QUALITY_HASH (1)
3737
#define MICROPY_ENABLE_SCHEDULER (1)
3838
#define MICROPY_READER_VFS (1)
39+
#define MICROPY_MODULE_GETATTR (1)
3940
#define MICROPY_PY_DELATTR_SETATTR (1)
4041
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
4142
#define MICROPY_PY_BUILTINS_RANGE_BINOP (1)

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ typedef double mp_float_t;
633633
#define MICROPY_MODULE_BUILTIN_INIT (0)
634634
#endif
635635

636+
// Whether to support module-level __getattr__ (see PEP 562)
637+
#ifndef MICROPY_MODULE_GETATTR
638+
#define MICROPY_MODULE_GETATTR (0)
639+
#endif
640+
636641
// Whether module weak links are supported
637642
#ifndef MICROPY_MODULE_WEAK_LINKS
638643
#define MICROPY_MODULE_WEAK_LINKS (0)

py/objmodule.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ STATIC void module_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
6161
mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
6262
if (elem != NULL) {
6363
dest[0] = elem->value;
64+
#if MICROPY_MODULE_GETATTR
65+
} else if (attr != MP_QSTR___getattr__) {
66+
elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___getattr__), MP_MAP_LOOKUP);
67+
if (elem != NULL) {
68+
dest[0] = mp_call_function_1(elem->value, MP_OBJ_NEW_QSTR(attr));
69+
}
70+
#endif
6471
}
6572
} else {
6673
// delete/store attribute

tests/import/module_getattr.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# test __getattr__ on module
2+
3+
# ensure that does_not_exist doesn't exist to start with
4+
this = __import__(__name__)
5+
try:
6+
this.does_not_exist
7+
assert False
8+
except AttributeError:
9+
pass
10+
11+
# define __getattr__
12+
def __getattr__(attr):
13+
if attr == 'does_not_exist':
14+
return False
15+
raise AttributeError
16+
17+
# do feature test (will also test functionality if the feature exists)
18+
if not hasattr(this, 'does_not_exist'):
19+
print('SKIP')
20+
raise SystemExit
21+
22+
# check that __getattr__ works as expected
23+
print(this.does_not_exist)

0 commit comments

Comments
 (0)
0