diff options
| author | shadchin <[email protected]> | 2022-02-10 16:44:39 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:39 +0300 |
| commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
| tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py | |
| parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py')
| -rw-r--r-- | contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py | 418 |
1 files changed, 209 insertions, 209 deletions
diff --git a/contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py b/contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py index 0a287848379..fe31f437dac 100644 --- a/contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py +++ b/contrib/tools/python3/src/Lib/importlib/_bootstrap_external.py @@ -19,34 +19,34 @@ work. One should use importlib as the public-facing version of this module. # reference any injected objects! This includes not only global code but also # anything specified at the class level. -# Import builtin modules -import _imp -import _io -import sys -import _warnings -import marshal - - -_MS_WINDOWS = (sys.platform == 'win32') -if _MS_WINDOWS: - import nt as _os - import winreg -else: - import posix as _os - - -if _MS_WINDOWS: - path_separators = ['\\', '/'] -else: - path_separators = ['/'] -# Assumption made in _path_join() -assert all(len(sep) == 1 for sep in path_separators) -path_sep = path_separators[0] -path_sep_tuple = tuple(path_separators) -path_separators = ''.join(path_separators) -_pathseps_with_colon = {f':{s}' for s in path_separators} - - +# Import builtin modules +import _imp +import _io +import sys +import _warnings +import marshal + + +_MS_WINDOWS = (sys.platform == 'win32') +if _MS_WINDOWS: + import nt as _os + import winreg +else: + import posix as _os + + +if _MS_WINDOWS: + path_separators = ['\\', '/'] +else: + path_separators = ['/'] +# Assumption made in _path_join() +assert all(len(sep) == 1 for sep in path_separators) +path_sep = path_separators[0] +path_sep_tuple = tuple(path_separators) +path_separators = ''.join(path_separators) +_pathseps_with_colon = {f':{s}' for s in path_separators} + + # Bootstrap-related code ###################################################### _CASE_INSENSITIVE_PLATFORMS_STR_KEY = 'win', _CASE_INSENSITIVE_PLATFORMS_BYTES_KEY = 'cygwin', 'darwin' @@ -62,8 +62,8 @@ def _make_relax_case(): key = b'PYTHONCASEOK' def _relax_case(): - """True if filenames must be checked case-insensitively and ignore environment flags are not set.""" - return not sys.flags.ignore_environment and key in _os.environ + """True if filenames must be checked case-insensitively and ignore environment flags are not set.""" + return not sys.flags.ignore_environment and key in _os.environ else: def _relax_case(): """True if filenames must be checked case-insensitively.""" @@ -71,65 +71,65 @@ def _make_relax_case(): return _relax_case -def _pack_uint32(x): +def _pack_uint32(x): """Convert a 32-bit integer to little-endian.""" return (int(x) & 0xFFFFFFFF).to_bytes(4, 'little') -def _unpack_uint32(data): +def _unpack_uint32(data): """Convert 4 bytes in little-endian to an integer.""" - assert len(data) == 4 - return int.from_bytes(data, 'little') + assert len(data) == 4 + return int.from_bytes(data, 'little') + +def _unpack_uint16(data): + """Convert 2 bytes in little-endian to an integer.""" + assert len(data) == 2 + return int.from_bytes(data, 'little') -def _unpack_uint16(data): - """Convert 2 bytes in little-endian to an integer.""" - assert len(data) == 2 - return int.from_bytes(data, 'little') - -if _MS_WINDOWS: - def _path_join(*path_parts): - """Replacement for os.path.join().""" - if not path_parts: - return "" - if len(path_parts) == 1: - return path_parts[0] - root = "" - path = [] - for new_root, tail in map(_os._path_splitroot, path_parts): - if new_root.startswith(path_sep_tuple) or new_root.endswith(path_sep_tuple): - root = new_root.rstrip(path_separators) or root - path = [path_sep + tail] - elif new_root.endswith(':'): - if root.casefold() != new_root.casefold(): - # Drive relative paths have to be resolved by the OS, so we reset the - # tail but do not add a path_sep prefix. - root = new_root - path = [tail] - else: - path.append(tail) - else: - root = new_root or root - path.append(tail) - path = [p.rstrip(path_separators) for p in path if p] - if len(path) == 1 and not path[0]: - # Avoid losing the root's trailing separator when joining with nothing - return root + path_sep - return root + path_sep.join(path) +if _MS_WINDOWS: + def _path_join(*path_parts): + """Replacement for os.path.join().""" + if not path_parts: + return "" + if len(path_parts) == 1: + return path_parts[0] + root = "" + path = [] + for new_root, tail in map(_os._path_splitroot, path_parts): + if new_root.startswith(path_sep_tuple) or new_root.endswith(path_sep_tuple): + root = new_root.rstrip(path_separators) or root + path = [path_sep + tail] + elif new_root.endswith(':'): + if root.casefold() != new_root.casefold(): + # Drive relative paths have to be resolved by the OS, so we reset the + # tail but do not add a path_sep prefix. + root = new_root + path = [tail] + else: + path.append(tail) + else: + root = new_root or root + path.append(tail) + path = [p.rstrip(path_separators) for p in path if p] + if len(path) == 1 and not path[0]: + # Avoid losing the root's trailing separator when joining with nothing + return root + path_sep + return root + path_sep.join(path) + +else: + def _path_join(*path_parts): + """Replacement for os.path.join().""" + return path_sep.join([part.rstrip(path_separators) + for part in path_parts if part]) -else: - def _path_join(*path_parts): - """Replacement for os.path.join().""" - return path_sep.join([part.rstrip(path_separators) - for part in path_parts if part]) - def _path_split(path): """Replacement for os.path.split().""" - i = max(path.rfind(p) for p in path_separators) - if i < 0: - return '', path - return path[:i], path[i + 1:] + i = max(path.rfind(p) for p in path_separators) + if i < 0: + return '', path + return path[:i], path[i + 1:] def _path_stat(path): @@ -163,20 +163,20 @@ def _path_isdir(path): return _path_is_mode_type(path, 0o040000) -if _MS_WINDOWS: - def _path_isabs(path): - """Replacement for os.path.isabs.""" - if not path: - return False - root = _os._path_splitroot(path)[0].replace('/', '\\') - return len(root) > 1 and (root.startswith('\\\\') or root.endswith('\\')) - -else: - def _path_isabs(path): - """Replacement for os.path.isabs.""" - return path.startswith(path_separators) - - +if _MS_WINDOWS: + def _path_isabs(path): + """Replacement for os.path.isabs.""" + if not path: + return False + root = _os._path_splitroot(path)[0].replace('/', '\\') + return len(root) > 1 and (root.startswith('\\\\') or root.endswith('\\')) + +else: + def _path_isabs(path): + """Replacement for os.path.isabs.""" + return path.startswith(path_separators) + + def _write_atomic(path, data, mode=0o666): """Best-effort function to write data to a path atomically. Be prepared to handle a FileExistsError if concurrent writing of the @@ -321,23 +321,23 @@ _code_type = type(_write_atomic.__code__) # Python 3.7a2 3391 (update GET_AITER #31709) # Python 3.7a4 3392 (PEP 552: Deterministic pycs #31650) # Python 3.7b1 3393 (remove STORE_ANNOTATION opcode #32550) -# Python 3.7b5 3394 (restored docstring as the first stmt in the body; +# Python 3.7b5 3394 (restored docstring as the first stmt in the body; # this might affected the first line number #32911) -# Python 3.8a1 3400 (move frame block handling to compiler #17611) -# Python 3.8a1 3401 (add END_ASYNC_FOR #33041) -# Python 3.8a1 3410 (PEP570 Python Positional-Only Parameters #36540) -# Python 3.8b2 3411 (Reverse evaluation order of key: value in dict -# comprehensions #35224) -# Python 3.8b2 3412 (Swap the position of positional args and positional -# only args in ast.arguments #37593) -# Python 3.8b4 3413 (Fix "break" and "continue" in "finally" #37830) -# Python 3.9a0 3420 (add LOAD_ASSERTION_ERROR #34880) -# Python 3.9a0 3421 (simplified bytecode for with blocks #32949) -# Python 3.9a0 3422 (remove BEGIN_FINALLY, END_FINALLY, CALL_FINALLY, POP_FINALLY bytecodes #33387) -# Python 3.9a2 3423 (add IS_OP, CONTAINS_OP and JUMP_IF_NOT_EXC_MATCH bytecodes #39156) -# Python 3.9a2 3424 (simplify bytecodes for *value unpacking) -# Python 3.9a2 3425 (simplify bytecodes for **value unpacking) - +# Python 3.8a1 3400 (move frame block handling to compiler #17611) +# Python 3.8a1 3401 (add END_ASYNC_FOR #33041) +# Python 3.8a1 3410 (PEP570 Python Positional-Only Parameters #36540) +# Python 3.8b2 3411 (Reverse evaluation order of key: value in dict +# comprehensions #35224) +# Python 3.8b2 3412 (Swap the position of positional args and positional +# only args in ast.arguments #37593) +# Python 3.8b4 3413 (Fix "break" and "continue" in "finally" #37830) +# Python 3.9a0 3420 (add LOAD_ASSERTION_ERROR #34880) +# Python 3.9a0 3421 (simplified bytecode for with blocks #32949) +# Python 3.9a0 3422 (remove BEGIN_FINALLY, END_FINALLY, CALL_FINALLY, POP_FINALLY bytecodes #33387) +# Python 3.9a2 3423 (add IS_OP, CONTAINS_OP and JUMP_IF_NOT_EXC_MATCH bytecodes #39156) +# Python 3.9a2 3424 (simplify bytecodes for *value unpacking) +# Python 3.9a2 3425 (simplify bytecodes for **value unpacking) + # # MAGIC must change whenever the bytecode emitted by the compiler may no # longer be understood by older implementations of the eval loop (usually @@ -346,7 +346,7 @@ _code_type = type(_write_atomic.__code__) # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # in PC/launcher.c must also be updated. -MAGIC_NUMBER = (3425).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3425).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _PYCACHE = '__pycache__' @@ -400,35 +400,35 @@ def cache_from_source(path, debug_override=None, *, optimization=None): if not optimization.isalnum(): raise ValueError('{!r} is not alphanumeric'.format(optimization)) almost_filename = '{}.{}{}'.format(almost_filename, _OPT, optimization) - filename = almost_filename + BYTECODE_SUFFIXES[0] - if sys.pycache_prefix is not None: - # We need an absolute path to the py file to avoid the possibility of - # collisions within sys.pycache_prefix, if someone has two different - # `foo/bar.py` on their system and they import both of them using the - # same sys.pycache_prefix. Let's say sys.pycache_prefix is - # `C:\Bytecode`; the idea here is that if we get `Foo\Bar`, we first - # make it absolute (`C:\Somewhere\Foo\Bar`), then make it root-relative - # (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in an - # unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`. - if not _path_isabs(head): - head = _path_join(_os.getcwd(), head) + filename = almost_filename + BYTECODE_SUFFIXES[0] + if sys.pycache_prefix is not None: + # We need an absolute path to the py file to avoid the possibility of + # collisions within sys.pycache_prefix, if someone has two different + # `foo/bar.py` on their system and they import both of them using the + # same sys.pycache_prefix. Let's say sys.pycache_prefix is + # `C:\Bytecode`; the idea here is that if we get `Foo\Bar`, we first + # make it absolute (`C:\Somewhere\Foo\Bar`), then make it root-relative + # (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in an + # unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`. + if not _path_isabs(head): + head = _path_join(_os.getcwd(), head) + + # Strip initial drive from a Windows path. We know we have an absolute + # path here, so the second part of the check rules out a POSIX path that + # happens to contain a colon at the second character. + if head[1] == ':' and head[0] not in path_separators: + head = head[2:] + + # Strip initial path separator from `head` to complete the conversion + # back to a root-relative path before joining. + return _path_join( + sys.pycache_prefix, + head.lstrip(path_separators), + filename, + ) + return _path_join(head, _PYCACHE, filename) - # Strip initial drive from a Windows path. We know we have an absolute - # path here, so the second part of the check rules out a POSIX path that - # happens to contain a colon at the second character. - if head[1] == ':' and head[0] not in path_separators: - head = head[2:] - # Strip initial path separator from `head` to complete the conversion - # back to a root-relative path before joining. - return _path_join( - sys.pycache_prefix, - head.lstrip(path_separators), - filename, - ) - return _path_join(head, _PYCACHE, filename) - - def source_from_cache(path): """Given the path to a .pyc. file, return the path to its .py file. @@ -442,29 +442,29 @@ def source_from_cache(path): raise NotImplementedError('sys.implementation.cache_tag is None') path = _os.fspath(path) head, pycache_filename = _path_split(path) - found_in_pycache_prefix = False - if sys.pycache_prefix is not None: - stripped_path = sys.pycache_prefix.rstrip(path_separators) - if head.startswith(stripped_path + path_sep): - head = head[len(stripped_path):] - found_in_pycache_prefix = True - if not found_in_pycache_prefix: - head, pycache = _path_split(head) - if pycache != _PYCACHE: - raise ValueError(f'{_PYCACHE} not bottom-level directory in ' - f'{path!r}') + found_in_pycache_prefix = False + if sys.pycache_prefix is not None: + stripped_path = sys.pycache_prefix.rstrip(path_separators) + if head.startswith(stripped_path + path_sep): + head = head[len(stripped_path):] + found_in_pycache_prefix = True + if not found_in_pycache_prefix: + head, pycache = _path_split(head) + if pycache != _PYCACHE: + raise ValueError(f'{_PYCACHE} not bottom-level directory in ' + f'{path!r}') dot_count = pycache_filename.count('.') if dot_count not in {2, 3}: - raise ValueError(f'expected only 2 or 3 dots in {pycache_filename!r}') + raise ValueError(f'expected only 2 or 3 dots in {pycache_filename!r}') elif dot_count == 3: optimization = pycache_filename.rsplit('.', 2)[-2] if not optimization.startswith(_OPT): raise ValueError("optimization portion of filename does not start " - f"with {_OPT!r}") + f"with {_OPT!r}") opt_level = optimization[len(_OPT):] if not opt_level.isalnum(): - raise ValueError(f"optimization level {optimization!r} is not an " - "alphanumeric value") + raise ValueError(f"optimization level {optimization!r} is not an " + "alphanumeric value") base_filename = pycache_filename.partition('.')[0] return _path_join(head, base_filename + SOURCE_SUFFIXES[0]) @@ -582,7 +582,7 @@ def _classify_pyc(data, name, exc_details): message = f'reached EOF while reading pyc header of {name!r}' _bootstrap._verbose_message('{}', message) raise EOFError(message) - flags = _unpack_uint32(data[4:8]) + flags = _unpack_uint32(data[4:8]) # Only the first two flags are defined. if flags & ~0b11: message = f'invalid flags {flags!r} in {name!r}' @@ -609,12 +609,12 @@ def _validate_timestamp_pyc(data, source_mtime, source_size, name, An ImportError is raised if the bytecode is stale. """ - if _unpack_uint32(data[8:12]) != (source_mtime & 0xFFFFFFFF): + if _unpack_uint32(data[8:12]) != (source_mtime & 0xFFFFFFFF): message = f'bytecode is stale for {name!r}' _bootstrap._verbose_message('{}', message) raise ImportError(message, **exc_details) if (source_size is not None and - _unpack_uint32(data[12:16]) != (source_size & 0xFFFFFFFF)): + _unpack_uint32(data[12:16]) != (source_size & 0xFFFFFFFF)): raise ImportError(f'bytecode is stale for {name!r}', **exc_details) @@ -658,9 +658,9 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None): def _code_to_timestamp_pyc(code, mtime=0, source_size=0): "Produce the data for a timestamp-based pyc." data = bytearray(MAGIC_NUMBER) - data.extend(_pack_uint32(0)) - data.extend(_pack_uint32(mtime)) - data.extend(_pack_uint32(source_size)) + data.extend(_pack_uint32(0)) + data.extend(_pack_uint32(mtime)) + data.extend(_pack_uint32(source_size)) data.extend(marshal.dumps(code)) return data @@ -669,7 +669,7 @@ def _code_to_hash_pyc(code, source_hash, checked=True): "Produce the data for a hash-based pyc." data = bytearray(MAGIC_NUMBER) flags = 0b1 | checked << 1 - data.extend(_pack_uint32(flags)) + data.extend(_pack_uint32(flags)) assert len(source_hash) == 8 data.extend(source_hash) data.extend(marshal.dumps(code)) @@ -776,9 +776,9 @@ class WindowsRegistryFinder: @classmethod def _open_registry(cls, key): try: - return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) + return winreg.OpenKey(winreg.HKEY_CURRENT_USER, key) except OSError: - return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key) + return winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key) @classmethod def _search_registry(cls, fullname): @@ -790,7 +790,7 @@ class WindowsRegistryFinder: sys_version='%d.%d' % sys.version_info[:2]) try: with cls._open_registry(key) as hkey: - filepath = winreg.QueryValue(hkey, '') + filepath = winreg.QueryValue(hkey, '') except OSError: return None return filepath @@ -858,16 +858,16 @@ class SourceLoader(_LoaderBasics): def path_mtime(self, path): """Optional method that returns the modification time (an int) for the - specified path (a str). + specified path (a str). Raises OSError when the path cannot be handled. """ raise OSError def path_stats(self, path): - """Optional method returning a metadata dict for the specified - path (a str). - + """Optional method returning a metadata dict for the specified + path (a str). + Possible keys: - 'mtime' (mandatory) is the numeric timestamp of last source code modification; @@ -1035,12 +1035,12 @@ class FileLoader: def get_data(self, path): """Return the data from path as raw bytes.""" - if isinstance(self, (SourceLoader, ExtensionFileLoader)): - with _io.open_code(str(path)) as file: - return file.read() - else: - with _io.FileIO(path, 'r') as file: - return file.read() + if isinstance(self, (SourceLoader, ExtensionFileLoader)): + with _io.open_code(str(path)) as file: + return file.read() + else: + with _io.FileIO(path, 'r') as file: + return file.read() # ResourceReader ABC API. @@ -1154,11 +1154,11 @@ class ExtensionFileLoader(FileLoader, _LoaderBasics): def __init__(self, name, path): self.name = name - if not _path_isabs(path): - try: - path = _path_join(_os.getcwd(), path) - except OSError: - pass + if not _path_isabs(path): + try: + path = _path_join(_os.getcwd(), path) + except OSError: + pass self.path = path def __eq__(self, other): @@ -1245,9 +1245,9 @@ class _NamespacePath: def __iter__(self): return iter(self._recalculate()) - def __getitem__(self, index): - return self._recalculate()[index] - + def __getitem__(self, index): + return self._recalculate()[index] + def __setitem__(self, index, path): self._path[index] = path @@ -1440,20 +1440,20 @@ class PathFinder: return None return spec.loader - @classmethod - def find_distributions(cls, *args, **kwargs): - """ - Find distributions. + @classmethod + def find_distributions(cls, *args, **kwargs): + """ + Find distributions. + + Return an iterable of all Distribution instances capable of + loading the metadata for packages matching ``context.name`` + (or all names if ``None`` indicated) along the paths in the list + of directories ``context.path``. + """ + from importlib.metadata import MetadataPathFinder + return MetadataPathFinder.find_distributions(*args, **kwargs) + - Return an iterable of all Distribution instances capable of - loading the metadata for packages matching ``context.name`` - (or all names if ``None`` indicated) along the paths in the list - of directories ``context.path``. - """ - from importlib.metadata import MetadataPathFinder - return MetadataPathFinder.find_distributions(*args, **kwargs) - - class FileFinder: """File-based finder. @@ -1473,8 +1473,8 @@ class FileFinder: self._loaders = loaders # Base (directory) path self.path = path or '.' - if not _path_isabs(self.path): - self.path = _path_join(_os.getcwd(), self.path) + if not _path_isabs(self.path): + self.path = _path_join(_os.getcwd(), self.path) self._path_mtime = -1 self._path_cache = set() self._relaxed_path_cache = set() @@ -1537,10 +1537,10 @@ class FileFinder: is_namespace = _path_isdir(base_path) # Check for a file w/ a proper suffix exists. for suffix, loader_class in self._loaders: - try: - full_path = _path_join(self.path, tail_module + suffix) - except ValueError: - return None + try: + full_path = _path_join(self.path, tail_module + suffix) + except ValueError: + return None _bootstrap._verbose_message('trying {}', full_path, verbosity=2) if cache_module + suffix in cache: if _path_isfile(full_path): @@ -1673,22 +1673,22 @@ def _setup(_bootstrap_module): continue else: raise ImportError('importlib requires posix or nt') - + setattr(self_module, '_os', os_module) setattr(self_module, 'path_sep', path_sep) setattr(self_module, 'path_separators', ''.join(path_separators)) - setattr(self_module, '_pathseps_with_colon', {f':{s}' for s in path_separators}) + setattr(self_module, '_pathseps_with_colon', {f':{s}' for s in path_separators}) - # Directly load built-in modules needed during bootstrap. - builtin_names = ['_io', '_warnings', 'marshal'] + # Directly load built-in modules needed during bootstrap. + builtin_names = ['_io', '_warnings', 'marshal'] if builtin_os == 'nt': - builtin_names.append('winreg') - for builtin_name in builtin_names: - if builtin_name not in sys.modules: - builtin_module = _bootstrap._builtin_from_name(builtin_name) - else: - builtin_module = sys.modules[builtin_name] - setattr(self_module, builtin_name, builtin_module) + builtin_names.append('winreg') + for builtin_name in builtin_names: + if builtin_name not in sys.modules: + builtin_module = _bootstrap._builtin_from_name(builtin_name) + else: + builtin_module = sys.modules[builtin_name] + setattr(self_module, builtin_name, builtin_module) # Constants setattr(self_module, '_relax_case', _make_relax_case()) |
