Skip to content

Commit 03f14aa

Browse files
committed
naming and inheritance for importlib
This MR breaks out _frozen_importlib_external (which is the same thing as importlib._bootstrap_external) and _frozen_importlib (which is the same thing as importlib._bootstrap). related to python#3968
1 parent 2a6ba80 commit 03f14aa

File tree

8 files changed

+291
-207
lines changed

8 files changed

+291
-207
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ _collections_abc.ItemsView.__reversed__
1313
_collections_abc.KeysView.__reversed__
1414
_collections_abc.ValuesView.__reversed__
1515
_ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed
16+
_frozen_importlib_external.ExtensionFileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
17+
_frozen_importlib_external.FileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
18+
_frozen_importlib_external.FileLoader.get_resource_reader # Wrapped with _check_name decorator which changes runtime signature
19+
_frozen_importlib_external.FileLoader.load_module # Wrapped with _check_name decorator which changes runtime signature
1620
_threading_local.local.__new__
1721
ast.Bytes.__new__
1822
ast.Ellipsis.__new__
@@ -44,6 +48,10 @@ hmac.new # Stub is a white lie; see comments in the stub
4448
http.HTTPStatus.description # set in __new__
4549
http.HTTPStatus.phrase # set in __new__
4650
http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta
51+
importlib._bootstrap_external.ExtensionFileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
52+
importlib._bootstrap_external.FileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
53+
importlib._bootstrap_external.FileLoader.get_resource_reader # Wrapped with _check_name decorator which changes runtime signature
54+
importlib._bootstrap_external.FileLoader.load_module # Wrapped with _check_name decorator which changes runtime signature
4755
importlib.abc.FileLoader.get_filename # Wrapped with _check_name decorator which changes runtime signature
4856
importlib.abc.FileLoader.load_module # Wrapped with _check_name decorator which changes runtime signature
4957
importlib.abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined for backwards compatibility
@@ -262,6 +270,12 @@ idlelib
262270
importlib.machinery.WindowsRegistryFinder.DEBUG_BUILD
263271
importlib.machinery.WindowsRegistryFinder.REGISTRY_KEY
264272
importlib.machinery.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
273+
_frozen_importlib_external.WindowsRegistryFinder.DEBUG_BUILD
274+
_frozen_importlib_external.WindowsRegistryFinder.REGISTRY_KEY
275+
_frozen_importlib_external.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
276+
importlib._bootstrap_external.WindowsRegistryFinder.DEBUG_BUILD
277+
importlib._bootstrap_external.WindowsRegistryFinder.REGISTRY_KEY
278+
importlib._bootstrap_external.WindowsRegistryFinder.REGISTRY_KEY_DEBUG
265279

266280
# Undocumented implementation details
267281
profile.Profile.dispatch

stdlib/VERSIONS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ _curses: 3.0-
3333
_decimal: 3.3-
3434
_dummy_thread: 3.0-3.8
3535
_dummy_threading: 3.0-3.8
36+
_frozen_importlib: 3.0-
37+
_frozen_importlib_external: 3.0-
3638
_heapq: 3.0-
3739
_imp: 3.0-
3840
_interpchannels: 3.13-

