diff options
author | Damien George <damien.p.george@gmail.com> | 2014-02-05 21:27:05 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-02-05 21:27:05 +0000 |
commit | cdcb4906d41f42789f6610047e8ae6aa79a61e47 (patch) | |
tree | 099453009838e4ba11de4a32cda89f076dc20256 /py/builtinimport.c | |
parent | b8ec17c2d16407dd186d1f8f76c1e7c420bb7729 (diff) | |
parent | 911089606376e60bd9451a85eb9558a23cde9039 (diff) | |
download | micropython-cdcb4906d41f42789f6610047e8ae6aa79a61e47.tar.gz micropython-cdcb4906d41f42789f6610047e8ae6aa79a61e47.zip |
Merge pull request #262 from pfalcon/sys-path
Implement sys.path support
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r-- | py/builtinimport.c | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index a5deed3924..92d9ada897 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -19,6 +19,8 @@ #include "map.h" #include "builtin.h" +mp_obj_t sys_path; + mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { /* printf("import:\n"); @@ -37,10 +39,43 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { } // find the file to import - mp_lexer_t *lex = mp_import_open_file(mod_name); + uint mod_name_len; + const byte* mod_name_p = qstr_data(mod_name, &mod_name_len); + mp_lexer_t *lex = NULL; + + uint path_num = 0; + mp_obj_t *path_items; + if (sys_path != MP_OBJ_NULL) { + mp_obj_list_get(sys_path, &path_num, &path_items); + } + + if (path_num == 0) { + CHECKBUF(fname, PATH_MAX); + CHECKBUF_APPEND(fname, mod_name_p, mod_name_len); + CHECKBUF_APPEND(fname, ".py", sizeof(".py") - 1); + CHECKBUF_APPEND_0(fname); + lex = mp_lexer_new_from_file(fname); + } else { + for (int i = 0; i < path_num; i++) { + CHECKBUF(fname, PATH_MAX); + uint p_len; + const byte *p = mp_obj_str_get_data(path_items[i], &p_len); + if (p_len > 0) { + CHECKBUF_APPEND(fname, p, p_len); + CHECKBUF_APPEND(fname, "/", 1); + } + CHECKBUF_APPEND(fname, mod_name_p, mod_name_len); + CHECKBUF_APPEND(fname, ".py", sizeof(".py") - 1); + CHECKBUF_APPEND_0(fname); + lex = mp_lexer_new_from_file(fname); + if (lex != NULL) { + break; + } + } + } + if (lex == NULL) { - // TODO handle lexer error correctly - return mp_const_none; + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ImportError, "ImportError: No module named '%s'", mod_name_p)); } qstr source_name = mp_lexer_source_name(lex); |