2019-07-08 21:48:19 +02:00
|
|
|
import sys
|
|
|
|
|
import builtins
|
|
|
|
|
import types
|
|
|
|
|
|
2019-08-08 00:27:01 +02:00
|
|
|
# keep the builtin function accessible in this module and via imp.__import__
|
|
|
|
|
__import__ = __import__
|
|
|
|
|
|
2019-07-08 21:48:19 +02:00
|
|
|
# Deprecated since version 3.4: Use types.ModuleType instead.
|
|
|
|
|
# but micropython aims toward full 3.4
|
|
|
|
|
|
|
|
|
|
# Return a new empty module object called name. This object is not inserted in sys.modules.
|
|
|
|
|
def new_module(name):
|
|
|
|
|
return types.ModuleType(name)
|
|
|
|
|
|
|
|
|
|
|
2019-08-08 00:27:01 +02:00
|
|
|
# not spaghetti
|
|
|
|
|
def importer(name,*argv,**kw):
|
2019-07-08 21:48:19 +02:00
|
|
|
global __import__
|
|
|
|
|
try:
|
|
|
|
|
return __import__(name,*argv)
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
file = ':{0}.py'.format(name)
|
2019-08-08 00:27:01 +02:00
|
|
|
print("INFO: getting online version of",file)
|
2019-07-08 21:48:19 +02:00
|
|
|
# todo open the file via open() or raise importerror
|
|
|
|
|
try:
|
|
|
|
|
code = open(file,'r').read()
|
|
|
|
|
except:
|
|
|
|
|
raise ImportError('module not found')
|
|
|
|
|
|
|
|
|
|
#build a empty module
|
|
|
|
|
mod = types.ModuleType(name)
|
|
|
|
|
|
|
|
|
|
mod.__file__ = file
|
|
|
|
|
|
|
|
|
|
# compile module from cached file
|
|
|
|
|
try:
|
|
|
|
|
code = compile( code, file, 'exec')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
sys.print_exception(e)
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
# execute it in its own empty namespace.
|
2019-08-08 00:27:01 +02:00
|
|
|
try:
|
|
|
|
|
ns = vars(mod)
|
|
|
|
|
except:
|
|
|
|
|
print("WARNING: this python implementation lacks vars()")
|
|
|
|
|
ns = mod.__dict__
|
|
|
|
|
|
2019-07-08 21:48:19 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
exec( code, ns, ns)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
sys.print_exception(e)
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
# though micropython would normally insert module before executing the whole body
|
|
|
|
|
# do it after.
|
|
|
|
|
sys.modules[name] = mod
|
|
|
|
|
return mod
|
|
|
|
|
|
|
|
|
|
# install hook
|
|
|
|
|
builtins.__import__ = importer
|
|
|
|
|
print("__import__ is now", importer)
|