tiny-test-fw: only load module from the same file one time:

we should only load one module once.
if we load one module twice, python will regard the same object loaded in the first time and second time as different objects.
it will lead to strange errors like `isinstance(object, type_of_this_object)` return False
This commit is contained in:
He Yin Ling 2019-11-22 15:49:40 +08:00 committed by Angus Gratton
parent 69c0e6243e
commit d3e0301aee

View file

@ -38,11 +38,23 @@ def console_log(data, color="white", end="\n"):
sys.stdout.flush() sys.stdout.flush()
__LOADED_MODULES = dict()
# we should only load one module once.
# if we load one module twice,
# python will regard the same object loaded in the first time and second time as different objects.
# it will lead to strange errors like `isinstance(object, type_of_this_object)` return False
def load_source(name, path): def load_source(name, path):
try: try:
from importlib.machinery import SourceFileLoader return __LOADED_MODULES[name]
return SourceFileLoader(name, path).load_module() except KeyError:
except ImportError: try:
# importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3) from importlib.machinery import SourceFileLoader
import imp ret = SourceFileLoader(name, path).load_module()
return imp.load_source(name, path) except ImportError:
# importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
import imp
ret = imp.load_source(name, path)
__LOADED_MODULES[name] = ret
return ret