From 117cc1da6c3d47a7857471524bb4962bc9d885f0 Mon Sep 17 00:00:00 2001 From: Mukilan Thiyagarajan Date: Mon, 30 Oct 2023 13:23:20 +0530 Subject: [PATCH] mach: use `importlib` module instead of `imp` (#30645) * mach: use `importlib` module instead of `imp` `imp` module has been deprecated since python 3.4 and has been removed in 3.12. The recommended alternative is to use the `importlib` module that was introduced in python 3.1 This is required to fix the CI failures in macos builds since GitHub runner images for macos-13 now use python 3.12 Signed-off-by: Mukilan Thiyagarajan * mach: use `importlib` module instead of `imp` `imp` module has been deprecated since python 3.4 and has been removed in 3.12. The recommended alternative is to use the `importlib` module that was introduced in python 3.1 This is required to fix the CI failures in macos builds since GitHub runner images for macos-13 now use python 3.12 Signed-off-by: Mukilan Thiyagarajan --------- Signed-off-by: Mukilan Thiyagarajan --- python/mach/mach/main.py | 18 +++++++----------- python/mach_bootstrap.py | 6 +++--- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/python/mach/mach/main.py b/python/mach/mach/main.py index 83f01128513..9b459aa53c0 100644 --- a/python/mach/mach/main.py +++ b/python/mach/mach/main.py @@ -10,7 +10,7 @@ from __future__ import absolute_import, print_function, unicode_literals import argparse import codecs import errno -import imp +import importlib import logging import os import sys @@ -253,17 +253,13 @@ To see more help for a specific command, run: module name specified. If no name is specified, a random one will be chosen. """ - if module_name is None: - # Ensure parent module is present otherwise we'll (likely) get - # an error due to unknown parent. - if 'mach.commands' not in sys.modules: - mod = imp.new_module('mach.commands') - sys.modules['mach.commands'] = mod - - module_name = 'mach.commands.%s' % uuid.uuid4().hex - try: - imp.load_source(module_name, path) + if module_name is None: + module_name = 'mach.commands.%s' % uuid.uuid4().hex + 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) except IOError as e: if e.errno != errno.ENOENT: raise diff --git a/python/mach_bootstrap.py b/python/mach_bootstrap.py index 484cdb8a710..aed66995f2e 100644 --- a/python/mach_bootstrap.py +++ b/python/mach_bootstrap.py @@ -140,10 +140,10 @@ def _activate_virtualenv(topdir): activate_path = os.path.join(virtualenv_path, script_dir, "activate_this.py") need_pip_upgrade = False if not (os.path.exists(virtualenv_path) and os.path.exists(activate_path)): - import imp + import importlib try: - imp.find_module('virtualenv') - except ImportError: + importlib.import_module('virtualenv') + except ModuleNotFoundError: sys.exit("Python virtualenv is not installed. Please install it prior to running mach.") _process_exec([python, "-m", "virtualenv", "-p", python, "--system-site-packages", virtualenv_path])