Skip to content

Commit e11b17c

Browse files
committed
Implement support for sys.path when loading modules.
sys.path is not initialized by rt_init(), that's left for platform-specific startup code. (For example, bare metal port may have some hardcoded defaults, and let user change sys.path directly; while port for OS with environment feature can take path from environment). If it's not explicitly initialized, modules will be imported only from a current directory.
1 parent 6e6b888 commit e11b17c

6 files changed

Lines changed: 58 additions & 5 deletions

File tree

py/builtinimport.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "map.h"
2020
#include "builtin.h"
2121

22+
mp_obj_t sys_path;
23+
2224
mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
2325
/*
2426
printf("import:\n");
@@ -37,10 +39,43 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
3739
}
3840

3941
// find the file to import
40-
mp_lexer_t *lex = mp_import_open_file(mod_name);
42+
uint mod_name_len;
43+
const byte* mod_name_p = qstr_data(mod_name, &mod_name_len);
44+
mp_lexer_t *lex = NULL;
45+
46+
uint path_num = 0;
47+
mp_obj_t *path_items;
48+
if (sys_path != MP_OBJ_NULL) {
49+
mp_obj_list_get(sys_path, &path_num, &path_items);
50+
}
51+
52+
if (path_num == 0) {
53+
CHECKBUF(fname, PATH_MAX);
54+
CHECKBUF_APPEND(fname, mod_name_p, mod_name_len);
55+
CHECKBUF_APPEND(fname, ".py", sizeof(".py") - 1);
56+
CHECKBUF_APPEND_0(fname);
57+
lex = mp_lexer_new_from_file(fname);
58+
} else {
59+
for (int i = 0; i < path_num; i++) {
60+
CHECKBUF(fname, PATH_MAX);
61+
uint p_len;
62+
const byte *p = mp_obj_str_get_data(path_items[i], &p_len);
63+
if (p_len > 0) {
64+
CHECKBUF_APPEND(fname, p, p_len);
65+
CHECKBUF_APPEND(fname, "/", 1);
66+
}
67+
CHECKBUF_APPEND(fname, mod_name_p, mod_name_len);
68+
CHECKBUF_APPEND(fname, ".py", sizeof(".py") - 1);
69+
CHECKBUF_APPEND_0(fname);
70+
lex = mp_lexer_new_from_file(fname);
71+
if (lex != NULL) {
72+
break;
73+
}
74+
}
75+
}
76+
4177
if (lex == NULL) {
42-
// TODO handle lexer error correctly
43-
return mp_const_none;
78+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ImportError, "ImportError: No module named '%s'", mod_name_p));
4479
}
4580
qstr source_name = mp_lexer_source_name(lex);
4681

py/lexerunix.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
1515
int fd = open(filename, O_RDONLY);
1616
if (fd < 0) {
17-
printf("cannot open file %s\n", filename);
1817
return NULL;
1918
}
2019
uint size = lseek(fd, 0, SEEK_END);

py/misc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ void vstr_add_strn(vstr_t *vstr, const char *str, int len);
9090
void vstr_cut_tail(vstr_t *vstr, int len);
9191
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
9292

93+
/** non-dynamic size-bounded variable buffer/string *************/
94+
95+
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
96+
#define CHECKBUF_APPEND(buf, src, src_len) \
97+
{ int l = MIN(src_len, buf##_len); \
98+
memcpy(buf##_p, src, l); \
99+
buf##_len -= l; \
100+
buf##_p += l; }
101+
#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
102+
#define CHECKBUF_LEN(buf) (buf##_p - buf)
103+
93104
#ifdef va_start
94105
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
95106
#endif

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Q(max)
7373
Q(min)
7474
Q(next)
7575
Q(ord)
76+
Q(path)
7677
Q(pow)
7778
Q(print)
7879
Q(range)

py/runtime.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,14 @@ void rt_init(void) {
168168

169169
#if MICROPY_CPYTHON_COMPAT
170170
// Precreate sys module, so "import sys" didn't throw exceptions.
171-
mp_obj_new_module(MP_QSTR_sys);
171+
mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
172+
// Avoid warning of unused var
173+
(void)m_sys;
172174
#endif
175+
// init sys.path
176+
// for efficiency, left to platform-specific startup code
177+
//sys_path = mp_obj_new_list(0, NULL);
178+
//rt_store_attr(m_sys, MP_QSTR_path, sys_path);
173179

174180
mp_module_micropython_init();
175181

py/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ void rt_locals_set(struct _mp_map_t *m);
4646
struct _mp_map_t *rt_globals_get(void);
4747
void rt_globals_set(struct _mp_map_t *m);
4848
struct _mp_map_t *rt_loaded_modules_get(void);
49+
extern mp_obj_t sys_path;

0 commit comments

Comments
 (0)