summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-09-08 10:45:23 +0100
committerDamien George <damien.p.george@gmail.com>2014-09-08 10:45:23 +0100
commit377b80b62450bde6fc0c06f50254f3c751127247 (patch)
treed3c2436a76f8589ff5e2f5448a78aa8a98d85d3a
parent5c00757a5cac767ee30fa25bbf720e85477f899d (diff)
downloadmicropython-377b80b62450bde6fc0c06f50254f3c751127247.tar.gz
micropython-377b80b62450bde6fc0c06f50254f3c751127247.zip
py: Print imported module's location (__file__) if available.
-rw-r--r--bare-arm/mpconfigport.h1
-rw-r--r--py/objmodule.c14
2 files changed, 14 insertions, 1 deletions
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, "<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) {