stdlib/_frozen_importlib.pyi

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import importlib.abc
2+
import importlib.machinery
3+
import sys
4+
import types
5+
from _typeshed.importlib import LoaderProtocol
6+
from collections.abc import Mapping, Sequence
7+
from types import ModuleType
8+
from typing import Any
9+
10+
# Signature of `builtins.__import__` should be kept identical to `importlib.__import__`
11+
def __import__(
12+
name: str,
13+
globals: Mapping[str, object] | None = None,
14+
locals: Mapping[str, object] | None = None,
15+
fromlist: Sequence[str] = (),
16+
level: int = 0,
17+
) -> ModuleType: ...
18+
def spec_from_loader(
19+
name: str, loader: LoaderProtocol | None, *, origin: str | None = None, is_package: bool | None = None
20+
) -> importlib.machinery.ModuleSpec | None: ...
21+
def module_from_spec(spec: importlib.machinery.ModuleSpec) -> types.ModuleType: ...
22+
23+
class ModuleSpec:
24+
def __init__(
25+
self,
26+
name: str,
27+
loader: importlib.abc.Loader | None,
28+
*,
29+
origin: str | None = None,
30+
loader_state: Any = None,
31+
is_package: bool | None = None,
32+
) -> None: ...
33+
name: str
34+
loader: importlib.abc.Loader | None
35+
origin: str | None
36+
submodule_search_locations: list[str] | None
37+
loader_state: Any
38+
cached: str | None
39+
@property
40+
def parent(self) -> str | None: ...
41+
has_location: bool
42+
def __eq__(self, other: object) -> bool: ...
43+
44+
class BuiltinImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
45+
# MetaPathFinder
46+
if sys.version_info < (3, 12):
47+
@classmethod
48+
def find_module(cls, fullname: str, path: Sequence[str] | None = None) -> importlib.abc.Loader | None: ...
49+
50+
@classmethod
51+
def find_spec(
52+
cls, fullname: str, path: Sequence[str] | None = None, target: types.ModuleType | None = None
53+
) -> ModuleSpec | None: ...
54+
# InspectLoader
55+
@classmethod
56+
def is_package(cls, fullname: str) -> bool: ...
57+
@classmethod
58+
def load_module(cls, fullname: str) -> types.ModuleType: ...
59+
@classmethod
60+
def get_code(cls, fullname: str) -> None: ...
61+
@classmethod
62+
def get_source(cls, fullname: str) -> None: ...
63+
# Loader
64+
if sys.version_info < (3, 12):
65+
@staticmethod
66+
def module_repr(module: types.ModuleType) -> str: ...
67+
if sys.version_info >= (3, 10):
68+
@staticmethod
69+
def create_module(spec: ModuleSpec) -> types.ModuleType | None: ...
70+
@staticmethod
71+
def exec_module(module: types.ModuleType) -> None: ...
72+
else:
73+
@classmethod
74+
def create_module(cls, spec: ModuleSpec) -> types.ModuleType | None: ...
75+
@classmethod
76+
def exec_module(cls, module: types.ModuleType) -> None: ...
77+
78+
class FrozenImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
79+
# MetaPathFinder
80+
if sys.version_info < (3, 12):
81+
@classmethod
82+
def find_module(cls, fullname: str, path: Sequence[str] | None = None) -> importlib.abc.Loader | None: ...
83+
84+
@classmethod
85+
def find_spec(
86+
cls, fullname: str, path: Sequence[str] | None = None, target: types.ModuleType | None = None
87+
) -> ModuleSpec | None: ...
88+
# InspectLoader
89+
@classmethod
90+
def is_package(cls, fullname: str) -> bool: ...
91+
@classmethod
92+
def load_module(cls, fullname: str) -> types.ModuleType: ...
93+
@classmethod
94+
def get_code(cls, fullname: str) -> None: ...
95+
@classmethod
96+
def get_source(cls, fullname: str) -> None: ...
97+
# Loader
98+
if sys.version_info < (3, 12):
99+
@staticmethod
100+
def module_repr(m: types.ModuleType) -> str: ...
101+
if sys.version_info >= (3, 10):
102+
@staticmethod
103+
def create_module(spec: ModuleSpec) -> types.ModuleType | None: ...
104+
else:
105+
@classmethod
106+
def create_module(cls, spec: ModuleSpec) -> types.ModuleType | None: ...
107+
108+
@staticmethod
109+
def exec_module(module: types.ModuleType) -> None: ...

