py: Print imported module's location (__file__) if available.

This commit is contained in:
Damien George 2014-09-08 10:45:23 +01:00
parent 5c00757a5c
commit 377b80b624
2 changed files with 14 additions and 1 deletions

View File

@ -17,6 +17,7 @@
#define MICROPY_PY_BUILTINS_SET (0) #define MICROPY_PY_BUILTINS_SET (0)
#define MICROPY_PY_BUILTINS_SLICE (0) #define MICROPY_PY_BUILTINS_SLICE (0)
#define MICROPY_PY_BUILTINS_PROPERTY (0) #define MICROPY_PY_BUILTINS_PROPERTY (0)
#define MICROPY_PY___FILE__ (0)
#define MICROPY_PY_GC (0) #define MICROPY_PY_GC (0)
#define MICROPY_PY_ARRAY (0) #define MICROPY_PY_ARRAY (0)
#define MICROPY_PY_COLLECTIONS (0) #define MICROPY_PY_COLLECTIONS (0)

View File

@ -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) { 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; mp_obj_module_t *self = self_in;
print(env, "<module '%s' from '-unknown-file-'>", 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, "<module '%s' from '%s'>", name, mp_obj_str_get_str(elem->value));
return;
}
#endif
print(env, "<module '%s'>", name);
} }
STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { STATIC void module_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {