From aa0484d76b76112a1d395fb81897b31cb7113cdb Mon Sep 17 00:00:00 2001 From: David Heidelberg Date: Tue, 11 May 2021 11:54:36 +0200 Subject: [PATCH] mach: cleanup after dropping Python 2 support Signed-off-by: David Heidelberg --- mach | 75 +--------------------------------------- python/mach_bootstrap.py | 6 ++-- 2 files changed, 4 insertions(+), 77 deletions(-) diff --git a/mach b/mach index c6efe1a9411..c521eaf9231 100755 --- a/mach +++ b/mach @@ -11,15 +11,11 @@ from __future__ import print_function, unicode_literals - import os import sys -# Check for the current python version as some users (especially on archlinux) -# may not have python 2 installed and their /bin/python binary symlinked to -# python 3. if sys.version_info < (3, 5): - print("mach does not support python 3 (< 3.5), please install python 3 (>= 3.5)") + print("mach does not support python < 3.5, please install python 3 >= 3.5") sys.exit(1) @@ -36,75 +32,6 @@ def main(args): if __name__ == '__main__': sys.dont_write_bytecode = True - if sys.platform == 'win32' and sys.version_info < (3, 4): - # This is a complete hack to work around the fact that Windows - # multiprocessing needs to import the original module (ie: this - # file), but only works if it has a .py extension. - # - # We do this by a sort of two-level function interposing. The first - # level interposes forking.get_command_line() with our version defined - # in my_get_command_line(). Our version of get_command_line will - # replace the command string with the contents of the fork_interpose() - # function to be used in the subprocess. - # - # The subprocess then gets an interposed imp.find_module(), which we - # hack up to find 'mach' without the .py extension, since we already - # know where it is (it's us!). If we're not looking for 'mach', then - # the original find_module will suffice. - # - # See also: http://bugs.python.org/issue19946 - # And: https://bugzilla.mozilla.org/show_bug.cgi?id=914563 - # XXX In Python 3.4 the multiprocessing module was re-written and the - # below code is no longer valid. The Python issue19946 also claims to - # be fixed in this version. It's not clear whether this hack is still - # needed in 3.4+ or not, but at least some basic mach commands appear - # to work without it. So skip it in 3.4+ until we determine it's still - # needed. - import inspect - from multiprocessing import forking - global orig_command_line - - def fork_interpose(): - import imp - import os - import sys - orig_find_module = imp.find_module - def my_find_module(name, dirs): - if name == 'mach': - path = os.path.join(dirs[0], 'mach') - f = open(path) - return (f, path, ('', 'r', imp.PY_SOURCE)) - return orig_find_module(name, dirs) - - # Don't allow writing bytecode file for mach module. - orig_load_module = imp.load_module - def my_load_module(name, file, path, description): - # multiprocess.forking invokes imp.load_module manually and - # hard-codes the name __parents_main__ as the module name. - if name == '__parents_main__': - old_bytecode = sys.dont_write_bytecode - sys.dont_write_bytecode = True - try: - return orig_load_module(name, file, path, description) - finally: - sys.dont_write_bytecode = old_bytecode - - return orig_load_module(name, file, path, description) - - imp.find_module = my_find_module - imp.load_module = my_load_module - from multiprocessing.forking import main; main() - - def my_get_command_line(): - fork_code, lineno = inspect.getsourcelines(fork_interpose) - # Remove the first line (for 'def fork_interpose():') and the three - # levels of indentation (12 spaces). - fork_string = ''.join(x[12:] for x in fork_code[1:]) - cmdline = orig_command_line() - cmdline[2] = fork_string - return cmdline - orig_command_line = forking.get_command_line - forking.get_command_line = my_get_command_line if os.path.exists('/etc/NIXOS') and not 'IN_NIX_SHELL' in os.environ: # we're on a nixOS system, need to run mach in nix-shell import subprocess diff --git a/python/mach_bootstrap.py b/python/mach_bootstrap.py index 0c5eb005694..88f0fdbe8a0 100644 --- a/python/mach_bootstrap.py +++ b/python/mach_bootstrap.py @@ -279,10 +279,10 @@ def bootstrap(topdir): print('Current path:', topdir) sys.exit(1) - # Ensure we are running Python 2.7+ or Python 3.5+. We put this check here so we generate a + # Ensure we are running Python 3.5+. We put this check here so we generate a # user-friendly error message rather than a cryptic stack trace on module import. - if sys.version_info < (2, 7) or (sys.version_info >= (3, 0) and sys.version_info < (3, 5)): - print('Python2 (>=2.7) or Python3 (>=3.5) is required to run mach.') + if sys.version_info < (3, 5): + print('Python3 (>=3.5) is required to run mach.') print('You are running Python', platform.python_version()) sys.exit(1)