stdlib/_frozen_importlib_external.pyi

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import _ast
2+
import importlib.abc
3+
import importlib.machinery
4+
import importlib.readers
5+
import sys
6+
import types
7+
from _typeshed import Incomplete, ReadableBuffer, StrOrBytesPath, StrPath
8+
from _typeshed.importlib import LoaderProtocol
9+
from collections.abc import Callable, Iterable, Mapping, MutableSequence, Sequence
10+
from importlib.machinery import ModuleSpec
11+
from importlib.metadata import DistributionFinder, PathDistribution
12+
from typing import Any, Literal
13+
from typing_extensions import deprecated
14+
15+
path_separators: Incomplete
16+
path_sep: Incomplete
17+
path_sep_tuple: Incomplete
18+
19+
MAGIC_NUMBER: bytes
20+
21+
def cache_from_source(path: str, debug_override: bool | None = None, *, optimization: Any | None = None) -> str: ...
22+
def source_from_cache(path: str) -> str: ...
23+
def decode_source(source_bytes: ReadableBuffer) -> str: ...
24+
def spec_from_file_location(
25+
name: str,
26+
location: StrOrBytesPath | None = None,
27+
*,
28+
loader: LoaderProtocol | None = None,
29+
submodule_search_locations: list[str] | None = ...,
30+
) -> importlib.machinery.ModuleSpec | None: ...
31+
32+
class WindowsRegistryFinder(importlib.abc.MetaPathFinder):
33+
if sys.version_info < (3, 12):
34+
@classmethod
35+
def find_module(cls, fullname: str, path: Sequence[str] | None = None) -> importlib.abc.Loader | None: ...
36+
37+
@classmethod
38+
def find_spec(
39+
cls, fullname: str, path: Sequence[str] | None = None, target: types.ModuleType | None = None
40+
) -> ModuleSpec | None: ...
41+
42+
class PathFinder(importlib.abc.MetaPathFinder):
43+
if sys.version_info >= (3, 10):
44+
@staticmethod
45+
def invalidate_caches() -> None: ...
46+
else:
47+
@classmethod
48+
def invalidate_caches(cls) -> None: ...
49+
if sys.version_info >= (3, 10):
50+
@staticmethod
51+
def find_distributions(context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ...
52+
else:
53+
@classmethod
54+
def find_distributions(cls, context: DistributionFinder.Context = ...) -> Iterable[PathDistribution]: ...
55+
56+
@classmethod
57+
def find_spec(
58+
cls, fullname: str, path: Sequence[str] | None = None, target: types.ModuleType | None = None
59+
) -> ModuleSpec | None: ...
60+
if sys.version_info < (3, 12):
61+
@classmethod
62+
def find_module(cls, fullname: str, path: Sequence[str] | None = None) -> importlib.abc.Loader | None: ...
63+
64+
SOURCE_SUFFIXES: list[str]
65+
DEBUG_BYTECODE_SUFFIXES: list[str]
66+
OPTIMIZED_BYTECODE_SUFFIXES: list[str]
67+
BYTECODE_SUFFIXES: list[str]
68+
EXTENSION_SUFFIXES: list[str]
69+
70+
class FileFinder(importlib.abc.PathEntryFinder):
71+
path: str
72+
def __init__(self, path: str, *loader_details: tuple[type[importlib.abc.Loader], list[str]]) -> None: ...
73+
@classmethod
74+
def path_hook(
75+
cls, *loader_details: tuple[type[importlib.abc.Loader], list[str]]
76+
) -> Callable[[str], importlib.abc.PathEntryFinder]: ...
77+
78+
class _LoaderBasics:
79+
def is_package(self, fullname: str) -> bool: ...
80+
def create_module(self, spec: ModuleSpec) -> types.ModuleType | None: ...
81+
def exec_module(self, module: types.ModuleType) -> None: ...
82+
def load_module(self, fullname: str) -> types.ModuleType: ...
83+
84+
class SourceLoader(_LoaderBasics):
85+
def path_mtime(self, path: str) -> float: ...
86+
def set_data(self, path: str, data: bytes) -> None: ...
87+
def get_source(self, fullname: str) -> str | None: ...
88+
def path_stats(self, path: str) -> Mapping[str, Any]: ...
89+
def source_to_code(
90+
self, data: ReadableBuffer | str | _ast.Module | _ast.Expression | _ast.Interactive, path: ReadableBuffer | StrPath
91+
) -> types.CodeType: ...
92+
def get_code(self, fullname: str) -> types.CodeType | None: ...
93+
94+
class FileLoader:
95+
name: str
96+
path: str
97+
def __init__(self, fullname: str, path: str) -> None: ...
98+
def get_data(self, path: str) -> bytes: ...
99+
def get_filename(self, fullname: str | None = None) -> str: ...
100+
def load_module(self, fullname: str | None = None) -> types.ModuleType: ...
101+
def get_resource_reader(self, module: types.ModuleType) -> importlib.readers.FileReader: ...
102+
103+
class SourceFileLoader(FileLoader, SourceLoader, importlib.abc.FileLoader, importlib.abc.SourceLoader): # type: ignore[misc] # Cannot determine consistent method resolution order - ABCs create circular inheritance
104+
def set_data(self, path: str, data: ReadableBuffer, *, _mode: int = 0o666) -> None: ...
105+
def path_stats(self, path: str) -> Mapping[str, Any]: ...
106+
107+
class SourcelessFileLoader(FileLoader, _LoaderBasics, importlib.abc.FileLoader): # type: ignore[misc] # Cannot determine consistent method resolution order - ABCs create circular inheritance
108+
def get_code(self, fullname: str) -> types.CodeType | None: ...
109+
def get_source(self, fullname: str) -> None: ...
110+
111+
class ExtensionFileLoader(FileLoader, _LoaderBasics, importlib.abc.ExecutionLoader):
112+
def __init__(self, name: str, path: str) -> None: ...
113+
def get_filename(self, name: str | None = None) -> str: ...
114+
def get_source(self, fullname: str) -> None: ...
115+
def create_module(self, spec: ModuleSpec) -> types.ModuleType: ...
116+
def exec_module(self, module: types.ModuleType) -> None: ...
117+
def get_code(self, fullname: str) -> None: ...
118+
def __eq__(self, other: object) -> bool: ...
119+
def __hash__(self) -> int: ...
120+
121+
if sys.version_info >= (3, 11):
122+
class NamespaceLoader(importlib.abc.InspectLoader):
123+
def __init__(
124+
self, name: str, path: MutableSequence[str], path_finder: Callable[[str, tuple[str, ...]], ModuleSpec]
125+
) -> None: ...
126+
def is_package(self, fullname: str) -> Literal[True]: ...
127+
def get_source(self, fullname: str) -> Literal[""]: ...
128+
def get_code(self, fullname: str) -> types.CodeType: ...
129+
def create_module(self, spec: ModuleSpec) -> None: ...
130+
def exec_module(self, module: types.ModuleType) -> None: ...
131+
@deprecated("load_module() is deprecated; use exec_module() instead")
132+
def load_module(self, fullname: str) -> types.ModuleType: ...
133+
def get_resource_reader(self, module: types.ModuleType) -> importlib.readers.NamespaceReader: ...
134+
if sys.version_info < (3, 12):
135+
@staticmethod
136+
@deprecated("module_repr() is deprecated, and has been removed in Python 3.12")
137+
def module_repr(module: types.ModuleType) -> str: ...

stdlib/importlib/__init__.pyi

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import sys
2-
from collections.abc import Mapping, Sequence
2+
from importlib._bootstrap import __import__ as __import__
33
from importlib.abc import Loader
44
from types import ModuleType
55

66
__all__ = ["__import__", "import_module", "invalidate_caches", "reload"]
77

8-
# Signature of `builtins.__import__` should be kept identical to `importlib.__import__`
9-
def __import__(
10-
name: str,
11-
globals: Mapping[str, object] | None = None,
12-
locals: Mapping[str, object] | None = None,
13-
fromlist: Sequence[str] = (),
14-
level: int = 0,
15-
) -> ModuleType: ...
16-
178
# `importlib.import_module` return type should be kept the same as `builtins.__import__`
189
def import_module(name: str, package: str | None = None) -> ModuleType: ...
1910

stdlib/importlib/abc.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import types
44
from _typeshed import ReadableBuffer, StrPath
55
from abc import ABCMeta, abstractmethod
66
from collections.abc import Iterator, Mapping, Sequence
7+
from importlib import _bootstrap_external
78
from importlib.machinery import ModuleSpec
89
from io import BufferedReader
910
from typing import IO, Any, Literal, Protocol, overload, runtime_checkable
@@ -56,7 +57,7 @@ class ExecutionLoader(InspectLoader):
5657
@abstractmethod
5758
def get_filename(self, fullname: str) -> str: ...
5859

59-
class SourceLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
60+
class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLoader, metaclass=ABCMeta): # type: ignore[misc] # incompatible definitions of source_to_code in the base classes
6061
def path_mtime(self, path: str) -> float: ...
6162
def set_data(self, path: str, data: bytes) -> None: ...
6263
def get_source(self, fullname: str) -> str | None: ...
@@ -101,7 +102,7 @@ else:
101102
# Not defined on the actual class, but expected to exist.
102103
def find_spec(self, fullname: str, target: types.ModuleType | None = ...) -> ModuleSpec | None: ...
103104

104-
class FileLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
105+
class FileLoader(_bootstrap_external.FileLoader, ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
105106
name: str
106107
path: str
107108
def __init__(self, fullname: str, path: str) -> None: ...

0 commit comments

Comments
 (0)