summaryrefslogtreecommitdiffstats
path: root/build/mach_initialize.py
diff options
context:
space:
mode:
Diffstat (limited to 'build/mach_initialize.py')
-rw-r--r--build/mach_initialize.py88
1 files changed, 2 insertions, 86 deletions
diff --git a/build/mach_initialize.py b/build/mach_initialize.py
index c0a4e515a8..ccd79d9a99 100644
--- a/build/mach_initialize.py
+++ b/build/mach_initialize.py
@@ -6,19 +6,9 @@ import math
import os
import shutil
import sys
+from importlib.abc import MetaPathFinder
from pathlib import Path
-if sys.version_info[0] < 3:
- import __builtin__ as builtins
-
- class MetaPathFinder(object):
- pass
-
-else:
- from importlib.abc import MetaPathFinder
-
-from types import ModuleType
-
STATE_DIR_FIRST_RUN = """
Mach and the build system store shared state in a common directory
on the filesystem. The following directory will be created:
@@ -490,76 +480,6 @@ def _create_state_dir():
# and caveats.
# Objdirs outside the source directory are ignored because in most cases, if
# a .pyc/.pyo file exists there, a .py file will be next to it anyways.
-class ImportHook(object):
- def __init__(self, original_import):
- self._original_import = original_import
- # Assume the source directory is the parent directory of the one
- # containing this file.
- self._source_dir = (
- os.path.normcase(
- os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
- )
- + os.sep
- )
- self._modules = set()
-
- def __call__(self, name, globals=None, locals=None, fromlist=None, level=-1):
- if sys.version_info[0] >= 3 and level < 0:
- level = 0
-
- # name might be a relative import. Instead of figuring out what that
- # resolves to, which is complex, just rely on the real import.
- # Since we don't know the full module name, we can't check sys.modules,
- # so we need to keep track of which modules we've already seen to avoid
- # to stat() them again when they are imported multiple times.
- module = self._original_import(name, globals, locals, fromlist, level)
-
- # Some tests replace modules in sys.modules with non-module instances.
- if not isinstance(module, ModuleType):
- return module
-
- resolved_name = module.__name__
- if resolved_name in self._modules:
- return module
- self._modules.add(resolved_name)
-
- # Builtin modules don't have a __file__ attribute.
- if not getattr(module, "__file__", None):
- return module
-
- # Note: module.__file__ is not always absolute.
- path = os.path.normcase(os.path.abspath(module.__file__))
- # Note: we could avoid normcase and abspath above for non pyc/pyo
- # files, but those are actually rare, so it doesn't really matter.
- if not path.endswith((".pyc", ".pyo")):
- return module
-
- # Ignore modules outside our source directory
- if not path.startswith(self._source_dir):
- return module
-
- # If there is no .py corresponding to the .pyc/.pyo module we're
- # loading, remove the .pyc/.pyo file, and reload the module.
- # Since we already loaded the .pyc/.pyo module, if it had side
- # effects, they will have happened already, and loading the module
- # with the same name, from another directory may have the same side
- # effects (or different ones). We assume it's not a problem for the
- # python modules under our source directory (either because it
- # doesn't happen or because it doesn't matter).
- if not os.path.exists(module.__file__[:-1]):
- if os.path.exists(module.__file__):
- os.remove(module.__file__)
- del sys.modules[module.__name__]
- module = self(name, globals, locals, fromlist, level)
-
- return module
-
-
-# Hook import such that .pyc/.pyo files without a corresponding .py file in
-# the source directory are essentially ignored. See further below for details
-# and caveats.
-# Objdirs outside the source directory are ignored because in most cases, if
-# a .pyc/.pyo file exists there, a .py file will be next to it anyways.
class FinderHook(MetaPathFinder):
def __init__(self, klass):
# Assume the source directory is the parent directory of the one
@@ -616,8 +536,4 @@ def hook(finder):
return finder
-# Install our hook. This can be deleted when the Python 3 migration is complete.
-if sys.version_info[0] < 3:
- builtins.__import__ = ImportHook(builtins.__import__)
-else:
- sys.meta_path = [hook(c) for c in sys.meta_path]
+sys.meta_path = [hook(c) for c in sys.meta_path]