From 377b80b62450bde6fc0c06f50254f3c751127247 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 8 Sep 2014 10:45:23 +0100 Subject: [PATCH] py: Print imported module's location (__file__) if available. --- bare-arm/mpconfigport.h | 1 + py/objmodule.c | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bare-arm/mpconfigport.h b/bare-arm/mpconfigport.h index 3553d337fd..cd0ba8e3dd 100644 --- a/bare-arm/mpconfigport.h +++ b/bare-arm/mpconfigport.h @@ -17,6 +17,7 @@ #define MICROPY_PY_BUILTINS_SET (0) #define MICROPY_PY_BUILTINS_SLICE (0) #define MICROPY_PY_BUILTINS_PROPERTY (0) +#define MICROPY_PY___FILE__ (0) #define MICROPY_PY_GC (0) #define MICROPY_PY_ARRAY (0) #define MICROPY_PY_COLLECTIONS (0) diff --git a/py/objmodule.c b/py/objmodule.c index fa64736f7f..01349c3d28 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -40,7 +40,19 @@ STATIC mp_map_t mp_loaded_modules_map; // TODO: expose as sys.modules STATIC void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { mp_obj_module_t *self = self_in; - print(env, "", qstr_str(self->name)); + const char *name = qstr_str(self->name); + +#if MICROPY_PY___FILE__ + // If we store __file__ to imported modules then try to lookup this + // symbol to give more information about the module. + mp_map_elem_t *elem = mp_map_lookup(&self->globals->map, MP_OBJ_NEW_QSTR(MP_QSTR___file__), MP_MAP_LOOKUP); + if (elem != NULL) { + print(env, "", name, mp_obj_str_get_str(elem->value)); + return; + } +#endif + + print(env, "", name); } STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {