Skip to content

Commit af21a3b

Browse files
CPython Developersyouknowone
authored andcommitted
Update importlib/test_importlib from CPython 3.10.6
1 parent c0928b3 commit af21a3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4725
-1426
lines changed

Lib/importlib/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import _frozen_importlib_external as _bootstrap_external
3535
except ImportError:
3636
from . import _bootstrap_external
37-
_bootstrap_external._setup(_bootstrap)
37+
_bootstrap_external._set_bootstrap_module(_bootstrap)
3838
_bootstrap._bootstrap_external = _bootstrap_external
3939
else:
4040
_bootstrap_external.__name__ = 'importlib._bootstrap_external'
@@ -54,7 +54,6 @@
5454
# Fully bootstrapped at this point, import whatever you like, circular
5555
# dependencies and startup overhead minimisation permitting :)
5656

57-
import types
5857
import warnings
5958

6059

@@ -79,8 +78,8 @@ def find_loader(name, path=None):
7978
This function is deprecated in favor of importlib.util.find_spec().
8079
8180
"""
82-
warnings.warn('Deprecated since Python 3.4. '
83-
'Use importlib.util.find_spec() instead.',
81+
warnings.warn('Deprecated since Python 3.4 and slated for removal in '
82+
'Python 3.12; use importlib.util.find_spec() instead',
8483
DeprecationWarning, stacklevel=2)
8584
try:
8685
loader = sys.modules[name].__loader__
@@ -136,12 +135,13 @@ def reload(module):
136135
The module must have been successfully imported before.
137136
138137
"""
139-
if not module or not isinstance(module, types.ModuleType):
140-
raise TypeError("reload() argument must be a module")
141138
try:
142139
name = module.__spec__.name
143140
except AttributeError:
144-
name = module.__name__
141+
try:
142+
name = module.__name__
143+
except AttributeError:
144+
raise TypeError("reload() argument must be a module")
145145

146146
if sys.modules.get(name) is not module:
147147
msg = "module {} not in sys.modules"

Lib/importlib/_abc.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Subset of importlib.abc used to reduce importlib.util imports."""
2+
from . import _bootstrap
3+
import abc
4+
import warnings
5+
6+
7+
class Loader(metaclass=abc.ABCMeta):
8+
9+
"""Abstract base class for import loaders."""
10+
11+
def create_module(self, spec):
12+
"""Return a module to initialize and into which to load.
13+
14+
This method should raise ImportError if anything prevents it
15+
from creating a new module. It may return None to indicate
16+
that the spec should create the new module.
17+
"""
18+
# By default, defer to default semantics for the new module.
19+
return None
20+
21+
# We don't define exec_module() here since that would break
22+
# hasattr checks we do to support backward compatibility.
23+
24+
def load_module(self, fullname):
25+
"""Return the loaded module.
26+
27+
The module must be added to sys.modules and have import-related
28+
attributes set properly. The fullname is a str.
29+
30+
ImportError is raised on failure.
31+
32+
This method is deprecated in favor of loader.exec_module(). If
33+
exec_module() exists then it is used to provide a backwards-compatible
34+
functionality for this method.
35+
36+
"""
37+
if not hasattr(self, 'exec_module'):
38+
raise ImportError
39+
# Warning implemented in _load_module_shim().
40+
return _bootstrap._load_module_shim(self, fullname)
41+
42+
def module_repr(self, module):
43+
"""Return a module's repr.
44+
45+
Used by the module type when the method does not raise
46+
NotImplementedError.
47+
48+
This method is deprecated.
49+
50+
"""
51+
warnings.warn("importlib.abc.Loader.module_repr() is deprecated and "
52+
"slated for removal in Python 3.12", DeprecationWarning)
53+
# The exception will cause ModuleType.__repr__ to ignore this method.
54+
raise NotImplementedError

Lib/importlib/_adapters.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from contextlib import suppress
2+
3+
from . import abc
4+
5+
6+
class SpecLoaderAdapter:
7+
"""
8+
Adapt a package spec to adapt the underlying loader.
9+
"""
10+
11+
def __init__(self, spec, adapter=lambda spec: spec.loader):
12+
self.spec = spec
13+
self.loader = adapter(spec)
14+
15+
def __getattr__(self, name):
16+
return getattr(self.spec, name)
17+
18+
19+
class TraversableResourcesLoader:
20+
"""
21+
Adapt a loader to provide TraversableResources.
22+
"""
23+
24+
def __init__(self, spec):
25+
self.spec = spec
26+
27+
def get_resource_reader(self, name):
28+
return DegenerateFiles(self.spec)._native()
29+
30+
31+
class DegenerateFiles:
32+
"""
33+
Adapter for an existing or non-existant resource reader
34+
to provide a degenerate .files().
35+
"""
36+
37+
class Path(abc.Traversable):
38+
def iterdir(self):
39+
return iter(())
40+
41+
def is_dir(self):
42+
return False
43+
44+
is_file = exists = is_dir # type: ignore
45+
46+
def joinpath(self, other):
47+
return DegenerateFiles.Path()
48+
49+
@property
50+
def name(self):
51+
return ''
52+
53+
def open(self, mode='rb', *args, **kwargs):
54+
raise ValueError()
55+
56+
def __init__(self, spec):
57+
self.spec = spec
58+
59+
@property
60+
def _reader(self):
61+
with suppress(AttributeError):
62+
return self.spec.loader.get_resource_reader(self.spec.name)
63+
64+
def _native(self):
65+
"""
66+
Return the native reader if it supports files().
67+
"""
68+
reader = self._reader
69+
return reader if hasattr(reader, 'files') else self
70+
71+
def __getattr__(self, attr):
72+
return getattr(self._reader, attr)
73+
74+
def files(self):
75+
return DegenerateFiles.Path()
76+
77+
78+
def wrap_spec(package):
79+
"""
80+
Construct a package spec with traversable compatibility
81+
on the spec/loader/reader.
82+
"""
83+
return SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)

0 commit comments

Comments
 (0)