Skip to content

Commit 377b80b

Browse files
committed
py: Print imported module's location (__file__) if available.
1 parent 5c00757 commit 377b80b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

bare-arm/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MICROPY_PY_BUILTINS_SET (0)
1818
#define MICROPY_PY_BUILTINS_SLICE (0)
1919
#define MICROPY_PY_BUILTINS_PROPERTY (0)
20+
#define MICROPY_PY___FILE__ (0)
2021
#define MICROPY_PY_GC (0)
2122
#define MICROPY_PY_ARRAY (0)
2223
#define MICROPY_PY_COLLECTIONS (0)

py/objmodule.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ STATIC mp_map_t mp_loaded_modules_map; // TODO: expose as sys.modules
4040

4141
STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
4242
mp_obj_module_t *self = self_in;
43-
print(env, "<module '%s' from '-unknown-file-'>", qstr_str(self->name));
43+
const char *name = qstr_str(self->name);
44+
45+
#if MICROPY_PY___FILE__
46+
// If we store __file__ to imported modules then try to lookup this
47+
// symbol to give more information about the module.
48+
mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP);
49+
if (elem != NULL) {
50+
print(env, "<module '%s' from '%s'>", name, mp_obj_str_get_str(elem->value));
51+
return;
52+
}
53+
#endif
54+
55+
print(env, "<module '%s'>", name);
4456
}
4557

4658
STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {

0 commit comments

Comments
 (0)