From 21a0bd5f59b8004fc0ab7a5a4de118dadc1e7bc3 Mon Sep 17 00:00:00 2001 From: roytam1 Date: Tue, 19 May 2026 21:44:33 +0800 Subject: [PATCH] ported from UXP: Load mochitest modules without imp (ee00ac98) --- testing/mochitest/mach_commands.py | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/testing/mochitest/mach_commands.py b/testing/mochitest/mach_commands.py index 07b9fc1b4..9855f47bd 100644 --- a/testing/mochitest/mach_commands.py +++ b/testing/mochitest/mach_commands.py @@ -26,6 +26,24 @@ from mach.decorators import ( here = os.path.abspath(os.path.dirname(__file__)) +def load_source_module(module_name, path): + if module_name in sys.modules: + return sys.modules[module_name] + + try: + import importlib.util + spec = importlib.util.spec_from_file_location(module_name, path) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + return module + except ImportError: + import imp + with open(path, 'r') as fh: + return imp.load_module(module_name, fh, path, + ('.py', 'r', imp.PY_SOURCE)) + + ENG_BUILD_REQUIRED = ''' The mochitest command requires an engineering build. It may be the case that VARIANT=user or PRODUCTION=1 were set. Try re-building with VARIANT=eng: @@ -120,11 +138,8 @@ class MochitestRunner(MozbuildObject): """ # runtests.py is ambiguous, so we load the file/module manually. if 'mochitest' not in sys.modules: - import imp path = os.path.join(self.mochitest_dir, 'runtests.py') - with open(path, 'r') as fh: - imp.load_module('mochitest', fh, path, - ('.py', 'r', imp.PY_SOURCE)) + load_source_module('mochitest', path) import mochitest @@ -164,11 +179,8 @@ class MochitestRunner(MozbuildObject): if host_ret != 0: return host_ret - import imp path = os.path.join(self.mochitest_dir, 'runtestsremote.py') - with open(path, 'r') as fh: - imp.load_module('runtestsremote', fh, path, - ('.py', 'r', imp.PY_SOURCE)) + load_source_module('runtestsremote', path) import runtestsremote options = Namespace(**kwargs) @@ -218,14 +230,8 @@ def setup_argument_parser(): with warnings.catch_warnings(): warnings.simplefilter('ignore') - import imp path = os.path.join(build_obj.topobjdir, mochitest_dir, 'runtests.py') - if not os.path.exists(path): - path = os.path.join(here, "runtests.py") - - with open(path, 'r') as fh: - imp.load_module('mochitest', fh, path, - ('.py', 'r', imp.PY_SOURCE)) + load_source_module('mochitest', path) from mochitest_options import MochitestArgumentParser