diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-20 00:03:34 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-20 00:38:39 +0200 |
commit | d720ab5236015124a13c09175ed674e565414faa (patch) | |
tree | 201a5cca563eaad2e76651add86b8dc4de923c1a /py/builtinimport.c | |
parent | f438b936e0edcbc3eab9af6cc3513db1f01ab17e (diff) |
Implement modules as singletons Python semantics.
In Python, importing module several times returns same underlying module
object. This also fixes import statement handling for builtin modules.
There're still issues:
1. CPython exposes set of loaded modules as sys.modules, we may want to
do that either.
2. Builtin modules are implicitly imported, which is not really correct.
We should separate registering a (builtin) module and importing a module.
CPython keeps builtin module names in sys.builtin_module_names .
Diffstat (limited to 'py/builtinimport.c')
-rw-r--r-- | py/builtinimport.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c index e3c630a0a..92d5d5ac9 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -28,8 +28,13 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) { } */ - // find the file to import qstr mod_name = mp_obj_get_qstr(args[0]); + mp_obj_t loaded = mp_obj_module_get(mod_name); + if (loaded != MP_OBJ_NULL) { + return loaded; + } + + // find the file to import mp_lexer_t *lex = mp_import_open_file(mod_name); if (lex == NULL) { // TODO handle lexer error correctly |