From 3c99de352c72df58354bec863e9f4c79d7a83ab0 Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Fri, 22 Nov 2019 15:49:40 +0800 Subject: [PATCH] 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 --- tools/tiny-test-fw/Utility/__init__.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tools/tiny-test-fw/Utility/__init__.py b/tools/tiny-test-fw/Utility/__init__.py index 2a0759a7bc..fbd2989bb0 100644 --- a/tools/tiny-test-fw/Utility/__init__.py +++ b/tools/tiny-test-fw/Utility/__init__.py @@ -38,11 +38,23 @@ def console_log(data, color="white", end="\n"): 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): try: - from importlib.machinery import SourceFileLoader - return SourceFileLoader(name, path).load_module() - except ImportError: - # importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3) - import imp - return imp.load_source(name, path) + return __LOADED_MODULES[name] + except KeyError: + try: + from importlib.machinery import SourceFileLoader + ret = SourceFileLoader(name, path).load_module() + 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